Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.44% covered (success)
94.44%
34 / 36
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ScalarParam
94.44% covered (success)
94.44%
34 / 36
50.00% covered (danger)
50.00%
2 / 4
16.04
0.00% covered (danger)
0.00%
0 / 1
 __construct
95.83% covered (success)
95.83%
23 / 24
0.00% covered (danger)
0.00%
0 / 1
11
 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%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 newFromJsonArray
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
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 Stringable;
25use Wikimedia\JsonCodec\JsonCodecableTrait;
26
27/**
28 * Value object representing a message parameter holding a single value.
29 *
30 * Message parameter classes are pure value objects and are safely newable.
31 *
32 * @newable
33 */
34class ScalarParam extends MessageParam {
35    use JsonCodecableTrait;
36
37    /**
38     * Construct a text parameter
39     *
40     * @stable to call.
41     *
42     * @param string $type One of the ParamType constants.
43     * @param string|int|float|MessageSpecifier|Stringable $value
44     */
45    public function __construct( string $type, $value ) {
46        if ( !in_array( $type, ParamType::cases() ) ) {
47            throw new InvalidArgumentException( '$type must be one of the ParamType constants' );
48        }
49        if ( $type === ParamType::LIST ) {
50            throw new InvalidArgumentException(
51                'ParamType::LIST cannot be used with ScalarParam; use ListParam instead'
52            );
53        }
54        if ( $value instanceof MessageSpecifier ) {
55            // Ensure that $this->value is JSON-serializable, even if $value is not
56            $value = MessageValue::newFromSpecifier( $value );
57        } elseif ( is_object( $value ) && (
58            $value instanceof Stringable || is_callable( [ $value, '__toString' ] )
59        ) ) {
60            // TODO: Remove separate '__toString' check above once we drop PHP 7.4
61            $value = (string)$value;
62        } elseif ( !is_string( $value ) && !is_numeric( $value ) ) {
63            $valType = get_debug_type( $value );
64            if ( $value === null || is_bool( $value ) ) {
65                trigger_error(
66                    "Using $valType as a message parameter was deprecated in MediaWiki 1.43",
67                    E_USER_DEPRECATED
68                );
69                $value = (string)$value;
70            } else {
71                throw new InvalidArgumentException(
72                    "Scalar parameter must be a string, number, Stringable, or MessageSpecifier; got $valType"
73                );
74            }
75        }
76
77        $this->type = $type;
78        $this->value = $value;
79    }
80
81    public function dump(): string {
82        if ( $this->value instanceof MessageValue ) {
83            $contents = $this->value->dump();
84        } else {
85            $contents = htmlspecialchars( (string)$this->value );
86        }
87        return "<$this->type>" . $contents . "</$this->type>";
88    }
89
90    public function toJsonArray(): array {
91        // WARNING: When changing how this class is serialized, follow the instructions
92        // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>!
93        return [
94            $this->type => $this->value,
95        ];
96    }
97
98    public static function newFromJsonArray( array $json ): ScalarParam {
99        // WARNING: When changing how this class is serialized, follow the instructions
100        // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>!
101        if ( count( $json ) !== 1 ) {
102            throw new InvalidArgumentException( 'Invalid format' );
103        }
104
105        $type = key( $json );
106        $value = current( $json );
107
108        return new self( $type, $value );
109    }
110}