Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.66% covered (warning)
89.66%
26 / 29
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ScalarParam
89.66% covered (warning)
89.66%
26 / 29
50.00% covered (danger)
50.00%
2 / 4
14.22
0.00% covered (danger)
0.00%
0 / 1
 __construct
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 1
8.09
 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
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
1<?php
2
3namespace Wikimedia\Message;
4
5use InvalidArgumentException;
6use MediaWiki\Json\JsonDeserializer;
7use Stringable;
8
9/**
10 * Value object representing a message parameter holding a single value.
11 *
12 * Message parameter classes are pure value objects and are safely newable.
13 *
14 * When using the deprecated ParamType::OBJECT, the parameter value
15 * should be (de)serializable, otherwise (de)serialization of the
16 * ScalarParam object will fail.
17 *
18 * @newable
19 */
20class ScalarParam extends MessageParam {
21    /**
22     * Construct a text parameter
23     *
24     * @stable to call.
25     *
26     * @param string $type One of the ParamType constants.
27     *   Using ParamType::OBJECT is deprecated since 1.43.
28     * @param string|int|float|MessageValue|Stringable $value
29     */
30    public function __construct( $type, $value ) {
31        if ( !in_array( $type, ParamType::cases() ) ) {
32            throw new InvalidArgumentException( '$type must be one of the ParamType constants' );
33        }
34        if ( $type === ParamType::LIST ) {
35            throw new InvalidArgumentException(
36                'ParamType::LIST cannot be used with ScalarParam; use ListParam instead'
37            );
38        }
39        if ( $type === ParamType::OBJECT ) {
40            wfDeprecatedMsg( 'Using ParamType::OBJECT was deprecated in MediaWiki 1.43', '1.43' );
41        } elseif ( $value instanceof Stringable ) {
42            // Stringify the stringable to ensure that $this->value is JSON-serializable
43            // (but don't do it when using ParamType::OBJECT, since those objects may not expect it)
44            $value = (string)$value;
45        } elseif ( !is_string( $value ) && !is_numeric( $value ) &&
46            !$value instanceof MessageValue ) {
47            $type = get_debug_type( $value );
48            throw new InvalidArgumentException(
49                "Scalar parameter must be a string, number, or MessageValue; got $type"
50            );
51        }
52
53        $this->type = $type;
54        $this->value = $value;
55    }
56
57    public function dump() {
58        if ( $this->value instanceof MessageValue ) {
59            $contents = $this->value->dump();
60        } else {
61            $contents = htmlspecialchars( (string)$this->value );
62        }
63        return "<{$this->type}>" . $contents . "</{$this->type}>";
64    }
65
66    protected function toJsonArray(): array {
67        // WARNING: When changing how this class is serialized, follow the instructions
68        // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>!
69        return [
70            $this->type => $this->value,
71        ];
72    }
73
74    public static function newFromJsonArray( JsonDeserializer $deserializer, array $json ) {
75        // WARNING: When changing how this class is serialized, follow the instructions
76        // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>!
77        if ( count( $json ) !== 1 ) {
78            throw new InvalidArgumentException( 'Invalid format' );
79        }
80        // Use a dummy loop to get the first (and only) key/value pair in the array.
81        foreach ( $json as $type => $value ) {
82            return new self( $type, $value );
83        }
84    }
85}