Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
81.40% |
35 / 43 |
|
50.00% |
3 / 6 |
CRAP | |
0.00% |
0 / 1 |
| ScalarParam | |
81.40% |
35 / 43 |
|
50.00% |
3 / 6 |
23.84 | |
0.00% |
0 / 1 |
| __construct | |
95.83% |
23 / 24 |
|
0.00% |
0 / 1 |
10 | |||
| dump | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| isSameAs | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
30 | |||
| toJsonArray | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| jsonClassHintFor | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| newFromJsonArray | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Wikimedia\Message; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | use Stringable; |
| 7 | |
| 8 | /** |
| 9 | * Value object representing a message parameter holding a single value. |
| 10 | * |
| 11 | * Message parameter classes are pure value objects and are safely newable. |
| 12 | * |
| 13 | * @newable |
| 14 | */ |
| 15 | class ScalarParam extends MessageParam { |
| 16 | |
| 17 | /** |
| 18 | * Construct a text parameter |
| 19 | * |
| 20 | * @stable to call. |
| 21 | * |
| 22 | * @param string|ParamType $type One of the ParamType constants. |
| 23 | * @param string|int|float|MessageSpecifier|Stringable $value |
| 24 | */ |
| 25 | public function __construct( string|ParamType $type, $value ) { |
| 26 | if ( is_string( $type ) ) { |
| 27 | wfDeprecated( __METHOD__ . ' with string type', '1.45' ); |
| 28 | $type = ParamType::from( $type ); |
| 29 | } |
| 30 | if ( $type === ParamType::LIST ) { |
| 31 | throw new InvalidArgumentException( |
| 32 | 'ParamType::LIST cannot be used with ScalarParam; use ListParam instead' |
| 33 | ); |
| 34 | } |
| 35 | if ( $value instanceof MessageSpecifier ) { |
| 36 | // Ensure that $this->value is JSON-serializable, even if $value is not |
| 37 | $value = MessageValue::newFromSpecifier( $value ); |
| 38 | } elseif ( is_object( $value ) && $value instanceof Stringable ) { |
| 39 | $value = (string)$value; |
| 40 | } elseif ( !is_string( $value ) && !is_numeric( $value ) ) { |
| 41 | $valType = get_debug_type( $value ); |
| 42 | if ( $value === null || is_bool( $value ) ) { |
| 43 | trigger_error( |
| 44 | "Using $valType as a message parameter was deprecated in MediaWiki 1.43", |
| 45 | E_USER_DEPRECATED |
| 46 | ); |
| 47 | $value = (string)$value; |
| 48 | } else { |
| 49 | throw new InvalidArgumentException( |
| 50 | "Scalar parameter must be a string, number, Stringable, or MessageSpecifier; got $valType" |
| 51 | ); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | $this->type = $type; |
| 56 | $this->value = $value; |
| 57 | } |
| 58 | |
| 59 | public function dump(): string { |
| 60 | if ( $this->value instanceof MessageValue ) { |
| 61 | $contents = $this->value->dump(); |
| 62 | } else { |
| 63 | $contents = htmlspecialchars( (string)$this->value ); |
| 64 | } |
| 65 | return "<{$this->type->value}>" . $contents . "</{$this->type->value}>"; |
| 66 | } |
| 67 | |
| 68 | public function isSameAs( MessageParam $mp ): bool { |
| 69 | if ( !( $mp instanceof ScalarParam && $this->type === $mp->type ) ) { |
| 70 | return false; |
| 71 | } |
| 72 | if ( $this->value instanceof MessageValue ) { |
| 73 | return $mp->value instanceof MessageValue && |
| 74 | $this->value->isSameAs( $mp->value ); |
| 75 | } |
| 76 | return $this->value === $mp->value; |
| 77 | } |
| 78 | |
| 79 | public function toJsonArray(): array { |
| 80 | // WARNING: When changing how this class is serialized, follow the instructions |
| 81 | // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>! |
| 82 | return [ |
| 83 | $this->type->value => $this->value, |
| 84 | ]; |
| 85 | } |
| 86 | |
| 87 | /** @inheritDoc */ |
| 88 | public static function jsonClassHintFor( string $keyName ) { |
| 89 | // If this is not a scalar value (string|int|float) then it's |
| 90 | // probably a MessageValue, and we can hint it as such to |
| 91 | // reduce serialization overhead. |
| 92 | return MessageValue::hint(); |
| 93 | } |
| 94 | |
| 95 | public static function newFromJsonArray( array $json ): ScalarParam { |
| 96 | // WARNING: When changing how this class is serialized, follow the instructions |
| 97 | // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>! |
| 98 | if ( count( $json ) !== 1 ) { |
| 99 | throw new InvalidArgumentException( 'Invalid format' ); |
| 100 | } |
| 101 | |
| 102 | $type = key( $json ); |
| 103 | $value = current( $json ); |
| 104 | |
| 105 | return new self( ParamType::from( $type ), $value ); |
| 106 | } |
| 107 | } |