Path::copyParent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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