Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
69.57% covered (warning)
69.57%
32 / 46
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ZMonoLingualStringSet
69.57% covered (warning)
69.57%
32 / 46
71.43% covered (warning)
71.43%
5 / 7
16.06
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
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
66.67% covered (warning)
66.67%
6 / 9
0.00% covered (danger)
0.00%
0 / 1
5.93
 getSerialized
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 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
 getStringSet
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * WikiLambda ZMonoLingualStringSet
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;
15use MediaWiki\Extension\WikiLambda\ZObjectUtils;
16
17class ZMonoLingualStringSet extends ZObject {
18
19    /**
20     * Create a ZMonoLingualStringSet instance given a language ZReference and an array
21     * or ZTypedList of ZString instances. Internally this class bypasses ZTypedList
22     * and stores an array.
23     *
24     * @param ZReference $language
25     * @param ZTypedList|array $value
26     */
27    public function __construct( $language, $value = [] ) {
28        $this->data[ ZTypeRegistry::Z_MONOLINGUALSTRINGSET_LANGUAGE ] = $language;
29        $this->data[ ZTypeRegistry::Z_MONOLINGUALSTRINGSET_VALUE ] = [];
30        foreach ( ZObjectUtils::getIterativeList( $value ) as $index => $element ) {
31            $this->data[ ZTypeRegistry::Z_MONOLINGUALSTRINGSET_VALUE ][] = $element;
32        }
33    }
34
35    /**
36     * @inheritDoc
37     */
38    public static function getDefinition(): array {
39        return [
40            'type' => [
41                'type' => ZTypeRegistry::Z_REFERENCE,
42                'value' => ZTypeRegistry::Z_MONOLINGUALSTRINGSET,
43            ],
44            'keys' => [
45                ZTypeRegistry::Z_MONOLINGUALSTRINGSET_LANGUAGE => [
46                    'type' => ZTypeRegistry::HACK_REFERENCE_LANGUAGE,
47                    'required' => true,
48                ],
49                ZTypeRegistry::Z_MONOLINGUALSTRINGSET_VALUE => [
50                    'type' => ZTypeRegistry::HACK_ARRAY_Z_STRING,
51                    'required' => true,
52                ],
53            ],
54        ];
55    }
56
57    /**
58     * @inheritDoc
59     */
60    public function isValid(): bool {
61        if ( !( $this->data[ ZTypeRegistry::Z_MONOLINGUALSTRINGSET_LANGUAGE ] instanceof ZReference ) ) {
62            return false;
63        }
64        if ( !is_array( $this->data[ ZTypeRegistry::Z_MONOLINGUALSTRINGSET_VALUE ] ) ) {
65            return false;
66        }
67        foreach ( $this->data[ ZTypeRegistry::Z_MONOLINGUALSTRINGSET_VALUE ] as $value ) {
68            if ( !( $value instanceof ZString ) ) {
69                return false;
70            }
71        }
72        // We also check validity of the language Zid.
73        $langs = ZLangRegistry::singleton();
74        return $langs->isValidLanguageZid( $this->getLanguage() );
75    }
76
77    /**
78     * @inheritDoc
79     */
80    public function getSerialized( $form = self::FORM_CANONICAL ) {
81        $listType = new ZReference( ZTypeRegistry::Z_STRING );
82        $language = $this->data[ ZTypeRegistry::Z_MONOLINGUALSTRINGSET_LANGUAGE ];
83        $typedList = new ZTypedList(
84            ZTypedList::buildType( $listType ),
85            $this->data[ ZTypeRegistry::Z_MONOLINGUALSTRINGSET_VALUE ]
86        );
87        return (object)[
88            ZTypeRegistry::Z_OBJECT_TYPE => $this->getZTypeObject()->getSerialized( $form ),
89            ZTypeRegistry::Z_MONOLINGUALSTRINGSET_LANGUAGE => $language->getSerialized( $form ),
90            ZTypeRegistry::Z_MONOLINGUALSTRINGSET_VALUE => $typedList->getSerialized( $form )
91        ];
92    }
93
94    /**
95     * Get a map representing this ZMonoLingualString language and string set.
96     *
97     * @return array Language and string set of this ZMonoLingualString
98     */
99    public function getZValue() {
100        return [ $this->getLanguage() => $this->getStringSet() ];
101    }
102
103    /**
104     * Get the Zid that represents the language for this ZMonoLingualStringSet
105     *
106     * @return string Language Zid
107     */
108    public function getLanguage() {
109        return $this->data[ ZTypeRegistry::Z_MONOLINGUALSTRINGSET_LANGUAGE ]->getZValue() ?? '';
110    }
111
112    /**
113     * Get an array of strings values contained by this ZMonoLingualStringSet
114     *
115     * @return string[] Set of string values
116     */
117    public function getStringSet() {
118        return array_map( static function ( $zstring ) {
119            return $zstring->getZValue();
120        }, $this->data[ ZTypeRegistry::Z_MONOLINGUALSTRINGSET_VALUE ]
121        );
122    }
123}