Passed
Push — code-cleanup ( 2854f1...e37789 )
by Jonathan
01:45
created

AbstractPart::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Purl;
6
7
use ArrayAccess;
8
9
/**
10
 * AbstractPart class is implemented by each part of a Url where necessary.
11
 *
12
 * @implements ArrayAccess
13
 */
14
abstract class AbstractPart implements ArrayAccess
15
{
16
    /** @var bool */
17
    protected $initialized = false;
18
19
    /** @var mixed[] */
20
    protected $data = [];
21
22
    /** @var string[] */
23
    protected $partClassMap = [];
24
25
    /**
26
     * @return mixed[]
27
     */
28 4
    public function getData() : array
29
    {
30 4
        $this->initialize();
31
32 4
        return $this->data;
33
    }
34
35
    /**
36
     * @param mixed[] $data
37
     */
38 7
    public function setData(array $data) : void
39
    {
40 7
        $this->initialize();
41
42 7
        $this->data = $data;
43 7
    }
44
45 1
    public function isInitialized() : bool
46
    {
47 1
        return $this->initialized;
48
    }
49
50 3
    public function has(string $key) : bool
51
    {
52 3
        $this->initialize();
53
54 3
        return isset($this->data[$key]);
55
    }
56
57
    /**
58
     * @return mixed|null
59
     */
60 64
    public function get(string $key)
61
    {
62 64
        $this->initialize();
63
64 64
        return $this->data[$key] ?? null;
65
    }
66
67
    /**
68
     * @param mixed $value
69
     */
70 8
    public function set(string $key, $value) : AbstractPart
71
    {
72 8
        $this->initialize();
73 8
        $this->data[$key] = $value;
74
75 8
        return $this;
76
    }
77
78
    /**
79
     * @param mixed $value
80
     */
81 3
    public function add($value) : AbstractPart
82
    {
83 3
        $this->initialize();
84 3
        $this->data[] = $value;
85
86 3
        return $this;
87
    }
88
89 3
    public function remove(string $key) : void
90
    {
91 3
        $this->initialize();
92
93 3
        unset($this->data[$key]);
94 3
    }
95
96
    public function __isset(string $key) : bool
97
    {
98
        return $this->has($key);
99
    }
100
101
    /**
102
     * @return mixed
103
     */
104 63
    public function __get(string $key)
105
    {
106 63
        return $this->get($key);
107
    }
108
109
    /**
110
     * @param mixed $value
111
     */
112 6
    public function __set(string $key, $value) : AbstractPart
113
    {
114 6
        return $this->set($key, $value);
115
    }
116
117
    public function __unset(string $key) : void
118
    {
119
        $this->remove($key);
120
    }
121
122
    /**
123
     * @param string $key
124
     */
125 2
    public function offsetExists($key) : bool
126
    {
127 2
        $this->initialize();
128
129 2
        return isset($this->data[$key]);
130
    }
131
132
    /**
133
     * @param string $key
134
     *
135
     * @return mixed
136
     */
137 1
    public function offsetGet($key)
138
    {
139 1
        return $this->get($key);
140
    }
141
142
    /**
143
     * @param string $key
144
     * @param mixed  $value
145
     *
146
     * @return void
147
     */
148 1
    public function offsetSet($key, $value)
149
    {
150 1
        $this->set($key, $value);
151 1
    }
152
153
    /**
154
     * @param string $key
155
     *
156
     * @return void
157
     */
158 1
    public function offsetUnset($key)
159
    {
160 1
        $this->remove($key);
161 1
    }
162
163 77
    protected function initialize() : void
164
    {
165 77
        if ($this->initialized === true) {
166 69
            return;
167
        }
168
169 77
        $this->initialized = true;
170
171 77
        $this->doInitialize();
172 77
    }
173
174
    /**
175
     * @param string|AbstractPart $value
176
     *
177
     * @return mixed
178
     */
179 69
    protected function preparePartValue(string $key, $value)
180
    {
181 69
        if (! isset($this->partClassMap[$key])) {
182 57
            return $value;
183
        }
184
185 69
        $className = $this->partClassMap[$key];
186
187 69
        return ! $value instanceof $className ? new $className($value) : $value;
188
    }
189
190
    abstract public function __toString() : string;
191
192
    abstract protected function doInitialize() : void;
193
}
194