Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
52 / 52
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
ZKey
100.00% covered (success)
100.00%
52 / 52
100.00% covered (success)
100.00%
6 / 6
18
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getDefinition
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
1
 isValid
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
13
 getKeyType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getKeyId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getKeyLabel
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * WikiLambda ZKey
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;
14use MediaWiki\Extension\WikiLambda\ZObjectUtils;
15
16class ZKey extends ZObject {
17
18    /**
19     * Construct a new ZKey instance
20     *
21     * @param ZObject $type ZReference to the type for this key value
22     * @param ZObject $identity ZString with the key ID
23     * @param ZObject $label ZMultiLingualString that contains the label of this key
24     */
25    public function __construct( $type, $identity, $label ) {
26        $this->data[ ZTypeRegistry::Z_KEY_TYPE ] = $type;
27        $this->data[ ZTypeRegistry::Z_KEY_ID ] = $identity;
28        $this->data[ ZTypeRegistry::Z_KEY_LABEL ] = $label;
29    }
30
31    /**
32     * @inheritDoc
33     */
34    public static function getDefinition(): array {
35        return [
36            'type' => [
37                'type' => ZTypeRegistry::Z_REFERENCE,
38                'value' => ZTypeRegistry::Z_KEY,
39            ],
40            'keys' => [
41                ZTypeRegistry::Z_KEY_TYPE => [
42                    'type' => ZTypeRegistry::HACK_REFERENCE_TYPE,
43                    'required' => true,
44                ],
45                ZTypeRegistry::Z_KEY_ID => [
46                    // TODO (T262097): Per the model, we used to dereference this ZReference into
47                    // the string of its ZType, but creates recursion issues when evaluating ZKeys
48                    // of ZTypes that are being created. For now, just store the string ZReference.
49                    'type' => ZTypeRegistry::Z_TYPE_IDENTITY,
50                    'required' => true,
51                ],
52                ZTypeRegistry::Z_KEY_LABEL => [
53                    'type' => ZTypeRegistry::Z_MULTILINGUALSTRING,
54                ],
55            ],
56        ];
57    }
58
59    /**
60     * @inheritDoc
61     */
62    public function isValid(): bool {
63        if ( !isset( $this->data[ ZTypeRegistry::Z_KEY_TYPE ] ) ) {
64            return false;
65        }
66        if ( !( $this->data[ ZTypeRegistry::Z_KEY_TYPE ] instanceof ZReference ) ) {
67            return false;
68        }
69        $type = $this->data[ ZTypeRegistry::Z_KEY_TYPE ]->getZValue();
70        if ( !ZObjectUtils::isValidZObjectReference( $type ) ) {
71            return false;
72        }
73
74        // Identity must be a global reference (LATER: or a built instance of global references)
75        if ( !isset( $this->data[ ZTypeRegistry::Z_KEY_ID ] ) ) {
76            return false;
77        }
78        if ( !( $this->data[ ZTypeRegistry::Z_KEY_ID ] instanceof ZString ) ) {
79            return false;
80        }
81        $identity = $this->data[ ZTypeRegistry::Z_KEY_ID ]->getZValue();
82        if ( !ZObjectUtils::isValidZObjectGlobalKey( $identity ) ) {
83            return false;
84        }
85
86        // Label must be an array of valid ZMonoLingualStrings or a valid ZMultiLingualString
87        if ( !isset( $this->data[ ZTypeRegistry::Z_KEY_LABEL ] ) ) {
88            return false;
89        }
90        $labels = $this->data[ ZTypeRegistry::Z_KEY_LABEL ];
91        if ( $labels instanceof ZMultiLingualString ) {
92            return $labels->isValid();
93        }
94        if ( !is_array( $labels ) ) {
95            return false;
96        }
97        foreach ( $labels as $label ) {
98            if ( !( $label instanceof ZMonoLingualString ) ) {
99                return false;
100            }
101            if ( !$label->isValid() ) {
102                return false;
103            }
104        }
105        return true;
106    }
107
108    /**
109     * Get the Zid for the type that describes the value of this key.
110     *
111     * @return string
112     */
113    public function getKeyType() {
114        return $this->data[ ZTypeRegistry::Z_KEY_TYPE ]->getZValue();
115    }
116
117    /**
118     * Get the ZKey Id
119     *
120     * @return string
121     */
122    public function getKeyId() {
123        return $this->data[ ZTypeRegistry::Z_KEY_ID ]->getZValue();
124    }
125
126    /**
127     * Get the ZMultilingualString that contains the label for this key
128     *
129     * @return ZMultiLingualString
130     */
131    public function getKeyLabel() {
132        return $this->data[ ZTypeRegistry::Z_KEY_LABEL ];
133    }
134}