Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.49% covered (warning)
80.49%
33 / 41
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ScalarParam
80.49% covered (warning)
80.49%
33 / 41
40.00% covered (danger)
40.00%
2 / 5
22.97
0.00% covered (danger)
0.00%
0 / 1
 __construct
95.65% covered (success)
95.65%
22 / 23
0.00% covered (danger)
0.00%
0 / 1
10
 dump
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 isSameAs
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
30
 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 ) && $value instanceof Stringable ) {
58            $value = (string)$value;
59        } elseif ( !is_string( $value ) && !is_numeric( $value ) ) {
60            $valType = get_debug_type( $value );
61            if ( $value === null || is_bool( $value ) ) {
62                trigger_error(
63                    "Using $valType as a message parameter was deprecated in MediaWiki 1.43",
64                    E_USER_DEPRECATED
65                );
66                $value = (string)$value;
67            } else {
68                throw new InvalidArgumentException(
69                    "Scalar parameter must be a string, number, Stringable, or MessageSpecifier; got $valType"
70                );
71            }
72        }
73
74        $this->type = $type;
75        $this->value = $value;
76    }
77
78    public function dump(): string {
79        if ( $this->value instanceof MessageValue ) {
80            $contents = $this->value->dump();
81        } else {
82            $contents = htmlspecialchars( (string)$this->value );
83        }
84        return "<$this->type>" . $contents . "</$this->type>";
85    }
86
87    public function isSameAs( MessageParam $mp ): bool {
88        if ( !( $mp instanceof ScalarParam && $this->type === $mp->type ) ) {
89            return false;
90        }
91        if ( $this->value instanceof MessageValue ) {
92            return $mp->value instanceof MessageValue &&
93                $this->value->isSameAs( $mp->value );
94        }
95        return $this->value === $mp->value;
96    }
97
98    public function toJsonArray(): array {
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        return [
102            $this->type => $this->value,
103        ];
104    }
105
106    public static function newFromJsonArray( array $json ): ScalarParam {
107        // WARNING: When changing how this class is serialized, follow the instructions
108        // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>!
109        if ( count( $json ) !== 1 ) {
110            throw new InvalidArgumentException( 'Invalid format' );
111        }
112
113        $type = key( $json );
114        $value = current( $json );
115
116        return new self( $type, $value );
117    }
118}