| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\NodeData; |
| 5 |
|
| 6 |
use Wikimedia\Assert\Assert; |
| 7 |
use Wikimedia\JsonCodec\JsonCodecable; |
| 8 |
use Wikimedia\JsonCodec\JsonCodecableTrait; |
| 9 |
|
| 10 |
/** |
| 11 |
* Rich attribute data for a DOM Element. Both the key and the value |
| 12 |
* can be strings or more complex values. |
| 13 |
*/ |
| 14 |
class DataMwAttrib implements JsonCodecable { |
| 15 |
use JsonCodecableTrait; |
| 16 |
|
| 17 |
/** |
| 18 |
* The attribute name. |
| 19 |
* @var array{txt?:string,html?:string}|string |
| 20 |
*/ |
| 21 |
public $key; |
| 22 |
|
| 23 |
/** |
| 24 |
* The attribute value. |
| 25 |
* @var array|string |
| 26 |
*/ |
| 27 |
public $value; |
| 28 |
|
| 29 |
/** |
| 30 |
* @param array{txt?:string,html?:string}|string $key Attribute name |
| 31 |
* @param array|string $value Attribute value |
| 32 |
*/ |
| 33 |
public function __construct( $key, $value ) { |
| 34 |
$this->key = $key; |
| 35 |
$this->value = $value; |
| 36 |
} |
| 37 |
|
| 38 |
/** @inheritDoc */ |
| 39 |
public static function jsonClassHintFor( string $keyname ) { |
| 40 |
// No hints necessary, both $key and $value are arrays |
| 41 |
return null; |
| 42 |
} |
| 43 |
|
| 44 |
/** @inheritDoc */ |
| 45 |
public function toJsonArray(): array { |
| 46 |
return [ $this->key, $this->value ]; |
| 47 |
} |
| 48 |
|
| 49 |
/** @inheritDoc */ |
| 50 |
public static function newFromJsonArray( array $json ): DataMwAttrib { |
| 51 |
Assert::invariant( |
| 52 |
array_is_list( $json ) && count( $json ) === 2, |
| 53 |
"bad data-mw.attrib" |
| 54 |
); |
| 55 |
return new DataMwAttrib( $json[0], $json[1] ); |
| 56 |
} |
| 57 |
} |