Completed
Push — develop ( fc6f83...efe27a )
by Baptiste
02:01
created

Name::equals()   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
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Definition;
5
6
use Innmind\Compose\Exception\{
7
    NameMustContainAtLeastACharacter,
8
    NameNotNamespaced
9
};
10
use Innmind\Immutable\Str;
11
12
final class Name
13
{
14
    private $value;
15
16 189
    public function __construct(string $value)
17
    {
18 189
        $value = Str::of($value);
19
20 189
        if ($value->empty()) {
21 1
            throw new NameMustContainAtLeastACharacter;
22
        }
23
24 188
        $this->value = $value;
25 188
    }
26
27 31
    public function root(): self
28
    {
29 31
        $namespace = $this->value->split('.');
30
31 31
        if ($namespace->size() === 1) {
32 14
            throw new NameNotNamespaced((string) $this->value);
33
        }
34
35 19
        return new self((string) $namespace->first());
36
    }
37
38 14
    public function withoutRoot(): self
39
    {
40 14
        $namespace = $this->value->split('.');
41
42 14
        if ($namespace->size() === 1) {
43 1
            throw new NameNotNamespaced((string) $this->value);
44
        }
45
46 13
        return new self((string) $namespace->drop(1)->join('.'));
47
    }
48
49 5
    public function equals(self $other): bool
50
    {
51 5
        return (string) $this === (string) $other;
52
    }
53
54 173
    public function __toString(): string
55
    {
56 173
        return (string) $this->value;
57
    }
58
}
59