| 1 |
<?php |
| 2 |
// phpcs:disable MediaWiki.Commenting.FunctionComment.DefaultNullTypeParam -- T218324, T218816 |
| 3 |
declare( strict_types = 1 ); |
| 4 |
|
| 5 |
namespace Wikimedia\Parsoid\Tokens; |
| 6 |
|
| 7 |
/** |
| 8 |
* Represents a Key-value pair. |
| 9 |
*/ |
| 10 |
class KV implements \JsonSerializable { |
| 11 |
/** |
| 12 |
* Commonly a string, but where the key might be templated, |
| 13 |
* this can be an array of tokens even. |
| 14 |
* |
| 15 |
* @var string|Token|array<Token|string> |
| 16 |
*/ |
| 17 |
public $k; |
| 18 |
|
| 19 |
/** @var string|Token|array<Token|string>|KV[] */ |
| 20 |
public $v; |
| 21 |
|
| 22 |
/** @var KVSourceRange|null wikitext source offsets */ |
| 23 |
public $srcOffsets; |
| 24 |
|
| 25 |
/** @var string|null wikitext source */ |
| 26 |
public $ksrc; |
| 27 |
|
| 28 |
/** @var string|null wikitext source */ |
| 29 |
public $vsrc; |
| 30 |
|
| 31 |
/** |
| 32 |
* @param string|Token|array<Token|string> $k |
| 33 |
* Commonly a string, but where the key might be templated, |
| 34 |
* this can be an array of tokens even. |
| 35 |
* @param string|Token|array<Token|string>|KV[] $v |
| 36 |
* The value: string, token, of an array of tokens |
| 37 |
* @param ?KVSourceRange $srcOffsets wikitext source offsets |
| 38 |
* @param ?string $ksrc |
| 39 |
* @param ?string $vsrc |
| 40 |
*/ |
| 41 |
public function __construct( |
| 42 |
$k, $v, ?KVSourceRange $srcOffsets = null, ?string $ksrc = null, |
| 43 |
?string $vsrc = null |
| 44 |
) { |
| 45 |
$this->k = $k; |
| 46 |
$this->v = $v; |
| 47 |
$this->srcOffsets = $srcOffsets; |
| 48 |
if ( isset( $ksrc ) ) { |
| 49 |
$this->ksrc = $ksrc; |
| 50 |
} |
| 51 |
if ( isset( $vsrc ) ) { |
| 52 |
$this->vsrc = $vsrc; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* BUG: When there are multiple matching attributes, Sanitizer lets the last one win |
| 58 |
* whereas this method is letting the first one win. This can introduce subtle bugs! |
| 59 |
* |
| 60 |
* Lookup a string key in a KV array and return the first matching KV object |
| 61 |
* |
| 62 |
* @param KV[]|null $kvs |
| 63 |
* @param string $key |
| 64 |
* @return ?KV |
| 65 |
*/ |
| 66 |
public static function lookupKV( ?array $kvs, string $key ): ?KV { |
| 67 |
if ( $kvs === null ) { |
| 68 |
return null; |
| 69 |
} |
| 70 |
|
| 71 |
foreach ( $kvs as $kv ) { |
| 72 |
// PORT-FIXME: JS trim() will remove non-ASCII spaces (such as NBSP) too, |
| 73 |
// while PHP's won't. Does that matter? |
| 74 |
if ( is_string( $kv->k ) && trim( $kv->k ) === $key ) { |
| 75 |
return $kv; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return null; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Lookup a string key (first occurrence) in a KV array |
| 84 |
* and return the value of the KV object |
| 85 |
* |
| 86 |
* @param KV[]|null $kvs |
| 87 |
* @param string $key |
| 88 |
* @return string|Token|array<Token|string>|null |
| 89 |
*/ |
| 90 |
public static function lookup( ?array $kvs, string $key ) { |
| 91 |
$kv = self::lookupKV( $kvs, $key ); |
| 92 |
// PORT_FIXME: Potential bug lurking here ... if $kv->v is an array |
| 93 |
// this will return a copy, which if modified will not reflect |
| 94 |
// in the original KV object. |
| 95 |
return $kv->v ?? null; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Return the key portion of the KV's source offsets, or else null |
| 100 |
* if no source offsets are known. |
| 101 |
* @return SourceRange|null |
| 102 |
*/ |
| 103 |
public function keyOffset(): ?SourceRange { |
| 104 |
return $this->srcOffsets->key ?? null; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Return the value portion of the KV's source offsets, or else null |
| 109 |
* if no source offsets are known. |
| 110 |
* @return SourceRange|null |
| 111 |
*/ |
| 112 |
public function valueOffset(): ?SourceRange { |
| 113 |
return $this->srcOffsets->value ?? null; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* @inheritDoc |
| 118 |
*/ |
| 119 |
public function jsonSerialize(): array { |
| 120 |
$ret = [ "k" => $this->k, "v" => $this->v ]; |
| 121 |
if ( $this->srcOffsets ) { |
| 122 |
$ret["srcOffsets"] = $this->srcOffsets; |
| 123 |
} |
| 124 |
if ( isset( $this->ksrc ) ) { |
| 125 |
$ret["ksrc"] = $this->ksrc; |
| 126 |
} |
| 127 |
if ( isset( $this->vsrc ) ) { |
| 128 |
$ret["vsrc"] = $this->vsrc; |
| 129 |
} |
| 130 |
return $ret; |
| 131 |
} |
| 132 |
} |