1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* This file is part of the O2System Framework package.
|
4
|
|
|
*
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE
|
6
|
|
|
* file that was distributed with this source code.
|
7
|
|
|
*
|
8
|
|
|
* @author Steeve Andrian Salim
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------
|
13
|
|
|
|
14
|
|
|
namespace O2System\Spl\Patterns\Structural\Provider;
|
15
|
|
|
|
16
|
|
|
use O2System\Spl\Iterators\ArrayIterator;
|
17
|
|
|
|
18
|
|
|
/**
|
19
|
|
|
* Class AbstractProvider
|
20
|
|
|
*
|
21
|
|
|
* The best practice of this pattern class is to contain many objects instance
|
22
|
|
|
* which require to be validated.
|
23
|
|
|
*
|
24
|
|
|
* This pattern class is designed to be able to traversable using foreach.
|
25
|
|
|
*
|
26
|
|
|
* Note: This class is an abstract class so it can not be initiated.
|
27
|
|
|
*
|
28
|
|
|
* @package O2System\Spl\Patterns\Structural\Provider
|
29
|
|
|
*/
|
30
|
|
|
abstract class AbstractProvider implements
|
31
|
|
|
RegistryInterface,
|
32
|
|
|
\IteratorAggregate,
|
33
|
|
|
\Countable
|
34
|
|
|
{
|
35
|
|
|
/**
|
36
|
|
|
* AbstractProvider::$registry
|
37
|
|
|
*
|
38
|
|
|
* The array of contained objects.
|
39
|
|
|
* The access of this property is private so can't be manipulated from the child classes.
|
40
|
|
|
*
|
41
|
|
|
* @var array
|
42
|
|
|
*/
|
43
|
|
|
protected $registry = [];
|
44
|
|
|
|
45
|
|
|
// ------------------------------------------------------------------------
|
46
|
|
|
|
47
|
|
|
/**
|
48
|
|
|
* AbstractProvider::__get
|
49
|
|
|
*
|
50
|
|
|
* Application of __get magic method to retrieve the registered object which specified offset key.
|
51
|
|
|
*
|
52
|
|
|
* @param string $offset The object offset key.
|
53
|
|
|
*
|
54
|
|
|
* @return mixed Varies depends the data contents, return NULL when there offset is not found.
|
55
|
|
|
*/
|
56
|
|
|
final public function &__get($offset)
|
57
|
|
|
{
|
58
|
|
|
return $this->getObject($offset);
|
59
|
|
|
}
|
60
|
|
|
|
61
|
|
|
// ------------------------------------------------------------------------
|
62
|
|
|
|
63
|
|
|
/**
|
64
|
|
|
* AbstractProvider::__set
|
65
|
|
|
*
|
66
|
|
|
* Application of __set magic method to registered the object into the container.
|
67
|
|
|
*
|
68
|
|
|
* @param string $offset The object offset key.
|
69
|
|
|
* @param mixed $object The object to be contained.
|
70
|
|
|
*
|
71
|
|
|
* @return void
|
72
|
|
|
*/
|
73
|
|
|
final public function __set($offset, $object)
|
74
|
|
|
{
|
75
|
|
|
$this->register($object, $offset);
|
76
|
|
|
}
|
77
|
|
|
|
78
|
|
|
// ------------------------------------------------------------------------
|
79
|
|
|
|
80
|
|
|
/**
|
81
|
|
|
* AbstractProvider::getObject
|
82
|
|
|
*
|
83
|
|
|
* Retrieve the contained object which specified offset key.
|
84
|
|
|
* An alias of AbstractProvider::__get method.
|
85
|
|
|
*
|
86
|
|
|
* @param string $offset The object offset key.
|
87
|
|
|
*
|
88
|
|
|
* @return mixed Varies depends the data contents, return NULL when there offset is not found.
|
89
|
|
|
*/
|
90
|
|
|
public function &getObject($offset)
|
91
|
|
|
{
|
92
|
|
|
$get[ $offset ] = null;
|
|
|
|
|
93
|
|
|
|
94
|
|
|
if ($this->__isset($offset)) {
|
95
|
|
|
return $this->registry[ $offset ];
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
return $get[ $offset ];
|
99
|
|
|
}
|
100
|
|
|
|
101
|
|
|
// ------------------------------------------------------------------------
|
102
|
|
|
|
103
|
|
|
/**
|
104
|
|
|
* AbstractProvider::__isset
|
105
|
|
|
*
|
106
|
|
|
* Implements magic method isset to checks inaccessible properties.
|
107
|
|
|
*
|
108
|
|
|
* @param string $offset The object offset key.
|
109
|
|
|
*
|
110
|
|
|
* @return bool Returns TRUE on success or FALSE on failure.
|
111
|
|
|
*/
|
112
|
|
|
final public function __isset($offset)
|
113
|
|
|
{
|
114
|
|
|
return $this->exists($offset);
|
115
|
|
|
}
|
116
|
|
|
|
117
|
|
|
// ------------------------------------------------------------------------
|
118
|
|
|
|
119
|
|
|
/**
|
120
|
|
|
* AbstractProvider::has
|
121
|
|
|
*
|
122
|
|
|
* Checks if the provider has registry of an object with specified offset key.
|
123
|
|
|
* An alias of AbstractProvider::__isset method.
|
124
|
|
|
*
|
125
|
|
|
* @param string $offset The object offset key.
|
126
|
|
|
*
|
127
|
|
|
* @return bool Returns TRUE on success or FALSE on failure.
|
128
|
|
|
*/
|
129
|
|
|
public function exists($offset)
|
130
|
|
|
{
|
131
|
|
|
return (bool)isset($this->registry[ $offset ]);
|
132
|
|
|
}
|
133
|
|
|
|
134
|
|
|
// ------------------------------------------------------------------------
|
135
|
|
|
|
136
|
|
|
/**
|
137
|
|
|
* AbstractProvider::register
|
138
|
|
|
*
|
139
|
|
|
* Register the object into the class container.
|
140
|
|
|
* An alias of AbstractProvider::__set method.
|
141
|
|
|
*
|
142
|
|
|
* @param object $object The object to be contained.
|
143
|
|
|
* @param string $offset The object container array offset key.
|
144
|
|
|
*
|
145
|
|
|
* @return void
|
146
|
|
|
*/
|
147
|
|
|
public function register($object, $offset = null)
|
148
|
|
|
{
|
149
|
|
|
if (is_object($object)) {
|
150
|
|
|
if ($this instanceof ValidationInterface) {
|
151
|
|
|
if ($this->validate($object) === false) {
|
152
|
|
|
return;
|
153
|
|
|
}
|
154
|
|
|
}
|
155
|
|
|
|
156
|
|
|
if (is_null($offset)) {
|
157
|
|
|
$offset = get_class($object);
|
158
|
|
|
$offset = pathinfo($offset, PATHINFO_FILENAME);
|
159
|
|
|
}
|
160
|
|
|
|
161
|
|
|
if ( ! $this->__isset($offset)) {
|
162
|
|
|
$this->registry[ $offset ] = $object;
|
163
|
|
|
}
|
164
|
|
|
}
|
165
|
|
|
}
|
166
|
|
|
|
167
|
|
|
// ------------------------------------------------------------------------
|
168
|
|
|
|
169
|
|
|
/**
|
170
|
|
|
* AbstractProvider::__unset
|
171
|
|
|
*
|
172
|
|
|
* Removes an objects from the container.
|
173
|
|
|
*
|
174
|
|
|
* @param string $offset The object offset key.
|
175
|
|
|
*
|
176
|
|
|
* @return void
|
177
|
|
|
*/
|
178
|
|
|
final public function __unset($offset)
|
179
|
|
|
{
|
180
|
|
|
$this->remove($offset);
|
181
|
|
|
}
|
182
|
|
|
|
183
|
|
|
// ------------------------------------------------------------------------
|
184
|
|
|
|
185
|
|
|
/**
|
186
|
|
|
* AbstractProvider::remove
|
187
|
|
|
*
|
188
|
|
|
* Unregister an objects from the registry.
|
189
|
|
|
* An alias of AbstractProvider::__unset method.
|
190
|
|
|
*
|
191
|
|
|
* @param string $offset The object offset key.
|
192
|
|
|
*
|
193
|
|
|
* @return void
|
194
|
|
|
*/
|
195
|
|
|
public function remove($offset)
|
196
|
|
|
{
|
197
|
|
|
if ($this->__isset($offset)) {
|
198
|
|
|
if (method_exists($this->registry[ $offset ], '__destruct')) {
|
199
|
|
|
$this->registry[ $offset ]->__destruct();
|
200
|
|
|
}
|
201
|
|
|
|
202
|
|
|
unset($this->registry[ $offset ]);
|
203
|
|
|
}
|
204
|
|
|
}
|
205
|
|
|
|
206
|
|
|
// ------------------------------------------------------------------------
|
207
|
|
|
|
208
|
|
|
/**
|
209
|
|
|
* AbstractProvider::destroy
|
210
|
|
|
*
|
211
|
|
|
* Removes all objects from the registry and perform each object destruction.
|
212
|
|
|
*
|
213
|
|
|
* @return array Array of old registry
|
214
|
|
|
*/
|
215
|
|
|
final public function destroy()
|
216
|
|
|
{
|
217
|
|
|
foreach ($this->registry as $offset => $object) {
|
218
|
|
|
if (method_exists($object, '__destruct')) {
|
219
|
|
|
$object->__destruct();
|
220
|
|
|
}
|
221
|
|
|
unset($this->registry[ $offset ]);
|
222
|
|
|
}
|
223
|
|
|
}
|
224
|
|
|
|
225
|
|
|
// ------------------------------------------------------------------------
|
226
|
|
|
|
227
|
|
|
/**
|
228
|
|
|
* AbstractProvider::getObjectHash
|
229
|
|
|
*
|
230
|
|
|
* Gets a unique identifier for the registered object.
|
231
|
|
|
*
|
232
|
|
|
* @param $offset
|
233
|
|
|
*
|
234
|
|
|
* @return bool|string Returns FALSE on failure, or unique string identifier of the registered object on success.
|
235
|
|
|
*/
|
236
|
|
|
final public function getObjectHash($offset)
|
237
|
|
|
{
|
238
|
|
|
if ($this->__isset($offset)) {
|
239
|
|
|
return spl_object_hash($this->registry[ $offset ]);
|
240
|
|
|
}
|
241
|
|
|
|
242
|
|
|
return false;
|
243
|
|
|
}
|
244
|
|
|
|
245
|
|
|
// ------------------------------------------------------------------------
|
246
|
|
|
|
247
|
|
|
/**
|
248
|
|
|
* AbstractProvider::getObjectId
|
249
|
|
|
*
|
250
|
|
|
* Gets the integer object handle for the registered object.
|
251
|
|
|
*
|
252
|
|
|
* @param $offset
|
253
|
|
|
*
|
254
|
|
|
* @return bool|int Returns FALSE on failure, or the integer object handle for given object on success.
|
255
|
|
|
*/
|
256
|
|
|
final public function getObjectId($offset)
|
257
|
|
|
{
|
258
|
|
|
if ($this->__isset($offset)) {
|
259
|
|
|
return spl_object_id($this->registry[ $offset ]);
|
260
|
|
|
}
|
261
|
|
|
|
262
|
|
|
return false;
|
263
|
|
|
}
|
264
|
|
|
|
265
|
|
|
// ------------------------------------------------------------------------
|
266
|
|
|
|
267
|
|
|
/**
|
268
|
|
|
* AbstractProvider::count
|
269
|
|
|
*
|
270
|
|
|
* Application of Countable::count method to count the numbers of registered objects.
|
271
|
|
|
*
|
272
|
|
|
* @see http://php.net/manual/en/countable.count.php
|
273
|
|
|
*
|
274
|
|
|
* @return int The numbers of registered objects.
|
275
|
|
|
*/
|
276
|
|
|
final public function count()
|
277
|
|
|
{
|
278
|
|
|
return (int)count($this->registry);
|
279
|
|
|
}
|
280
|
|
|
|
281
|
|
|
// ------------------------------------------------------------------------
|
282
|
|
|
|
283
|
|
|
/**
|
284
|
|
|
* AbstractProvider::getIterator
|
285
|
|
|
*
|
286
|
|
|
* Application of IteratorAggregate::getIterator method to retrieve an external iterator.
|
287
|
|
|
*
|
288
|
|
|
* @see http://php.net/manual/en/iteratoraggregate.getiterator.php
|
289
|
|
|
* @return \ArrayIterator
|
290
|
|
|
*/
|
291
|
|
|
final public function getIterator()
|
292
|
|
|
{
|
293
|
|
|
return new ArrayIterator($this->registry);
|
294
|
|
|
}
|
295
|
|
|
} |