FullyQualifiedClassName::namespace()   A
last analyzed

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
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of forecast.it.fill project.
7
 * (c) Patrick Jaja <[email protected]>
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Architecture\Analyzer;
13
14
class FullyQualifiedClassName
15
{
16
    /** @var PatternString */
17
    private $fqcnString;
18
19
    /** @var PatternString */
20
    private $namespace;
21
22
    /** @var PatternString */
23
    private $class;
24
25
    private function __construct(PatternString $fqcnString, PatternString $namespace, PatternString $class)
26
    {
27
        $this->fqcnString = $fqcnString;
28
        $this->namespace = $namespace;
29
        $this->class = $class;
30
    }
31
32
    public function toString(): string
33
    {
34
        return $this->fqcnString->toString();
35
    }
36
37
    public function classMatches(string $pattern): bool
38
    {
39
        return $this->class->matches($pattern);
40
    }
41
42
    public function namespaceMatches(string $pattern): bool
43
    {
44
        return $this->namespace->matches($pattern);
45
    }
46
47
    public function matches(string $pattern): bool
48
    {
49
        return $this->fqcnString->matches($pattern);
50
    }
51
52
    public function className(): string
53
    {
54
        return $this->class->toString();
55
    }
56
57
    public function namespace(): string
58
    {
59
        return $this->namespace->toString();
60
    }
61
62
    public static function fromString(string $fqcn): self
63
    {
64
        $validFqcn = '/^[a-zA-Z_\x7f-\xff\\\\][a-zA-Z0-9_\x7f-\xff\\\\]*[a-zA-Z0-9_\x7f-\xff]$/';
65
66
        if (! (bool) preg_match($validFqcn, $fqcn)) {
67
            throw new \RuntimeException("{$fqcn} is not a valid namespace definition");
68
        }
69
70
        $pieces = explode('\\', $fqcn);
71
        $piecesWithoutEmpty = array_filter($pieces);
72
        $className = array_pop($piecesWithoutEmpty);
73
        $namespace = implode('\\', $piecesWithoutEmpty);
74
75
        return new self(new PatternString($fqcn), new PatternString($namespace), new PatternString($className));
76
    }
77
}
78