|
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
|
|
|
|