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