Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
ZBoolean
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
4 / 4
12
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
 getDefinition
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 isValid
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
 getZValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * WikiLambda ZBoolean
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;
14
15class ZBoolean extends ZObject {
16
17    /**
18     * @inheritDoc
19     */
20    public function __construct( $value = '' ) {
21        if ( is_bool( $value ) ) {
22            $this->data[ ZTypeRegistry::Z_BOOLEAN_VALUE ] = new ZReference( $value
23                ? ZTypeRegistry::Z_BOOLEAN_TRUE
24                : ZTypeRegistry::Z_BOOLEAN_FALSE
25            );
26            return;
27        }
28
29        if ( $value instanceof ZReference ) {
30            $this->data[ZTypeRegistry::Z_BOOLEAN_VALUE] = $value;
31            return;
32        }
33
34        // Otherwise give up and have it as null
35        $this->data[ ZTypeRegistry::Z_BOOLEAN_VALUE ] = null;
36    }
37
38    /**
39     * @inheritDoc
40     */
41    public static function getDefinition(): array {
42        return [
43            'type' => [
44                'type' => ZTypeRegistry::Z_REFERENCE,
45                'value' => ZTypeRegistry::Z_BOOLEAN,
46            ],
47            'keys' => [
48                ZTypeRegistry::Z_BOOLEAN_VALUE => [
49                    'type' => ZTypeRegistry::BUILTIN_REFERENCE,
50                    'required' => true
51                ],
52            ],
53        ];
54    }
55
56    /**
57     * @inheritDoc
58     */
59    public function isValid(): bool {
60        if ( $this->data[ZTypeRegistry::Z_BOOLEAN_VALUE] === null ) {
61            return false;
62        }
63        if (
64            $this->data[ZTypeRegistry::Z_BOOLEAN_VALUE] instanceof ZReference &&
65            $this->data[ZTypeRegistry::Z_BOOLEAN_VALUE]->isValid() &&
66            (
67                $this->data[ZTypeRegistry::Z_BOOLEAN_VALUE]->getZValue() === ZTypeRegistry::Z_BOOLEAN_TRUE ||
68                $this->data[ZTypeRegistry::Z_BOOLEAN_VALUE]->getZValue() === ZTypeRegistry::Z_BOOLEAN_FALSE
69            )
70        ) {
71            return true;
72        }
73
74        return false;
75    }
76
77    /**
78     * @return ZReference|null
79     */
80    public function getZValue() {
81        return $this->data[ ZTypeRegistry::Z_BOOLEAN_VALUE ];
82    }
83}