Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
TemplateDataStatus
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
7
100.00% covered (success)
100.00%
1 / 1
 jsonSerialize
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 newFromJson
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3namespace MediaWiki\Extension\TemplateData;
4
5use StatusValue;
6
7/**
8 * @license GPL-2.0-or-later
9 */
10class TemplateDataStatus {
11
12    /**
13     * @return array contains StatusValue ok and errors fields (does not serialize value)
14     */
15    public static function jsonSerialize( StatusValue $status ): array {
16        if ( $status->isOK() ) {
17            return [ 'ok' => true ];
18        }
19
20        [ $errorsOnlyStatus, $warningsOnlyStatus ] = $status->splitByErrorType();
21        // note that non-scalar values are not supported in errors or warnings
22        return [
23            'ok' => false,
24            'errors' => $errorsOnlyStatus->getErrors(),
25            'warnings' => $warningsOnlyStatus->getErrors()
26        ];
27    }
28
29    /**
30     * @param StatusValue|array|null $json contains StatusValue ok and errors fields (does not serialize value)
31     * @return StatusValue|null
32     */
33    public static function newFromJson( $json ): ?StatusValue {
34        if ( !is_array( $json ) ) {
35            return $json;
36        }
37
38        if ( $json['ok'] ) {
39            return StatusValue::newGood();
40        }
41
42        $status = new StatusValue();
43        foreach ( $json['errors'] as $error ) {
44            $status->fatal( $error['message'], ...$error['params'] );
45        }
46        foreach ( $json['warnings'] as $warning ) {
47            $status->warning( $warning['message'], ...$warning['params'] );
48        }
49        return $status;
50    }
51
52}