Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
KVSourceRange | |
0.00% |
0 / 21 |
|
0.00% |
0 / 5 |
30 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
__clone | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
offset | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
newFromJsonArray | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
toJsonArray | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace Wikimedia\Parsoid\Tokens; |
5 | |
6 | use Wikimedia\Assert\Assert; |
7 | use Wikimedia\JsonCodec\JsonCodecable; |
8 | use Wikimedia\JsonCodec\JsonCodecableTrait; |
9 | |
10 | /** |
11 | * Represents a source offset range for a key-value pair. |
12 | */ |
13 | class KVSourceRange implements JsonCodecable { |
14 | use JsonCodecableTrait; |
15 | |
16 | /** |
17 | * Source range for the key. |
18 | */ |
19 | public SourceRange $key; |
20 | |
21 | /** |
22 | * Source range for the value. |
23 | */ |
24 | public SourceRange $value; |
25 | |
26 | /** |
27 | * Create a new key-value source offset range. |
28 | * @param int $keyStart The start index of the key |
29 | * (unicode code points, inclusive) |
30 | * @param int $keyEnd The end index of the key |
31 | * (unicode code points, exclusive) |
32 | * @param int $valueStart The start index of the value |
33 | * (unicode code points, inclusive) |
34 | * @param int $valueEnd The end index of the value |
35 | * (unicode code points, exclusive) |
36 | */ |
37 | public function __construct( int $keyStart, int $keyEnd, int $valueStart, int $valueEnd ) { |
38 | $this->key = new SourceRange( $keyStart, $keyEnd ); |
39 | $this->value = new SourceRange( $valueStart, $valueEnd ); |
40 | } |
41 | |
42 | public function __clone() { |
43 | $this->key = clone $this->key; |
44 | $this->value = clone $this->value; |
45 | } |
46 | |
47 | /** |
48 | * Return a new key-value source offset range shifted by $amount. |
49 | * @param int $amount The amount to shift by |
50 | * @return KVSourceRange |
51 | */ |
52 | public function offset( int $amount ): KVSourceRange { |
53 | return new KVSourceRange( |
54 | $this->key->start + $amount, |
55 | $this->key->end + $amount, |
56 | $this->value->start + $amount, |
57 | $this->value->end + $amount |
58 | ); |
59 | } |
60 | |
61 | /** |
62 | * Create a new key-value source offset range from an array of |
63 | * integers (such as created during JSON serialization). |
64 | * @param int[] $so |
65 | * @return KVSourceRange |
66 | */ |
67 | public static function newFromJsonArray( array $so ): KVSourceRange { |
68 | Assert::invariant( |
69 | count( $so ) === 4, |
70 | 'Not enough elements in KVSourceRange array' |
71 | ); |
72 | return new KVSourceRange( $so[0], $so[1], $so[2], $so[3] ); |
73 | } |
74 | |
75 | /** |
76 | * @inheritDoc |
77 | */ |
78 | public function toJsonArray(): array { |
79 | return [ |
80 | $this->key->start, |
81 | $this->key->end, |
82 | $this->value->start, |
83 | $this->value->end, |
84 | ]; |
85 | } |
86 | } |