/
NodePath.php
115 lines (99 loc) · 2.98 KB
/
NodePath.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
namespace Neos\ContentRepository\Domain\ContentSubgraph;
/*
* This file is part of the Neos.ContentRepository package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\ContentRepository\Domain\NodeAggregate\NodeName;
use Neos\Flow\Annotations as Flow;
/**
* The node path is a list of NodeNames. It can be either absolute or relative.
*
* It describes the hierarchy path of a node to a root node in a subgraph.
*
* @Flow\Proxy(false)
* @api
*/
final class NodePath implements \JsonSerializable
{
/**
* @var string
*/
private $path;
private function __construct(string $path)
{
if ($path !== '/') {
$pathParts = explode('/', ltrim($path, '/'));
foreach ($pathParts as $pathPart) {
if (preg_match(NodeInterface::MATCH_PATTERN_NAME, $pathPart) !== 1) {
throw new \InvalidArgumentException(sprintf('The path "%s" is no valid NodePath because it contains a segment "%s" that is no valid NodeName', $path, $pathPart), 1548157108);
}
}
}
$this->path = $path;
}
public static function fromString(string $path): self
{
return new static($path);
}
public static function fromPathSegments(array $pathSegments): self
{
if ($pathSegments === []) {
return new static('/');
}
return new static('/' . implode('/', $pathSegments));
}
public function isRoot(): bool
{
return $this->path === '/';
}
public function isAbsolute(): bool
{
return strpos($this->path, '/') === 0;
}
/**
* IMMUTABLE function to create a new NodePath by appending a path segment. Returns a NEW NodePath object
* @param NodeName $nodeName
* @return NodePath
*/
public function appendPathSegment(NodeName $nodeName): self
{
return new static($this->path . '/' . $nodeName);
}
/**
* @return NodeName[]
*/
public function getParts(): array
{
$pathParts = explode('/', ltrim($this->path, '/'));
return array_map(function (string $pathPart) {
return NodeName::fromString($pathPart);
}, $pathParts);
}
public function getDepth(): int
{
if (!$this->isAbsolute()) {
throw new \RuntimeException(sprintf('Depth of relative node path "%s" cannot be determined', $this->path), 1548162166);
}
return count($this->getParts());
}
public function equals(NodePath $other): bool
{
return (string) $this === (string) $other;
}
public function jsonSerialize(): string
{
return $this->path;
}
public function __toString()
{
return $this->path;
}
}