Psr4Namespace   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 100
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setValue() 0 10 2
A isValid() 0 8 1
A getValue() 0 4 1
A __toString() 0 4 1
A equals() 0 4 1
A startsWith() 0 6 1
A length() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Benrowe\Fqcn\Value;
6
7
use Benrowe\Fqcn\Exception;
8
9
/**
10
 * This represents a psr4 based namespace
11
 *
12
 * @package Benrowe\Fqcn
13
 */
14
final class Psr4Namespace
15
{
16
    private $namespace;
17
18
    /**
19
     * Constructor
20
     * Take in the namespace value as a string
21
     *
22
     * @param string $namespace
23
     */
24
    public function __construct(string $namespace)
25
    {
26
        $this->setValue($namespace);
27
    }
28
29
    /**
30
     * Handle setting the namepsace string value
31
     *
32
     * @param string $value
33
     */
34
    private function setValue(string $value)
35
    {
36
        // standardise the value
37
        $value = trim($value, '\\');
38
        // validate the namespace!
39
        if (!$this->isValid($value)) {
40
            throw new Exception('Invalid Namespace');
41
        }
42
        $this->namespace = $value . '\\';
43
    }
44
45
    /**
46
     * Determine if the supplied namespace string is a valid according to the
47
     * psr4 standard
48
     *
49
     * @param  string  $namespace
50
     * @return boolean
51
     */
52
    private function isValid(string $namespace): bool
53
    {
54
        $parts = explode('\\', $namespace);
55
        $verified = array_filter($parts, function ($value) {
56
            return (bool)preg_match("/^[A-Za-z][\w\d_]+$/", $value);
57
        });
58
        return count($parts) === count($verified);
59
    }
60
61
    /**
62
     * Get the namespace value as a string
63
     *
64
     * @return string
65
     */
66
    public function getValue(): string
67
    {
68
        return $this->namespace;
69
    }
70
71
    /**
72
     * Handle the object when treated as a string
73
     *
74
     * @return string
75
     */
76
    public function __toString(): string
77
    {
78
        return (string)$this->getValue();
79
    }
80
81
    /**
82
     * Determine the supplied namespace is the same as the current namespace
83
     *
84
     * @param  Psr4Namespace $value
85
     * @return bool
86
     */
87
    public function equals(Psr4Namespace $value): bool
88
    {
89
        return $value->getValue() === $this->getValue();
90
    }
91
92
    /**
93
     * Determine if this namepsace starts with the supplied namespace
94
     *
95
     * @param  Psr4Namespace $value
96
     * @return bool
97
     */
98
    public function startsWith(Psr4Namespace $value): bool
99
    {
100
        $start = substr($this->getValue(), 0, strlen($value->getValue()));
101
102
        return $start === $value->getValue();
103
    }
104
105
    /**
106
     * Get the length of the namepsace as a string (characters)
107
     * @return int
108
     */
109
    public function length(): int
110
    {
111
        return strlen($this->getValue());
112
    }
113
}
114