Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
ZTypedError
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
6 / 6
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 getDefinition
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 buildType
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 isValid
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSerialized
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getErrorType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * WikiLambda ZTypedError
5 *
6 * @file
7 * @ingroup Extensions
8 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
9 * @license MIT
10 */
11
12namespace MediaWiki\Extension\WikiLambda\ZObjects;
13
14use MediaWiki\Extension\WikiLambda\Registry\ZTypeRegistry;
15use MediaWiki\Extension\WikiLambda\ZObjectUtils;
16
17class ZTypedError extends ZObject {
18
19    /**
20     * Create a new typed error instance
21     *
22     * @param ZFunctionCall $functionCall
23     * @param ZObject[] $keys
24     */
25    public function __construct( $functionCall, $keys = [] ) {
26        $this->type = $functionCall;
27        $nextLocalKey = 1;
28
29        foreach ( $keys as $key => $value )    {
30            if ( ZObjectUtils::isValidZObjectKey( $key ) ) {
31                $this->data[ $key ] = $value;
32            } else {
33                $this->data[ "K$nextLocalKey" ] = $value;
34                $nextLocalKey += 1;
35            }
36        }
37    }
38
39    /**
40     * @inheritDoc
41     */
42    public static function getDefinition(): array {
43        return [
44            'type' => [
45                'type' => ZTypeRegistry::Z_FUNCTIONCALL,
46                'value' => ZTypeRegistry::Z_FUNCTION_ERRORTYPE_TO_TYPE,
47            ],
48            'keys' => [],
49            'additionalKeys' => true,
50        ];
51    }
52
53    /**
54     * Build the function call that defines the type of this Typed Error
55     *
56     * @param string $errorType
57     * @return ZFunctionCall
58     */
59    public static function buildType( $errorType ): ZFunctionCall {
60        return new ZFunctionCall(
61            new ZReference( ZTypeRegistry::Z_FUNCTION_ERRORTYPE_TO_TYPE ),
62            [ ZTypeRegistry::Z_FUNCTION_ERRORTYPE_TYPE => new ZReference( $errorType ) ]
63        );
64    }
65
66    /**
67     * @inheritDoc
68     */
69    public function isValid(): bool {
70        // To validate an instance of a typed error, we need to check
71        // the keys and types of the errortype object
72        return true;
73    }
74
75    /**
76     * @inheritDoc
77     */
78    public function getSerialized( $form = self::FORM_CANONICAL ) {
79        $serialized = parent::getSerialized( $form );
80        $serialized->{ ZTypeRegistry::Z_OBJECT_TYPE } = $this->type->getSerialized( $form );
81        return $serialized;
82    }
83
84    /**
85     * Returns the Zid of the errortype that represents this ZTypedError instance
86     *
87     * @return string The zid of the ZErrorType
88     */
89    public function getErrorType(): string {
90        return $this->type->getValueByKey( ZTypeRegistry::Z_FUNCTION_ERRORTYPE_TYPE )->getZValue();
91    }
92
93}