Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
ZMonoLingualString
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
6 / 6
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getDefinition
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
1
 isValid
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getZValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLanguage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * WikiLambda ZMonoLingualString
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\ZLangRegistry;
14use MediaWiki\Extension\WikiLambda\Registry\ZTypeRegistry;
15
16class ZMonoLingualString extends ZObject {
17
18    /**
19     * Create a ZMonoLingualString instance given a language ZReference and a value ZString
20     *
21     * @param ZReference $language
22     * @param ZString $value
23     */
24    public function __construct( $language, $value ) {
25        $this->data[ ZTypeRegistry::Z_MONOLINGUALSTRING_LANGUAGE ] = $language;
26        $this->data[ ZTypeRegistry::Z_MONOLINGUALSTRING_VALUE ] = $value;
27    }
28
29    /**
30     * @inheritDoc
31     */
32    public static function getDefinition(): array {
33        return [
34            'type' => [
35                'type' => ZTypeRegistry::Z_REFERENCE,
36                'value' => ZTypeRegistry::Z_MONOLINGUALSTRING,
37            ],
38            'keys' => [
39                ZTypeRegistry::Z_MONOLINGUALSTRING_LANGUAGE => [
40                    'type' => ZTypeRegistry::HACK_REFERENCE_LANGUAGE,
41                    'required' => true,
42                ],
43                ZTypeRegistry::Z_MONOLINGUALSTRING_VALUE => [
44                    'type' => ZTypeRegistry::BUILTIN_STRING,
45                    'required' => true,
46                ],
47            ],
48        ];
49    }
50
51    /**
52     * @inheritDoc
53     */
54    public function isValid(): bool {
55        if ( !( $this->data[ ZTypeRegistry::Z_MONOLINGUALSTRING_LANGUAGE ] instanceof ZReference ) ) {
56            return false;
57        }
58        if ( !( $this->data[ ZTypeRegistry::Z_MONOLINGUALSTRING_VALUE ] instanceof ZString ) ) {
59            return false;
60        }
61        // We also check validity of the language Zid.
62        $langs = ZLangRegistry::singleton();
63        return $langs->isValidLanguageZid( $this->getLanguage() );
64    }
65
66    /**
67     * Get a map representing this ZMonoLingualString language and string value.
68     *
69     * @return array Language and string of this ZMonoLingualString
70     */
71    public function getZValue() {
72        return [ $this->getLanguage() => $this->getString() ];
73    }
74
75    /**
76     * Get the Zid that represents the language for this ZMonoLingualString
77     *
78     * @return string Language Zid
79     */
80    public function getLanguage() {
81        return $this->data[ ZTypeRegistry::Z_MONOLINGUALSTRING_LANGUAGE ]->getZValue() ?? '';
82    }
83
84    /**
85     * Get the string value of this ZMonoLingualString
86     *
87     * @return ?string String value
88     */
89    public function getString() {
90        return $this->data[ ZTypeRegistry::Z_MONOLINGUALSTRING_VALUE ]->getZValue();
91    }
92}