Urn::getNamespaceSpecificString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of the Urn project
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Urn;
9
10
/**
11
 * URN class.
12
 *
13
 * @package GravityMedia\Urn
14
 */
15
class Urn implements UrnInterface
16
{
17
    /**
18
     * Namespace identifier regex pattern.
19
     *
20
     * @const string
21
     */
22
    const NID_PATTERN = '/^[A-Za-z0-9-][A-Za-z0-9-]{0,31}$/';
23
24
    /**
25
     * Namespace specific string regex pattern.
26
     */
27
    const NSS_PATTERN = '/^[A-Za-z0-9()+,\-.:=@;$_!*\'%\/?#]+$/';
28
29
    /**
30
     * The namespace identifier.
31
     *
32
     * @var string
33
     */
34
    protected $namespaceIdentifier;
35
36
    /**
37
     * The namespace specific string.
38
     *
39
     * @var string
40
     */
41
    protected $namespaceSpecificString;
42
43
    /**
44
     * The protected constructor.
45
     */
46 4
    protected function __construct()
47
    {
48
        // to prevent object construction without nid and/or nss
49 4
    }
50
51
    /**
52
     * Create URN object from array.
53
     *
54
     * @param array $array
55
     *
56
     * @return static
57
     * @throws \InvalidArgumentException
58
     */
59 4
    public static function fromArray(array $array)
60
    {
61 4
        if (!isset($array['nid'])) {
62
            throw new \InvalidArgumentException('The namespace identifier was not specified');
63
        }
64
65 4
        if (!isset($array['nss'])) {
66
            throw new \InvalidArgumentException('The namespace specific string was not specified');
67
        }
68
69
        /** @var Urn $urn */
70 4
        $urn = new static();
71 4
        $urn = $urn->withNamespaceIdentifier($array['nid']);
72 4
        $urn = $urn->withNamespaceSpecificString($array['nss']);
73
74 4
        return $urn;
75
    }
76
77
    /**
78
     * Create URN object from string.
79
     *
80
     * @param string $string
81
     *
82
     * @return static
83
     * @throws \InvalidArgumentException
84
     */
85 6
    public static function fromString($string)
86
    {
87 6
        $array = explode(':', $string, 3);
88 6
        if (3 !== count($array) || 'urn' !== strtolower($array[0])) {
89 2
            throw new \InvalidArgumentException('The string argument appears to be malformed');
90
        }
91
92 4
        if (1 !== preg_match(static::NID_PATTERN, $array[1])) {
93
            throw new \InvalidArgumentException('The string does not contain a valid namespace identifier');
94
        }
95
96 4
        if (1 !== preg_match(static::NSS_PATTERN, $array[2])) {
97
            throw new \InvalidArgumentException('The string does not contain a valid namespace specific string');
98
        }
99
100 4
        $array['nid'] = $array[1];
101 4
        $array['nss'] = $array[2];
102
103 4
        return static::fromArray($array);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 4
    public function getNamespaceIdentifier()
110
    {
111 4
        return $this->namespaceIdentifier;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 4
    public function getNamespaceSpecificString()
118
    {
119 4
        return $this->namespaceSpecificString;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 4 View Code Duplication
    public function withNamespaceIdentifier($nid)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127 4
        if (1 !== preg_match(static::NID_PATTERN, $nid)) {
128
            throw new \InvalidArgumentException('The string does not contain a valid namespace identifier');
129
        }
130
131 4
        $urn = clone $this;
132 4
        $urn->namespaceIdentifier = (string)$nid;
133
134 4
        return $urn;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 4 View Code Duplication
    public function withNamespaceSpecificString($nss)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
    {
142 4
        if (1 !== preg_match(static::NSS_PATTERN, $nss)) {
143
            throw new \InvalidArgumentException('The string does not contain a valid namespace specific string');
144
        }
145
146 4
        $urn = clone $this;
147 4
        $urn->namespaceSpecificString = (string)$nss;
148
149 4
        return $urn;
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155 4
    public function __toString()
156
    {
157 4
        return $this->toString();
158
    }
159
160
    /**
161
     * Convert URN into a string representation.
162
     *
163
     * @return string
164
     */
165 4
    public function toString()
166
    {
167 4
        $urn = 'urn';
168 4
        $urn .= ':' . $this->getNamespaceIdentifier();
169 4
        $urn .= ':' . $this->getNamespaceSpecificString();
170
171 4
        return $urn;
172
    }
173
}
174