1 | <?php |
||
20 | abstract class AbstractPart implements \ArrayAccess |
||
21 | { |
||
22 | /** |
||
23 | * Flag for whether or not this part has been initialized. |
||
24 | * |
||
25 | * @var boolean |
||
26 | */ |
||
27 | protected $initialized = false; |
||
28 | |||
29 | /** |
||
30 | * Array of data for this part. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $data = []; |
||
35 | |||
36 | /** |
||
37 | * Array mapping part names to classes. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $partClassMap = []; |
||
42 | |||
43 | /** |
||
44 | * Gets the data for this part. This method will initialize the part if it is not already initialized. |
||
45 | * |
||
46 | * @return array |
||
47 | */ |
||
48 | 3 | public function getData(): array |
|
54 | |||
55 | /** |
||
56 | * Sets the data for this part. This method will initialize the part if it is not already initialized. |
||
57 | * |
||
58 | * @param array $data |
||
59 | * |
||
60 | * @return $this |
||
61 | */ |
||
62 | 1 | public function setData(array $data) |
|
75 | |||
76 | /** |
||
77 | * Check if this part has been initialized yet. |
||
78 | * |
||
79 | * @return boolean |
||
80 | */ |
||
81 | public function isInitialized(): bool |
||
85 | |||
86 | /** |
||
87 | * Check if this part has data by key. |
||
88 | * |
||
89 | * @param string $key |
||
90 | * |
||
91 | * @return boolean |
||
92 | */ |
||
93 | public function has($key): bool |
||
99 | |||
100 | /** |
||
101 | * Gets data from this part by key. |
||
102 | * |
||
103 | * @param string $key |
||
104 | * |
||
105 | * @return mixed|null |
||
106 | */ |
||
107 | 65 | public function get($key) |
|
113 | |||
114 | /** |
||
115 | * Set data for this part by key. |
||
116 | * |
||
117 | * @param string $key |
||
118 | * @param mixed $value |
||
119 | * |
||
120 | * @return static |
||
121 | */ |
||
122 | 6 | public function set($key, $value) |
|
129 | |||
130 | /** |
||
131 | * Add data for this part. |
||
132 | * |
||
133 | * @param mixed $value |
||
134 | * |
||
135 | * @return $this |
||
136 | */ |
||
137 | 3 | public function add($value) |
|
144 | |||
145 | /** |
||
146 | * Remove data from this part by key. |
||
147 | * |
||
148 | * @param $key |
||
149 | */ |
||
150 | public function remove($key) |
||
155 | |||
156 | /** Property Overloading */ |
||
157 | |||
158 | /** |
||
159 | * @param $key |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | public function __isset($key) |
||
167 | |||
168 | /** |
||
169 | * @param $key |
||
170 | * |
||
171 | * @return bool |
||
172 | */ |
||
173 | 65 | public function __get($key) |
|
177 | |||
178 | /** |
||
179 | * @param $key |
||
180 | * @param $value |
||
181 | * |
||
182 | * @return AbstractPart |
||
183 | */ |
||
184 | 2 | public function __set($key, $value) |
|
188 | |||
189 | /** |
||
190 | * @param $key |
||
191 | */ |
||
192 | public function __unset($key) |
||
196 | |||
197 | /** ArrayAccess */ |
||
198 | |||
199 | /** |
||
200 | * @param mixed $key |
||
201 | * |
||
202 | * @return bool |
||
203 | */ |
||
204 | public function offsetExists($key): bool |
||
210 | |||
211 | /** |
||
212 | * @param mixed $key |
||
213 | * |
||
214 | * @return bool |
||
215 | */ |
||
216 | public function offsetGet($key): bool |
||
220 | |||
221 | /** |
||
222 | * @param mixed $key |
||
223 | * @param mixed $value |
||
224 | * |
||
225 | * @return AbstractPart |
||
226 | */ |
||
227 | 1 | public function offsetSet($key, $value): AbstractPart |
|
231 | |||
232 | /** |
||
233 | * @param mixed $key |
||
234 | */ |
||
235 | public function offsetUnset($key) |
||
239 | |||
240 | 73 | protected function initialize() |
|
250 | |||
251 | /** |
||
252 | * Prepare a part value. |
||
253 | * |
||
254 | * @param string $key |
||
255 | * @param string|static $value |
||
256 | * |
||
257 | * @return static |
||
258 | */ |
||
259 | 65 | protected function preparePartValue($key, &$value) |
|
269 | |||
270 | /** |
||
271 | * Convert the instance back in to string form from the internal parts. |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | abstract public function __toString(); |
||
276 | |||
277 | /** |
||
278 | * Each part that extends AbstractPart must implement an doInitialize() method. |
||
279 | * |
||
280 | * @return void |
||
281 | */ |
||
282 | abstract protected function doInitialize(); |
||
283 | } |
||
284 |