Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 15 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
DataMwError | |
0.00% |
0 / 15 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
__clone | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
toJsonArray | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
newFromJsonArray | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace Wikimedia\Parsoid\NodeData; |
5 | |
6 | use Wikimedia\JsonCodec\JsonCodec; |
7 | use Wikimedia\JsonCodec\JsonCodecable; |
8 | use Wikimedia\JsonCodec\JsonCodecableTrait; |
9 | |
10 | /** |
11 | * Localizable errors, stored in data-mw. |
12 | */ |
13 | class DataMwError implements JsonCodecable { |
14 | use JsonCodecableTrait; |
15 | |
16 | /** |
17 | * The error name, as a localizable key. |
18 | */ |
19 | public string $key; |
20 | |
21 | /** |
22 | * An unlocalized version of the error name, as a fallback. |
23 | */ |
24 | public ?string $message; |
25 | |
26 | /** |
27 | * Optional parameters for the error message. |
28 | * These should all be codecable. |
29 | */ |
30 | public array $params; |
31 | |
32 | public function __construct( string $key, array $params = [], ?string $message = null ) { |
33 | $this->key = $key; |
34 | $this->params = $params; |
35 | $this->message = $message; |
36 | } |
37 | |
38 | public function __clone() { |
39 | if ( $this->params ) { |
40 | $codec = new JsonCodec; |
41 | $this->params = $codec->newFromJsonArray( |
42 | $codec->toJsonArray( $this->params ) |
43 | ); |
44 | } |
45 | } |
46 | |
47 | /** @inheritDoc */ |
48 | public function toJsonArray(): array { |
49 | $result = [ 'key' => $this->key ]; |
50 | if ( $this->message !== null ) { |
51 | $result['message'] = $this->message; |
52 | } |
53 | if ( $this->params !== [] ) { |
54 | $result['params'] = $this->params; |
55 | } |
56 | return $result; |
57 | } |
58 | |
59 | /** @inheritDoc */ |
60 | public static function newFromJsonArray( array $json ): DataMwError { |
61 | return new DataMwError( $json['key'], $json['params'] ?? [], $json['message'] ?? null ); |
62 | } |
63 | } |