Passed
Push — master ( 52d18d...80d494 )
by Patrick
10:33
created

FullyQualifiedClassName::namespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Architecture\Analyzer;
5
6
class FullyQualifiedClassName
7
{
8
    /** @var PatternString */
9
    private $fqcnString;
10
11
    /** @var PatternString */
12
    private $namespace;
13
14
    /** @var PatternString */
15
    private $class;
16
17
    private function __construct(PatternString $fqcnString, PatternString $namespace, PatternString $class)
18
    {
19
        $this->fqcnString = $fqcnString;
20
        $this->namespace = $namespace;
21
        $this->class = $class;
22
    }
23
24
    public function toString(): string
25
    {
26
        return $this->fqcnString->toString();
27
    }
28
29
    public function classMatches(string $pattern): bool
30
    {
31
        return $this->class->matches($pattern);
32
    }
33
34
    public function namespaceMatches(string $pattern): bool
35
    {
36
        return $this->namespace->matches($pattern);
37
    }
38
39
    public function matches(string $pattern): bool
40
    {
41
        return $this->fqcnString->matches($pattern);
42
    }
43
44
    public function className(): string
45
    {
46
        return $this->class->toString();
47
    }
48
49
    public function namespace(): string
50
    {
51
        return $this->namespace->toString();
52
    }
53
54
    public static function fromString(string $fqcn): self
55
    {
56
        $validFqcn = '/^[a-zA-Z_\x7f-\xff\\\\][a-zA-Z0-9_\x7f-\xff\\\\]*[a-zA-Z0-9_\x7f-\xff]$/';
57
58
        if (!(bool) preg_match($validFqcn, $fqcn)) {
59
            throw new \RuntimeException("$fqcn is not a valid namespace definition");
60
        }
61
62
        $pieces = explode('\\', $fqcn);
63
        $piecesWithoutEmpty = array_filter($pieces);
64
        $className = array_pop($piecesWithoutEmpty);
65
        $namespace = implode('\\', $piecesWithoutEmpty);
66
67
        return new self(new PatternString($fqcn), new PatternString($namespace), new PatternString($className));
68
    }
69
}
70