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

Name   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
dl 0
loc 50
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A withoutRoot() 0 9 2
A root() 0 9 2
A __toString() 0 3 1
A add() 0 3 1
A equals() 0 3 1
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