Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
KV | |
0.00% |
0 / 25 |
|
0.00% |
0 / 6 |
240 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
lookupKV | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
30 | |||
lookup | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
keyOffset | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
valueOffset | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
jsonSerialize | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 |
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 | * Lookup a string key in a KV array and return the first matching KV object |
58 | * |
59 | * @param KV[]|null $kvs |
60 | * @param string $key |
61 | * @return ?KV |
62 | */ |
63 | public static function lookupKV( ?array $kvs, string $key ): ?KV { |
64 | if ( $kvs === null ) { |
65 | return null; |
66 | } |
67 | |
68 | foreach ( $kvs as $kv ) { |
69 | // PORT-FIXME: JS trim() will remove non-ASCII spaces (such as NBSP) too, |
70 | // while PHP's won't. Does that matter? |
71 | if ( is_string( $kv->k ) && trim( $kv->k ) === $key ) { |
72 | return $kv; |
73 | } |
74 | } |
75 | |
76 | return null; |
77 | } |
78 | |
79 | /** |
80 | * Lookup a string key (first occurrence) in a KV array |
81 | * and return the value of the KV object |
82 | * |
83 | * @param KV[]|null $kvs |
84 | * @param string $key |
85 | * @return string|Token|Token[]|null |
86 | */ |
87 | public static function lookup( ?array $kvs, string $key ) { |
88 | $kv = self::lookupKV( $kvs, $key ); |
89 | // PORT_FIXME: Potential bug lurking here ... if $kv->v is an array |
90 | // this will return a copy, which if modified will not reflect |
91 | // in the original KV object. |
92 | return $kv->v ?? null; |
93 | } |
94 | |
95 | /** |
96 | * Return the key portion of the KV's source offsets, or else null |
97 | * if no source offsets are known. |
98 | * @return SourceRange|null |
99 | */ |
100 | public function keyOffset(): ?SourceRange { |
101 | return $this->srcOffsets->key ?? null; |
102 | } |
103 | |
104 | /** |
105 | * Return the value portion of the KV's source offsets, or else null |
106 | * if no source offsets are known. |
107 | * @return SourceRange|null |
108 | */ |
109 | public function valueOffset(): ?SourceRange { |
110 | return $this->srcOffsets->value ?? null; |
111 | } |
112 | |
113 | /** |
114 | * @inheritDoc |
115 | */ |
116 | public function jsonSerialize(): array { |
117 | $ret = [ "k" => $this->k, "v" => $this->v ]; |
118 | if ( $this->srcOffsets ) { |
119 | $ret["srcOffsets"] = $this->srcOffsets; |
120 | } |
121 | if ( isset( $this->ksrc ) ) { |
122 | $ret["ksrc"] = $this->ksrc; |
123 | } |
124 | if ( isset( $this->vsrc ) ) { |
125 | $ret["vsrc"] = $this->vsrc; |
126 | } |
127 | return $ret; |
128 | } |
129 | } |