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