Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
74.19% |
23 / 31 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| ListParam | |
74.19% |
23 / 31 |
|
66.67% |
4 / 6 |
21.97 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
| getListType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| dump | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| isSameAs | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| toJsonArray | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| newFromJsonArray | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
4.59 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | |
| 21 | namespace Wikimedia\Message; |
| 22 | |
| 23 | use InvalidArgumentException; |
| 24 | use Wikimedia\JsonCodec\JsonCodecableTrait; |
| 25 | |
| 26 | /** |
| 27 | * Value object representing a message parameter that consists of a list of values. |
| 28 | * |
| 29 | * Message parameter classes are pure value objects and are newable and (de)serializable. |
| 30 | * |
| 31 | * @newable |
| 32 | */ |
| 33 | class ListParam extends MessageParam { |
| 34 | use JsonCodecableTrait; |
| 35 | |
| 36 | private string $listType; |
| 37 | |
| 38 | /** |
| 39 | * @stable to call. |
| 40 | * |
| 41 | * @param string $listType One of the ListType constants. |
| 42 | * @param (MessageParam|MessageSpecifier|string|int|float)[] $elements Values in the list. |
| 43 | * Values that are not instances of MessageParam are wrapped using ParamType::TEXT. |
| 44 | */ |
| 45 | public function __construct( string $listType, array $elements ) { |
| 46 | if ( !in_array( $listType, ListType::cases() ) ) { |
| 47 | throw new InvalidArgumentException( '$listType must be one of the ListType constants' ); |
| 48 | } |
| 49 | $this->type = ParamType::LIST; |
| 50 | $this->listType = $listType; |
| 51 | $this->value = []; |
| 52 | foreach ( $elements as $element ) { |
| 53 | if ( $element instanceof MessageParam ) { |
| 54 | $this->value[] = $element; |
| 55 | } else { |
| 56 | $this->value[] = new ScalarParam( ParamType::TEXT, $element ); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get the type of the list |
| 63 | * |
| 64 | * @return string One of the ListType constants |
| 65 | */ |
| 66 | public function getListType(): string { |
| 67 | return $this->listType; |
| 68 | } |
| 69 | |
| 70 | public function dump(): string { |
| 71 | $contents = ''; |
| 72 | foreach ( $this->value as $element ) { |
| 73 | $contents .= $element->dump(); |
| 74 | } |
| 75 | return "<$this->type listType=\"$this->listType\">$contents</$this->type>"; |
| 76 | } |
| 77 | |
| 78 | public function isSameAs( MessageParam $mp ): bool { |
| 79 | return $mp instanceof ListParam && |
| 80 | $this->listType === $mp->listType && |
| 81 | count( $this->value ) === count( $mp->value ) && |
| 82 | array_all( |
| 83 | $this->value, |
| 84 | static fn ( $v, $k ) => $v->isSameAs( $mp->value[$k] ) |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | public function toJsonArray(): array { |
| 89 | // WARNING: When changing how this class is serialized, follow the instructions |
| 90 | // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>! |
| 91 | return [ |
| 92 | $this->type => array_map( |
| 93 | /** |
| 94 | * Serialize trivial parameters as scalar values to minimize the footprint. Full |
| 95 | * round-trip compatibility is guaranteed via the constructor. |
| 96 | */ |
| 97 | static fn ( $p ) => $p->getType() === ParamType::TEXT ? $p->getValue() : $p, |
| 98 | $this->value |
| 99 | ), |
| 100 | 'type' => $this->listType, |
| 101 | ]; |
| 102 | } |
| 103 | |
| 104 | public static function newFromJsonArray( array $json ): ListParam { |
| 105 | // WARNING: When changing how this class is serialized, follow the instructions |
| 106 | // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>! |
| 107 | if ( count( $json ) !== 2 || !isset( $json[ParamType::LIST] ) || !isset( $json['type'] ) ) { |
| 108 | throw new InvalidArgumentException( 'Invalid format' ); |
| 109 | } |
| 110 | return new self( $json['type'], $json[ParamType::LIST] ); |
| 111 | } |
| 112 | } |