Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.24% covered (success)
95.24%
20 / 21
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ListParam
95.24% covered (success)
95.24%
20 / 21
80.00% covered (warning)
80.00%
4 / 5
12
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 getListType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 dump
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 toJsonArray
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 newFromJsonArray
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
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
21namespace Wikimedia\Message;
22
23use InvalidArgumentException;
24use 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 */
33class 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 toJsonArray(): array {
79        // WARNING: When changing how this class is serialized, follow the instructions
80        // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>!
81        return [
82            $this->type => $this->value,
83            'type' => $this->listType,
84        ];
85    }
86
87    public static function newFromJsonArray( array $json ): ListParam {
88        // WARNING: When changing how this class is serialized, follow the instructions
89        // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>!
90        if ( count( $json ) !== 2 || !isset( $json[ParamType::LIST] ) || !isset( $json['type'] ) ) {
91            throw new InvalidArgumentException( 'Invalid format' );
92        }
93        return new self( $json['type'], $json[ParamType::LIST] );
94    }
95}