Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 36 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
Params | |
0.00% |
0 / 36 |
|
0.00% |
0 / 4 |
182 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
dict | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
named | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
56 | |||
getSlice | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace Wikimedia\Parsoid\Wt2Html; |
5 | |
6 | use Wikimedia\Parsoid\Tokens\KV; |
7 | use Wikimedia\Parsoid\Utils\TokenUtils; |
8 | |
9 | /** |
10 | * A parameter object wrapper, essentially an array of key/value pairs with a few extra methods. |
11 | */ |
12 | class Params { |
13 | /** @var KV[] */ |
14 | public $args; |
15 | |
16 | /** @var ?array */ |
17 | public $argDict; |
18 | |
19 | /** @var ?array */ |
20 | public $namedArgsDict; |
21 | |
22 | /** |
23 | * @param KV[] $args |
24 | */ |
25 | public function __construct( array $args ) { |
26 | $this->args = $args; |
27 | $this->argDict = null; |
28 | $this->namedArgsDict = null; |
29 | } |
30 | |
31 | /** |
32 | * FIXME: Used in Parsoid native parser functions implementation |
33 | * and may not be needed in the final version. |
34 | * |
35 | * Converts the args that are an array of ($k, $v) KV objects to |
36 | * an assocative array mapping $k to $v. |
37 | * |
38 | * @return array |
39 | */ |
40 | public function dict(): array { |
41 | if ( $this->argDict === null ) { |
42 | $res = []; |
43 | foreach ( $this->args as $kv ) { |
44 | $key = trim( TokenUtils::tokensToString( $kv->k ) ); |
45 | $res[$key] = $kv->v; |
46 | } |
47 | $this->argDict = $res; |
48 | } |
49 | return $this->argDict; |
50 | } |
51 | |
52 | /** |
53 | * Converts the args that are an array of ($k, $v) KV objects to |
54 | * an assocative array mapping $k to $v while handling named and |
55 | * locational orgs and trimming whitespace around the keys. |
56 | * |
57 | * @return array |
58 | */ |
59 | public function named(): array { |
60 | if ( $this->namedArgsDict === null ) { |
61 | $n = 1; |
62 | $out = []; |
63 | $namedArgs = []; |
64 | |
65 | foreach ( $this->args as $kv ) { |
66 | // FIXME: Also check for whitespace-only named args! |
67 | $k = $kv->k; |
68 | $v = $kv->v; |
69 | if ( is_string( $k ) ) { |
70 | $k = trim( $k ); |
71 | } |
72 | if ( !is_array( $k ) && |
73 | // Check for blank named parameters |
74 | $kv->srcOffsets->key->end === $kv->srcOffsets->value->start |
75 | ) { |
76 | $out[(string)$n] = $v; |
77 | $n++; |
78 | } elseif ( is_string( $k ) ) { |
79 | $namedArgs[$k] = true; |
80 | $out[$k] = $v; |
81 | } else { |
82 | $k = trim( TokenUtils::tokensToString( $k ) ); |
83 | $namedArgs[$k] = true; |
84 | $out[$k] = $v; |
85 | } |
86 | } |
87 | $this->namedArgsDict = [ 'namedArgs' => $namedArgs, 'dict' => $out ]; |
88 | } |
89 | |
90 | return $this->namedArgsDict; |
91 | } |
92 | |
93 | /** |
94 | * FIXME: Used in Parsoid native parser functions implementation |
95 | * and may not be needed in the final version. |
96 | * |
97 | * @param int $start |
98 | * @param int $end |
99 | * @return array |
100 | */ |
101 | public function getSlice( int $start, int $end ): array { |
102 | $out = []; |
103 | $args = array_slice( $this->args, $start, $end /*CHECK THIS*/ ); |
104 | foreach ( $args as $kv ) { |
105 | $out[] = new KV( $kv->k, TokenUtils::tokensToString( $kv->v ), $kv->srcOffsets ); |
106 | } |
107 | return $out; |
108 | } |
109 | } |