1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/config 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
|
|
|
namespace Daikon\Config; |
10
|
|
|
|
11
|
|
|
use Daikon\Interop\Assert; |
12
|
|
|
use Daikon\Interop\Assertion; |
13
|
|
|
|
14
|
|
|
final class ConfigPath implements ConfigPathInterface |
15
|
|
|
{ |
16
|
|
|
private const PATH_SEP = '.'; |
17
|
|
|
|
18
|
|
|
private string $scope; |
19
|
|
|
|
20
|
|
|
private array $parts; |
21
|
|
|
|
22
|
|
|
private string $separator; |
23
|
|
|
|
24
|
14 |
|
public static function fromString(string $path, string $separator = self::PATH_SEP): self |
25
|
|
|
{ |
26
|
14 |
|
$path = trim($path); |
27
|
14 |
|
$separator = trim($separator); |
28
|
14 |
|
Assert::that($separator) |
29
|
14 |
|
->length(1, 'Path separator must be exactly one char long.') |
30
|
14 |
|
->satisfy( |
31
|
14 |
|
fn(): bool => strpos($path, $separator) !== 0, |
|
|
|
|
32
|
14 |
|
"Initializing malformed ConfigPath. Path may not start with '$separator'" |
33
|
|
|
); |
34
|
13 |
|
$pathParts = explode($separator, $path); |
35
|
13 |
|
return new static(array_shift($pathParts), $pathParts, $separator); |
36
|
|
|
} |
37
|
|
|
|
38
|
8 |
|
public function getScope(): string |
39
|
|
|
{ |
40
|
8 |
|
return $this->scope; |
41
|
|
|
} |
42
|
|
|
|
43
|
7 |
|
public function getParts(): array |
44
|
|
|
{ |
45
|
7 |
|
return $this->parts; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public function hasParts(): bool |
49
|
|
|
{ |
50
|
1 |
|
return !empty($this->parts); |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
public function getLength(): int |
54
|
|
|
{ |
55
|
1 |
|
return count($this->parts); |
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
public function getSeparator(): string |
59
|
|
|
{ |
60
|
6 |
|
return $this->separator; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
public function __toString(): string |
64
|
|
|
{ |
65
|
1 |
|
$pathParts = $this->parts; |
66
|
1 |
|
array_unshift($pathParts, $this->scope); |
67
|
1 |
|
return join(self::PATH_SEP, $pathParts); |
68
|
|
|
} |
69
|
|
|
|
70
|
13 |
|
private function __construct(string $scope, array $parts, string $separator) |
71
|
|
|
{ |
72
|
13 |
|
Assertion::notEmpty($scope, 'Trying to create ConfigPath from empty scope.'); |
73
|
12 |
|
$this->separator = $separator; |
74
|
12 |
|
$this->scope = $scope; |
75
|
12 |
|
$this->parts = $parts; |
76
|
12 |
|
} |
77
|
|
|
} |
78
|
|
|
|