Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 16 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| DataMwError | |
0.00% |
0 / 16 |
|
0.00% |
0 / 5 |
72 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| __clone | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| equals | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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 | public function equals( DataMwError $other ): bool { |
| 48 | // Use non-strict equality test, which will compare the properties |
| 49 | // and compare the values in the params array. |
| 50 | // @phan-suppress-next-line PhanPluginComparisonObjectEqualityNotStrict |
| 51 | return $this == $other; |
| 52 | } |
| 53 | |
| 54 | /** @inheritDoc */ |
| 55 | public function toJsonArray(): array { |
| 56 | $result = [ 'key' => $this->key ]; |
| 57 | if ( $this->message !== null ) { |
| 58 | $result['message'] = $this->message; |
| 59 | } |
| 60 | if ( $this->params !== [] ) { |
| 61 | $result['params'] = $this->params; |
| 62 | } |
| 63 | return $result; |
| 64 | } |
| 65 | |
| 66 | /** @inheritDoc */ |
| 67 | public static function newFromJsonArray( array $json ): DataMwError { |
| 68 | return new DataMwError( $json['key'], $json['params'] ?? [], $json['message'] ?? null ); |
| 69 | } |
| 70 | } |