Passed
Push — master ( dd24f6...e23e40 )
by Mr
02:33
created

ConfigPath::hasParts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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,
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected ':', expecting T_DOUBLE_ARROW on line 31 at column 20
Loading history...
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