Debian Codegraph @

includes/libs/Message/DataMessageValue.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Message;
4
5 use Wikimedia\JsonCodec\JsonCodecableTrait;
6
7 /**
8 * Value object representing a message for i18n with alternative
9 * machine-readable data.
10 *
11 * This augments a MessageValue with an additional machine-readable code and
12 * structured data. The intended use is to facilitate error reporting in APIs.
13 *
14 * For example, a MessageValue reporting an "integer out of range" error might
15 * use one of three message keys, depending on whether there is a minimum, a
16 * maximum, or both. But an API would likely want to use one code for all three
17 * cases, and would likely want the endpoints represented along the lines of
18 * `[ 'min' => 1, 'max' => 10 ]` rather than
19 * `[ 0 => new ScalarParam( ParamType::TEXT, 1 ), 1 => new ScalarParam( ParamType::TEXT, 10 ) ]`.
20 *
21 * DataMessageValues are pure value objects and are newable and (de)serializable.
22 *
23 * @newable
24 */
25 class DataMessageValue extends MessageValue {
26 use JsonCodecableTrait;
27
28 /** @var string */
29 private $code;
30
31 /** @var array|null */
32 private $data;
33
34 /**
35 * @stable to call
36 *
37 * @param string $key
38 * @param (MessageParam|MessageValue|string|int|float)[] $params
39 * @param string|null $code String representing the concept behind
40 * this message.
41 * @param array|null $data Structured data representing the concept
42 * behind this message.
43 */
44 public function __construct( $key, $params = [], $code = null, ?array $data = null ) {
45 parent::__construct( $key, $params );
46
47 $this->code = $code ?? $key;
48 $this->data = $data;
49 }
50
51 /**
52 * Static constructor for easier chaining of `->params()` methods
53 * @param string $key
54 * @param (MessageParam|MessageValue|string|int|float)[] $params
55 * @param string|null $code
56 * @param array|null $data
57 * @return DataMessageValue
58 */
59 public static function new( $key, $params = [], $code = null, ?array $data = null ) {
60 return new DataMessageValue( $key, $params, $code, $data );
61 }
62
63 /**
64 * Get the message code
65 * @return string
66 */
67 public function getCode() {
68 return $this->code;
69 }
70
71 /**
72 * Get the message's structured data
73 * @return array|null
74 */
75 public function getData() {
76 return $this->data;
77 }
78
79 public function dump() {
80 $contents = '';
81 if ( $this->getParams() ) {
82 $contents = '<params>';
83 foreach ( $this->getParams() as $param ) {
84 $contents .= $param->dump();
85 }
86 $contents .= '</params>';
87 }
88
89 if ( $this->data !== null ) {
90 $contents .= '<data>' . htmlspecialchars( json_encode( $this->data ), ENT_NOQUOTES ) . '</data>';
91 }
92
93 return '<datamessage key="' . htmlspecialchars( $this->getKey() ) . '"'
94 . ' code="' . htmlspecialchars( $this->code ) . '">'
95 . $contents
96 . '</datamessage>';
97 }
98
99 public function toJsonArray(): array {
100 // WARNING: When changing how this class is serialized, follow the instructions
101 // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>!
102 return parent::toJsonArray() + [
103 'code' => $this->code,
104 'data' => $this->data,
105 ];
106 }
107
108 /** @inheritDoc */
109 public static function jsonClassHintFor( string $keyName ) {
110 // JsonStaticClassCodec invokes $className::jsonClassHintFor() so
111 // we need to have an explicit definition in DataMessageValue, we
112 // can't simply inherit the static method from MessageValue.
113 return parent::jsonClassHintFor( $keyName );
114 }
115
116 public static function newFromJsonArray( array $json ): DataMessageValue {
117 // WARNING: When changing how this class is serialized, follow the instructions
118 // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>!
119 return new self( $json['key'], $json['params'], $json['code'], $json['data'] );
120 }
121 }