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