1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
<?php
namespace Wikimedia\Message;
use InvalidArgumentException;
use MediaWiki\Json\JsonDeserializer;
/**
* Value object representing a message parameter that consists of a list of values.
*
* Message parameter classes are pure value objects and are newable and (de)serializable.
*
* @newable
*/
class ListParam extends MessageParam {
/** @var string */
private $listType;
/**
* @stable to call.
*
* @param string $listType One of the ListType constants.
* @param (MessageParam|MessageSpecifier|string|int|float)[] $elements Values in the list.
* Values that are not instances of MessageParam are wrapped using ParamType::TEXT.
*/
public function __construct( $listType, array $elements ) {
if ( !in_array( $listType, ListType::cases() ) ) {
throw new InvalidArgumentException( '$listType must be one of the ListType constants' );
}
$this->type = ParamType::LIST;
$this->listType = $listType;
$this->value = [];
foreach ( $elements as $element ) {
if ( $element instanceof MessageParam ) {
$this->value[] = $element;
} else {
$this->value[] = new ScalarParam( ParamType::TEXT, $element );
}
}
}
/**
* Get the type of the list
*
* @return string One of the ListType constants
*/
public function getListType() {
return $this->listType;
}
public function dump() {
$contents = '';
foreach ( $this->value as $element ) {
$contents .= $element->dump();
}
return "<{$this->type} listType=\"{$this->listType}\">$contents</{$this->type}>";
}
protected function toJsonArray(): array {
// WARNING: When changing how this class is serialized, follow the instructions
// at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>!
return [
$this->type => $this->value,
'type' => $this->listType,
];
}
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>!
if ( count( $json ) !== 2 || !isset( $json[ParamType::LIST] ) || !isset( $json['type'] ) ) {
throw new InvalidArgumentException( 'Invalid format' );
}
return new self( $json['type'], $json[ParamType::LIST] );
}
}
|