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