Passed
Push — master ( ed53b1...556216 )
by Thorsten
03:33
created

ValuePath   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 54.17%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 53
ccs 13
cts 24
cp 0.5417
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A push() 0 6 1
A reverse() 0 6 1
A count() 0 4 1
A getIterator() 0 4 1
A __toString() 0 7 2
A __clone() 0 4 1
A __construct() 0 8 1
1
<?php
2
/**
3
 * This file is part of the daikon-cqrs/entity project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
declare(strict_types=1);
10
11
namespace Daikon\Entity\Entity\Path;
12
13
use Ds\Vector;
14
15
final class ValuePath implements \IteratorAggregate, \Countable
16
{
17
    /**
18
     * @var Vector
19
     */
20
    private $internalVector;
21
22 5
    public function __construct(iterable $pathParts = null)
23
    {
24 5
        $this->internalVector = new Vector(
25 5
            (function (ValuePathPart ...$pathParts): array {
26 5
                return $pathParts;
27 5
            })(...$pathParts ?? [])
28
        );
29 5
    }
30
31
    public function push(ValuePathPart $pathPart): ValuePath
32
    {
33
        $clonedPath = clone $this;
34
        $clonedPath->internalVector->push($pathPart);
35
        return $clonedPath;
36
    }
37
38
    public function reverse(): ValuePath
39
    {
40
        $clonedPath = clone $this;
41
        $clonedPath->internalVector->reverse();
42
        return $clonedPath;
43
    }
44
45 3
    public function count(): int
46
    {
47 3
        return count($this->internalVector);
48
    }
49
50 2
    public function getIterator(): \Iterator
51
    {
52 2
        return $this->internalVector->getIterator();
53
    }
54
55
    public function __toString(): string
56
    {
57 3
        $flattenPath = function (string $path, ValuePathPart $pathPart): string {
58 3
            return empty($path) ? (string)$pathPart : sprintf('%s-%s', $path, $pathPart);
59
        };
60 3
        return $this->internalVector->reduce($flattenPath, '');
61
    }
62
63
    public function __clone()
64
    {
65
        $this->internalVector = new Vector($this->internalVector->toArray());
66
    }
67
}
68