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