Completed
Push — develop ( fe7998...59b49f )
by Baptiste
01:41
created

Name::withoutRoot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.6666
cc 2
eloc 4
nc 2
nop 0
crap 2
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 220
    public function __construct(string $value)
17
    {
18 220
        $value = Str::of($value);
19
20 220
        if ($value->empty()) {
21 1
            throw new NameMustContainAtLeastACharacter;
22
        }
23
24 219
        $this->value = $value;
25 219
    }
26
27 8
    public function add(self $name): self
28
    {
29 8
        return new self($this->value.'.'.$name);
30
    }
31
32 48
    public function root(): self
33
    {
34 48
        $namespace = $this->value->split('.');
35
36 48
        if ($namespace->size() === 1) {
37 22
            throw new NameNotNamespaced((string) $this->value);
38
        }
39
40 32
        return new self((string) $namespace->first());
41
    }
42
43 28
    public function withoutRoot(): self
44
    {
45 28
        $namespace = $this->value->split('.');
46
47 28
        if ($namespace->size() === 1) {
48 1
            throw new NameNotNamespaced((string) $this->value);
49
        }
50
51 27
        return new self((string) $namespace->drop(1)->join('.'));
52
    }
53
54 6
    public function equals(self $other): bool
55
    {
56 6
        return (string) $this === (string) $other;
57
    }
58
59 201
    public function __toString(): string
60
    {
61 201
        return (string) $this->value;
62
    }
63
}
64