value is JSON-serializable, even if $value is not // (but don't do it when using ParamType::OBJECT, since those objects may not expect it) $value = MessageValue::newFromSpecifier( $value ); } elseif ( is_object( $value ) && $value instanceof Stringable ) { $value = (string)$value; } elseif ( !is_string( $value ) && !is_numeric( $value ) ) { $valType = get_debug_type( $value ); if ( $value === null || is_bool( $value ) ) { wfDeprecatedMsg( "Using $valType as message parameter was deprecated in MediaWiki 1.43", '1.43' ); $value = (string)$value; } else { throw new InvalidArgumentException( "Scalar parameter must be a string, number, Stringable, or MessageSpecifier; got $valType" ); } } $this->type = $type; $this->value = $value; } public function dump() { if ( $this->value instanceof MessageValue ) { $contents = $this->value->dump(); } else { $contents = htmlspecialchars( (string)$this->value ); } return "<{$this->type}>" . $contents . "type}>"; } public function toJsonArray(): array { // WARNING: When changing how this class is serialized, follow the instructions // at ! return [ $this->type => $this->value, ]; } /** @inheritDoc */ public static function jsonClassHintFor( string $keyName ) { // If this is not a scalar value (string|int|float) then it's // probably a MessageValue, and we can hint it as such to // reduce serialization overhead. return MessageValue::hint(); } public static function newFromJsonArray( array $json ): ScalarParam { // WARNING: When changing how this class is serialized, follow the instructions // at ! if ( count( $json ) !== 1 ) { throw new InvalidArgumentException( 'Invalid format' ); } // Use a dummy loop to get the first (and only) key/value pair in the array. foreach ( $json as $type => $value ) { return new self( $type, $value ); } } }