Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
10 / 10
CRAP
100.00% covered (success)
100.00%
1 / 1
ZTypedPair
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
10 / 10
15
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getDefinition
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
 buildType
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 isValid
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
6
 getFirstType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFirstElement
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setFirstElement
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSecondType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSecondElement
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSecondElement
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * WikiLambda ZTypedPair
4 *
5 * @file
6 * @ingroup Extensions
7 * @copyright 2020– WikiLambda team; see AUTHORS.txt
8 * @license MIT
9 */
10
11namespace MediaWiki\Extension\WikiLambda\ZObjects;
12
13use MediaWiki\Extension\WikiLambda\Registry\ZTypeRegistry;
14
15class ZTypedPair extends ZObject {
16
17    private ZTypeRegistry $typeRegistry;
18
19    /**
20     * Create a new ZTypedPair instance
21     *
22     * @param ZFunctionCall $functionCall
23     * @param ZObject|null $firstItem
24     * @param ZObject|null $secondItem
25     */
26    public function __construct( $functionCall, $firstItem = null, $secondItem = null ) {
27        $this->type = $functionCall;
28
29        $this->data[ 'K1' ] = $firstItem;
30        $this->data[ 'K2' ] = $secondItem;
31
32        $this->typeRegistry = ZTypeRegistry::singleton();
33    }
34
35    /**
36     * @inheritDoc
37     */
38    public static function getDefinition(): array {
39        return [
40            'type' => [
41                'type' => ZTypeRegistry::Z_FUNCTIONCALL,
42                'value' => ZTypeRegistry::Z_FUNCTION_TYPED_PAIR,
43            ],
44            'keys' => [
45                'K1' => [
46                    'type' => ZTypeRegistry::Z_OBJECT,
47                ],
48                'K2' => [
49                    'type' => ZTypeRegistry::Z_OBJECT,
50                ],
51            ],
52        ];
53    }
54
55    /**
56     * Build the function call that defines the type of this Typed Pair
57     *
58     * @param string $firstType
59     * @param string $secondType
60     * @return ZFunctionCall
61     */
62    public static function buildType( $firstType, $secondType ): ZFunctionCall {
63        return new ZFunctionCall(
64            new ZReference( ZTypeRegistry::Z_FUNCTION_TYPED_PAIR ),
65            [
66                ZTypeRegistry::Z_FUNCTION_TYPED_FIRST_TYPE => new ZReference( $firstType ),
67                ZTypeRegistry::Z_FUNCTION_TYPED_SECOND_TYPE => new ZReference( $secondType )
68            ]
69        );
70    }
71
72    /**
73     * Valid if both the first and second items are extant ZObjects of the right type (or the type is Z1)
74     *
75     * @inheritDoc
76     */
77    public function isValid(): bool {
78        $firstItem = $this->data[ 'K1' ] ?? null;
79        $secondItem = $this->data[ 'K2' ] ?? null;
80
81        if ( !( $firstItem instanceof ZObject && $secondItem instanceof ZObject ) ) {
82            return false;
83        }
84
85        if ( !( $firstItem->isValid() && $secondItem->isValid() ) ) {
86            return false;
87        }
88
89        return (
90            $this->typeRegistry->isZObjectInstanceOfType( $firstItem, $this->getFirstType()->getZValue() ) &&
91            $this->typeRegistry->isZObjectInstanceOfType( $secondItem, $this->getSecondType()->getZValue() )
92        );
93    }
94
95    /**
96     * Returns the type of the first element of this ZTypedPair
97     *
98     * @return ZReference The type of the first element
99     */
100    public function getFirstType(): ZReference {
101        // @phan-suppress-next-line PhanTypeMismatchReturnSuperType phan can't tell this must be a ZReference
102        return $this->type->getValueByKey( ZTypeRegistry::Z_FUNCTION_TYPED_FIRST_TYPE );
103    }
104
105    /**
106     * Returns the first element of this ZTypedPair
107     *
108     * @return ?ZObject The first element
109     */
110    public function getFirstElement(): ?ZObject {
111        return $this->data[ 'K1' ] ?? null;
112    }
113
114    /**
115     * Sets the first element of this ZTypedPair
116     *
117     * @param ZObject $newValue The new first element
118     */
119    public function setFirstElement( ZObject $newValue ) {
120        $this->data[ 'K1' ] = $newValue;
121    }
122
123    /**
124     * Returns the type of the second element of this ZTypedPair
125     *
126     * @return ZReference The type of the second element
127     */
128    public function getSecondType(): ZReference {
129        // @phan-suppress-next-line PhanTypeMismatchReturnSuperType phan can't tell this must be a ZReference
130        return $this->type->getValueByKey( ZTypeRegistry::Z_FUNCTION_TYPED_SECOND_TYPE );
131    }
132
133    /**
134     * Returns the second element of this ZTypedPair
135     *
136     * @return ?ZObject The second element
137     */
138    public function getSecondElement(): ?ZObject {
139        return $this->data[ 'K2' ] ?? null;
140    }
141
142    /**
143     * Sets the second element of this ZTypedPair
144     *
145     * @param ZObject $newValue The new second element
146     */
147    public function setSecondElement( ZObject $newValue ) {
148        $this->data[ 'K2' ] = $newValue;
149    }
150}