Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.24% covered (warning)
88.24%
30 / 34
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ZNaturalLanguage
88.24% covered (warning)
88.24%
30 / 34
75.00% covered (warning)
75.00%
3 / 4
12.23
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getDefinition
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
1
 isValid
71.43% covered (warning)
71.43%
10 / 14
0.00% covered (danger)
0.00%
0 / 1
9.49
 getZValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * WikiLambda ZNaturalLanguage
4 *
5 * @file
6 * @ingroup Extensions
7 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
8 * @license MIT
9 */
10
11namespace MediaWiki\Extension\WikiLambda\ZObjects;
12
13use MediaWiki\Extension\WikiLambda\Registry\ZTypeRegistry;
14
15class ZNaturalLanguage extends ZObject {
16
17    /**
18     * Create a ZNaturalLanguage instance given a code and list of aliases
19     *
20     * @param ZString $code
21     * @param ZTypedList|null $aliases
22     */
23    public function __construct( $code, $aliases = null ) {
24        $this->data[ ZTypeRegistry::Z_LANGUAGE_CODE ] = $code;
25        if ( $aliases ) {
26            $this->data[ ZTYpeRegistry::Z_LANGUAGE_SECONDARYCODES ] = $aliases;
27        }
28    }
29
30    /**
31     * @inheritDoc
32     */
33    public static function getDefinition(): array {
34        return [
35            'type' => [
36                'type' => ZTypeRegistry::Z_REFERENCE,
37                'value' => ZTypeRegistry::Z_LANGUAGE,
38            ],
39            'keys' => [
40                ZTypeRegistry::Z_LANGUAGE_CODE => [
41                    'type' => ZTypeRegistry::BUILTIN_STRING,
42                    'required' => true
43                ],
44                ZTypeRegistry::Z_LANGUAGE_SECONDARYCODES => [
45                    'type' => ZTypeRegistry::HACK_ARRAY_Z_STRING,
46                    'required' => false
47                ],
48            ],
49        ];
50    }
51
52    /**
53     * @inheritDoc
54     */
55    public function isValid(): bool {
56        if ( !( $this->data[ZTypeRegistry::Z_LANGUAGE_CODE] instanceof ZString ) ) {
57            return false;
58        }
59
60        if ( isset( $this->data[ZTypeRegistry::Z_LANGUAGE_SECONDARYCODES] ) ) {
61            $aliases = $this->data[ZTypeRegistry::Z_LANGUAGE_SECONDARYCODES];
62            if ( $aliases instanceof ZTypedList ) {
63                if ( $aliases->getElementType()->getZValue() !== ZTypeRegistry::Z_STRING ) {
64                    return false;
65                }
66                $aliases = $aliases->getAsArray();
67            }
68
69            if ( is_array( $aliases ) ) {
70                foreach ( $aliases as $alias ) {
71                    if ( !( $alias instanceof ZString ) ) {
72                        return false;
73                    }
74                }
75            } else {
76                return false;
77            }
78        }
79        return true;
80    }
81
82    /**
83     * @return string
84     */
85    public function getZValue() {
86        return $this->data[ ZTypeRegistry::Z_LANGUAGE_CODE ]->getZValue();
87    }
88}