Debian Codegraph @

vendor/wikimedia/parsoid/src/NodeData/DataMwError.php

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