2019-09-11 17:05:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Wikimedia\Message;
|
|
|
|
|
|
2024-04-02 21:56:24 +00:00
|
|
|
use MediaWiki\Json\JsonDeserializer;
|
|
|
|
|
|
2019-09-11 17:05:47 +00:00
|
|
|
/**
|
|
|
|
|
* Value object representing a message for i18n with alternative
|
|
|
|
|
* machine-readable data.
|
|
|
|
|
*
|
|
|
|
|
* This augments a MessageValue with an additional machine-readable code and
|
|
|
|
|
* structured data. The intended use is to facilitate error reporting in APIs.
|
|
|
|
|
*
|
|
|
|
|
* For example, a MessageValue reporting an "integer out of range" error might
|
|
|
|
|
* use one of three message keys, depending on whether there is a minimum, a
|
|
|
|
|
* maximum, or both. But an API would likely want to use one code for all three
|
|
|
|
|
* cases, and would likely want the endpoints represented along the lines of
|
|
|
|
|
* `[ 'min' => 1, 'max' => 10 ]` rather than
|
|
|
|
|
* `[ 0 => new ScalarParam( ParamType::TEXT, 1 ), 1 => new ScalarParam( ParamType::TEXT, 10 ) ]`.
|
|
|
|
|
*
|
2024-04-02 21:56:24 +00:00
|
|
|
* DataMessageValues are pure value objects and are newable and (de)serializable.
|
2020-06-26 12:56:03 +00:00
|
|
|
*
|
|
|
|
|
* @newable
|
2019-09-11 17:05:47 +00:00
|
|
|
*/
|
|
|
|
|
class DataMessageValue extends MessageValue {
|
|
|
|
|
/** @var string */
|
|
|
|
|
private $code;
|
|
|
|
|
|
|
|
|
|
/** @var array|null */
|
|
|
|
|
private $data;
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-13 08:53:06 +00:00
|
|
|
* @stable to call
|
2020-06-26 12:56:03 +00:00
|
|
|
*
|
2019-09-11 17:05:47 +00:00
|
|
|
* @param string $key
|
|
|
|
|
* @param (MessageParam|MessageValue|string|int|float)[] $params
|
|
|
|
|
* @param string|null $code String representing the concept behind
|
|
|
|
|
* this message.
|
|
|
|
|
* @param array|null $data Structured data representing the concept
|
|
|
|
|
* behind this message.
|
|
|
|
|
*/
|
2024-10-16 18:58:33 +00:00
|
|
|
public function __construct( $key, $params = [], $code = null, ?array $data = null ) {
|
2019-09-11 17:05:47 +00:00
|
|
|
parent::__construct( $key, $params );
|
|
|
|
|
|
|
|
|
|
$this->code = $code ?? $key;
|
|
|
|
|
$this->data = $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Static constructor for easier chaining of `->params()` methods
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @param (MessageParam|MessageValue|string|int|float)[] $params
|
|
|
|
|
* @param string|null $code
|
|
|
|
|
* @param array|null $data
|
|
|
|
|
* @return DataMessageValue
|
|
|
|
|
*/
|
2024-10-16 18:58:33 +00:00
|
|
|
public static function new( $key, $params = [], $code = null, ?array $data = null ) {
|
2019-09-11 17:05:47 +00:00
|
|
|
return new DataMessageValue( $key, $params, $code, $data );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the message code
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getCode() {
|
|
|
|
|
return $this->code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the message's structured data
|
|
|
|
|
* @return array|null
|
|
|
|
|
*/
|
|
|
|
|
public function getData() {
|
|
|
|
|
return $this->data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function dump() {
|
|
|
|
|
$contents = '';
|
|
|
|
|
if ( $this->getParams() ) {
|
|
|
|
|
$contents = '<params>';
|
|
|
|
|
foreach ( $this->getParams() as $param ) {
|
|
|
|
|
$contents .= $param->dump();
|
|
|
|
|
}
|
|
|
|
|
$contents .= '</params>';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $this->data !== null ) {
|
|
|
|
|
$contents .= '<data>' . htmlspecialchars( json_encode( $this->data ), ENT_NOQUOTES ) . '</data>';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '<datamessage key="' . htmlspecialchars( $this->getKey() ) . '"'
|
|
|
|
|
. ' code="' . htmlspecialchars( $this->code ) . '">'
|
|
|
|
|
. $contents
|
|
|
|
|
. '</datamessage>';
|
|
|
|
|
}
|
2024-04-02 21:56:24 +00:00
|
|
|
|
2024-08-21 17:23:17 +00:00
|
|
|
protected function toJsonArray(): array {
|
2024-04-02 21:56:24 +00:00
|
|
|
// WARNING: When changing how this class is serialized, follow the instructions
|
|
|
|
|
// at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>!
|
|
|
|
|
return parent::toJsonArray() + [
|
|
|
|
|
'code' => $this->code,
|
|
|
|
|
'data' => $this->data,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function newFromJsonArray( JsonDeserializer $deserializer, array $json ) {
|
|
|
|
|
// WARNING: When changing how this class is serialized, follow the instructions
|
|
|
|
|
// at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>!
|
|
|
|
|
return new self( $json['key'], $json['params'], $json['code'], $json['data'] );
|
|
|
|
|
}
|
2019-09-11 17:05:47 +00:00
|
|
|
}
|