Completed
Push — master ( c16c79...ab8250 )
by Edward
04:36
created

Path::contains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Data\Path;
5
6
use function array_slice;
7
use function count;
8
9
final class Path implements PathInterface
10
{
11
12
    private $elements;
13
14 9
    public function __construct(...$elements)
15
    {
16 9
        $this->elements = $elements;
17 9
    }
18
19 2
    public function copyWithElement(int $index): PathInterface
20
    {
21 2
        return new self(...$this->elements, ...[$index]);
22
    }
23
24 2
    public function copyWithProperty(string $name): PathInterface
25
    {
26 2
        return new self(...$this->elements, ...[$name]);
27
    }
28
29
    public function copyParent(): PathInterface
30
    {
31
        if (empty($this->elements)) {
32
            throw new Exception\ParentNotFoundException($this);
33
        }
34
35
        return new self(...array_slice($this->elements, 0, -1));
36
    }
37
38 7
    public function getElements(): array
39
    {
40 7
        return $this->elements;
41
    }
42
43 4
    public function equals(PathInterface $path): bool
44
    {
45 4
        return $path->getElements() === $this->elements;
46
    }
47
48
    public function contains(PathInterface $path): bool
49
    {
50
        $subPath = array_slice($path->getElements(), 0, count($this->elements));
51
52
        return $subPath === $this->elements;
53
    }
54
}
55