Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.70% covered (warning)
50.70%
36 / 71
75.00% covered (warning)
75.00%
9 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
ZPersistentObject
50.70% covered (warning)
50.70%
36 / 71
75.00% covered (warning)
75.00%
9 / 12
86.37
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 getDefinition
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
2
 isValid
62.50% covered (warning)
62.50%
10 / 16
0.00% covered (danger)
0.00%
0 / 1
13.27
 getZValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getZid
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getInnerZObject
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getInternalZType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLabels
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLabel
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 getAliases
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDescriptions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDescription
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
3.02
1<?php
2/**
3 * WikiLambda generic ZPersistentObject class
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 Language;
14use MediaWiki\Extension\WikiLambda\Registry\ZTypeRegistry;
15use MediaWiki\Extension\WikiLambda\ZObjectUtils;
16
17class ZPersistentObject extends ZObject {
18
19    /** @var array */
20    protected $data = [];
21
22    /**
23     * Construct a ZPersistentObject instance
24     *
25     * @param ZObject $zid ZString representing the Zid that identifies this ZPersistentObject
26     * @param ZObject $value ZObject to be wrapped in this ZPersistentObject
27     * @param ZObject $label ZMultiLingualString that contains this ZPersistentObject's label
28     * @param ZObject|null $aliases ZMultiLingualStringSet with this ZPersistentObject's aliases or null
29     * @param ZObject|null $description ZMultiLingualString that contains this ZPersistentObject's description
30     */
31    public function __construct( $zid, $value, $label, $aliases = null, $description = null ) {
32        $aliases ??= new ZMultiLingualStringSet( [] );
33        $this->data[ ZTypeRegistry::Z_PERSISTENTOBJECT_ID ] = $zid;
34        $this->data[ ZTypeRegistry::Z_PERSISTENTOBJECT_VALUE ] = $value;
35        $this->data[ ZTypeRegistry::Z_PERSISTENTOBJECT_LABEL ] = $label;
36        $this->data[ ZTypeRegistry::Z_PERSISTENTOBJECT_ALIASES ] = $aliases;
37        $this->data[ ZTypeRegistry::Z_PERSISTENTOBJECT_DESCRIPTION ] = $description;
38    }
39
40    /**
41     * @inheritDoc
42     */
43    public static function getDefinition(): array {
44        return [
45            'type' => [
46                'type' => ZTypeRegistry::Z_REFERENCE,
47                'value' => ZTypeRegistry::Z_PERSISTENTOBJECT,
48            ],
49            'keys' => [
50                ZTypeRegistry::Z_PERSISTENTOBJECT_ID => [
51                    'type' => ZTypeRegistry::BUILTIN_REFERENCE_NULLABLE,
52                    'required' => true,
53                ],
54                ZTypeRegistry::Z_PERSISTENTOBJECT_VALUE => [
55                    'type' => ZTypeRegistry::Z_OBJECT,
56                    'required' => true,
57                ],
58                ZTypeRegistry::Z_PERSISTENTOBJECT_LABEL => [
59                    'type' => ZTypeRegistry::Z_MULTILINGUALSTRING,
60                    'required' => true,
61                ],
62                ZTypeRegistry::Z_PERSISTENTOBJECT_ALIASES => [
63                    'type' => ZTypeRegistry::Z_MULTILINGUALSTRINGSET,
64                    'required' => false,
65                ],
66                ZTypeRegistry::Z_PERSISTENTOBJECT_DESCRIPTION => [
67                    'type' => ZTypeRegistry::Z_MULTILINGUALSTRING,
68                    'required' => false,
69                ],
70            ],
71        ];
72    }
73
74    /**
75     * @inheritDoc
76     */
77    public function isValid(): bool {
78        // TODO (T296724): we accept ZStrings and ZReferences for now because functions-schemata/data
79        // files are incorrect (Z2K1 contains reference instead of string)
80        if (
81            !( $this->data[ ZTypeRegistry::Z_PERSISTENTOBJECT_ID ] instanceof ZString ) &&
82            !( $this->data[ ZTypeRegistry::Z_PERSISTENTOBJECT_ID ] instanceof ZReference )
83        ) {
84            return false;
85        }
86        $zid = $this->getZid();
87        if ( !ZObjectUtils::isValidOrNullZObjectReference( $zid ) ) {
88            return false;
89        }
90        if ( !( $this->data[ ZTypeRegistry::Z_PERSISTENTOBJECT_VALUE ] instanceof ZObject ) ) {
91            return false;
92        }
93        if ( !( $this->data[ ZTypeRegistry::Z_PERSISTENTOBJECT_LABEL ] instanceof ZMultiLingualString ) ) {
94            return false;
95        }
96        if ( !( $this->data[ ZTypeRegistry::Z_PERSISTENTOBJECT_ALIASES ] instanceof ZMultiLingualStringSet ) ) {
97            return false;
98        }
99        if ( ( $this->data[ ZTypeRegistry::Z_PERSISTENTOBJECT_DESCRIPTION ] !== null ) &&
100            !( $this->data[ ZTypeRegistry::Z_PERSISTENTOBJECT_DESCRIPTION ] instanceof ZMultiLingualString ) ) {
101            return false;
102        }
103        return true;
104    }
105
106    /**
107     * Get the generic content of the inner ZObject wrapped by this ZPersistentObject.
108     *
109     * @return mixed The generic content of the ZObject wrapped by this ZPersistentObject.
110     */
111    public function getZValue() {
112        return $this->getInnerZObject()->getZValue();
113    }
114
115    /**
116     * Get the Zid that identifies this ZPersistentObject.
117     *
118     * @return string The persisted (or null) ZID
119     */
120    public function getZid(): string {
121        return $this->data[ ZTypeRegistry::Z_PERSISTENTOBJECT_ID ]->getZValue();
122    }
123
124    /**
125     * Get the inner ZObject wrapped by this ZPersistentObject.
126     *
127     * @return ZObject The inner ZObject wrapped by this ZPersistentObject
128     */
129    public function getInnerZObject(): ZObject {
130        return $this->data[ ZTypeRegistry::Z_PERSISTENTOBJECT_VALUE ];
131    }
132
133    /**
134     * Get the type Zid of the ZObject wrapped by this ZPersistentObject.
135     * TODO (T296822): The type can also be a function call.
136     *
137     * @return string The type of the internal ZObject
138     */
139    public function getInternalZType(): string {
140        return $this->data[ ZTypeRegistry::Z_PERSISTENTOBJECT_VALUE ]->getZType();
141    }
142
143    /**
144     * Get the ZMultilingualString that contains the label of this ZPersistentObject.
145     *
146     * @return ZMultilingualString The mulilingual string object with the label
147     */
148    public function getLabels(): ZMultilingualString {
149        return $this->data[ ZTypeRegistry::Z_PERSISTENTOBJECT_LABEL ];
150    }
151
152    /**
153     * Get the label for a given Language (or its fallback).
154     *
155     * @param Language $language Language in which to provide the label.
156     * @param bool $defaultToEnglish
157     * @return ?string
158     */
159    public function getLabel( $language, $defaultToEnglish = false ): ?string {
160        if ( $defaultToEnglish ) {
161            return $this->getLabels()
162                ->buildStringForLanguage( $language )
163                ->fallbackWithEnglish()
164                ->getString();
165        }
166        return $this->getLabels()->getStringForLanguage( $language );
167    }
168
169    /**
170     * Get the ZMultilingualStringSet that contains the aliases for this ZPersistentObject.
171     *
172     * @return ZMultilingualStringSet The mulilingual stringset object with the aliases
173     */
174    public function getAliases(): ZMultiLingualStringSet {
175        return $this->data[ ZTypeRegistry::Z_PERSISTENTOBJECT_ALIASES ];
176    }
177
178    /**
179     * Get the ZMultilingualString that contains the description of this ZPersistentObject.
180     *
181     * @return ?ZMultilingualString The mulilingual string object with the description
182     */
183    public function getDescriptions(): ?ZMultilingualString {
184        return $this->data[ ZTypeRegistry::Z_PERSISTENTOBJECT_DESCRIPTION ];
185    }
186
187    /**
188     * Get the description for a given Language (or its fallback).
189     *
190     * @param Language $language Language in which to provide the description.
191     * @param bool $defaultToEnglish
192     * @return ?string
193     */
194    public function getDescription( $language, $defaultToEnglish = false ): ?string {
195        if ( !$this->getDescriptions() ) {
196            return null;
197        }
198        if ( $defaultToEnglish ) {
199            return $this->getDescriptions()
200                ->buildStringForLanguage( $language )
201                ->fallbackWithEnglish()
202                ->getString();
203        }
204        return $this->getDescriptions()->getStringForLanguage( $language );
205    }
206}