@@ -30,78 +30,78 @@ |
||
30 | 30 | */ |
31 | 31 | class StandardObjectStorage |
32 | 32 | { |
33 | - /** |
|
34 | - * An array of fetched object, accessible via table name and primary key. |
|
35 | - * If the primary key is split on several columns, access is done by an array of columns, serialized. |
|
36 | - * |
|
37 | - * @var array<string, array<string, TDBMObject>> |
|
38 | - */ |
|
39 | - private $objects = array(); |
|
33 | + /** |
|
34 | + * An array of fetched object, accessible via table name and primary key. |
|
35 | + * If the primary key is split on several columns, access is done by an array of columns, serialized. |
|
36 | + * |
|
37 | + * @var array<string, array<string, TDBMObject>> |
|
38 | + */ |
|
39 | + private $objects = array(); |
|
40 | 40 | |
41 | - /** |
|
42 | - * Sets an object in the storage. |
|
43 | - * |
|
44 | - * @param string $tableName |
|
45 | - * @param string $id |
|
46 | - * @param TDBMObject $dbRow |
|
47 | - */ |
|
48 | - public function set($tableName, $id, DbRow $dbRow) |
|
49 | - { |
|
50 | - $this->objects[$tableName][$id] = $dbRow; |
|
51 | - } |
|
41 | + /** |
|
42 | + * Sets an object in the storage. |
|
43 | + * |
|
44 | + * @param string $tableName |
|
45 | + * @param string $id |
|
46 | + * @param TDBMObject $dbRow |
|
47 | + */ |
|
48 | + public function set($tableName, $id, DbRow $dbRow) |
|
49 | + { |
|
50 | + $this->objects[$tableName][$id] = $dbRow; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Checks if an object is in the storage. |
|
55 | - * |
|
56 | - * @param string $tableName |
|
57 | - * @param string $id |
|
58 | - * |
|
59 | - * @return bool |
|
60 | - */ |
|
61 | - public function has($tableName, $id) |
|
62 | - { |
|
63 | - return isset($this->objects[$tableName][$id]); |
|
64 | - } |
|
53 | + /** |
|
54 | + * Checks if an object is in the storage. |
|
55 | + * |
|
56 | + * @param string $tableName |
|
57 | + * @param string $id |
|
58 | + * |
|
59 | + * @return bool |
|
60 | + */ |
|
61 | + public function has($tableName, $id) |
|
62 | + { |
|
63 | + return isset($this->objects[$tableName][$id]); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Returns an object from the storage (or null if no object is set). |
|
68 | - * |
|
69 | - * @param string $tableName |
|
70 | - * @param string $id |
|
71 | - * |
|
72 | - * @return DbRow |
|
73 | - */ |
|
74 | - public function get($tableName, $id) |
|
75 | - { |
|
76 | - if (isset($this->objects[$tableName][$id])) { |
|
77 | - return $this->objects[$tableName][$id]; |
|
78 | - } else { |
|
79 | - return; |
|
80 | - } |
|
81 | - } |
|
66 | + /** |
|
67 | + * Returns an object from the storage (or null if no object is set). |
|
68 | + * |
|
69 | + * @param string $tableName |
|
70 | + * @param string $id |
|
71 | + * |
|
72 | + * @return DbRow |
|
73 | + */ |
|
74 | + public function get($tableName, $id) |
|
75 | + { |
|
76 | + if (isset($this->objects[$tableName][$id])) { |
|
77 | + return $this->objects[$tableName][$id]; |
|
78 | + } else { |
|
79 | + return; |
|
80 | + } |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Removes an object from the storage. |
|
85 | - * |
|
86 | - * @param string $tableName |
|
87 | - * @param string $id |
|
88 | - */ |
|
89 | - public function remove($tableName, $id) |
|
90 | - { |
|
91 | - unset($this->objects[$tableName][$id]); |
|
92 | - } |
|
83 | + /** |
|
84 | + * Removes an object from the storage. |
|
85 | + * |
|
86 | + * @param string $tableName |
|
87 | + * @param string $id |
|
88 | + */ |
|
89 | + public function remove($tableName, $id) |
|
90 | + { |
|
91 | + unset($this->objects[$tableName][$id]); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Applies the callback to all objects. |
|
96 | - * |
|
97 | - * @param callable $callback |
|
98 | - */ |
|
99 | - public function apply(callable $callback) |
|
100 | - { |
|
101 | - foreach ($this->objects as $tableName => $table) { |
|
102 | - foreach ($table as $id => $obj) { |
|
103 | - $callback($obj, $tableName, $id); |
|
104 | - } |
|
105 | - } |
|
106 | - } |
|
94 | + /** |
|
95 | + * Applies the callback to all objects. |
|
96 | + * |
|
97 | + * @param callable $callback |
|
98 | + */ |
|
99 | + public function apply(callable $callback) |
|
100 | + { |
|
101 | + foreach ($this->objects as $tableName => $table) { |
|
102 | + foreach ($table as $id => $obj) { |
|
103 | + $callback($obj, $tableName, $id); |
|
104 | + } |
|
105 | + } |
|
106 | + } |
|
107 | 107 | } |
@@ -29,521 +29,521 @@ |
||
29 | 29 | */ |
30 | 30 | abstract class AbstractTDBMObject |
31 | 31 | { |
32 | - /** |
|
33 | - * The service this object is bound to. |
|
34 | - * |
|
35 | - * @var TDBMService |
|
36 | - */ |
|
37 | - protected $tdbmService; |
|
38 | - |
|
39 | - /** |
|
40 | - * An array of DbRow, indexed by table name. |
|
41 | - * |
|
42 | - * @var DbRow[] |
|
43 | - */ |
|
44 | - protected $dbRows = array(); |
|
45 | - |
|
46 | - /** |
|
47 | - * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
48 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
49 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
50 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
51 | - * |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - private $status; |
|
55 | - |
|
56 | - /** |
|
57 | - * Array storing beans related via many to many relationships (pivot tables). |
|
58 | - * |
|
59 | - * @var \SplObjectStorage[] Key: pivot table name, value: SplObjectStorage |
|
60 | - */ |
|
61 | - private $relationships = []; |
|
62 | - |
|
63 | - /** |
|
64 | - * @var bool[] Key: pivot table name, value: whether a query was performed to load the data. |
|
65 | - */ |
|
66 | - private $loadedRelationships = []; |
|
67 | - |
|
68 | - /** |
|
69 | - * Used with $primaryKeys when we want to retrieve an existing object |
|
70 | - * and $primaryKeys=[] if we want a new object. |
|
71 | - * |
|
72 | - * @param string $tableName |
|
73 | - * @param array $primaryKeys |
|
74 | - * @param TDBMService $tdbmService |
|
75 | - * |
|
76 | - * @throws TDBMException |
|
77 | - * @throws TDBMInvalidOperationException |
|
78 | - */ |
|
79 | - public function __construct($tableName = null, array $primaryKeys = array(), TDBMService $tdbmService = null) |
|
80 | - { |
|
81 | - // FIXME: lazy loading should be forbidden on tables with inheritance and dynamic type assignation... |
|
82 | - if (!empty($tableName)) { |
|
83 | - $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService); |
|
84 | - } |
|
85 | - |
|
86 | - if ($tdbmService === null) { |
|
87 | - $this->_setStatus(TDBMObjectStateEnum::STATE_DETACHED); |
|
88 | - } else { |
|
89 | - $this->_attach($tdbmService); |
|
90 | - if (!empty($primaryKeys)) { |
|
91 | - $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED); |
|
92 | - } else { |
|
93 | - $this->_setStatus(TDBMObjectStateEnum::STATE_NEW); |
|
94 | - } |
|
95 | - } |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Alternative constructor called when data is fetched from database via a SELECT. |
|
100 | - * |
|
101 | - * @param array $beanData array<table, array<column, value>> |
|
102 | - * @param TDBMService $tdbmService |
|
103 | - */ |
|
104 | - public function _constructFromData(array $beanData, TDBMService $tdbmService) |
|
105 | - { |
|
106 | - $this->tdbmService = $tdbmService; |
|
107 | - |
|
108 | - foreach ($beanData as $table => $columns) { |
|
109 | - $this->dbRows[$table] = new DbRow($this, $table, $tdbmService->_getPrimaryKeysFromObjectData($table, $columns), $tdbmService, $columns); |
|
110 | - } |
|
111 | - |
|
112 | - $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Alternative constructor called when bean is lazily loaded. |
|
117 | - * |
|
118 | - * @param string $tableName |
|
119 | - * @param array $primaryKeys |
|
120 | - * @param TDBMService $tdbmService |
|
121 | - */ |
|
122 | - public function _constructLazy($tableName, array $primaryKeys, TDBMService $tdbmService) |
|
123 | - { |
|
124 | - $this->tdbmService = $tdbmService; |
|
125 | - |
|
126 | - $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService); |
|
127 | - |
|
128 | - $this->status = TDBMObjectStateEnum::STATE_NOT_LOADED; |
|
129 | - } |
|
130 | - |
|
131 | - public function _attach(TDBMService $tdbmService) |
|
132 | - { |
|
133 | - if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) { |
|
134 | - throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.'); |
|
135 | - } |
|
136 | - $this->tdbmService = $tdbmService; |
|
137 | - |
|
138 | - // If we attach this object, we must work to make sure the tables are in ascending order (from low level to top level) |
|
139 | - $tableNames = array_keys($this->dbRows); |
|
140 | - $tableNames = $this->tdbmService->_getLinkBetweenInheritedTables($tableNames); |
|
141 | - $tableNames = array_reverse($tableNames); |
|
142 | - |
|
143 | - $newDbRows = []; |
|
144 | - |
|
145 | - foreach ($tableNames as $table) { |
|
146 | - if (!isset($this->dbRows[$table])) { |
|
147 | - $this->registerTable($table); |
|
148 | - } |
|
149 | - $newDbRows[$table] = $this->dbRows[$table]; |
|
150 | - } |
|
151 | - $this->dbRows = $newDbRows; |
|
152 | - |
|
153 | - $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
154 | - foreach ($this->dbRows as $dbRow) { |
|
155 | - $dbRow->_attach($tdbmService); |
|
156 | - } |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Sets the state of the TDBM Object |
|
161 | - * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
162 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
163 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
164 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
165 | - * |
|
166 | - * @param string $state |
|
167 | - */ |
|
168 | - public function _setStatus($state) |
|
169 | - { |
|
170 | - $this->status = $state; |
|
171 | - |
|
172 | - // TODO: we might ignore the loaded => dirty state here! dirty status comes from the db_row itself. |
|
173 | - foreach ($this->dbRows as $dbRow) { |
|
174 | - $dbRow->_setStatus($state); |
|
175 | - } |
|
176 | - } |
|
177 | - |
|
178 | - public function get($var, $tableName = null) |
|
179 | - { |
|
180 | - if ($tableName === null) { |
|
181 | - if (count($this->dbRows) > 1) { |
|
182 | - throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
183 | - } elseif (count($this->dbRows) === 1) { |
|
184 | - $tableName = array_keys($this->dbRows)[0]; |
|
185 | - } |
|
186 | - } |
|
187 | - |
|
188 | - if (!isset($this->dbRows[$tableName])) { |
|
189 | - if (count($this->dbRows[$tableName] === 0)) { |
|
190 | - throw new TDBMException('Object is not yet bound to any table.'); |
|
191 | - } else { |
|
192 | - throw new TDBMException('Unknown table "'.$tableName.'"" in object.'); |
|
193 | - } |
|
194 | - } |
|
195 | - |
|
196 | - return $this->dbRows[$tableName]->get($var); |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Returns true if a column is set, false otherwise. |
|
201 | - * |
|
202 | - * @param string $var |
|
203 | - * |
|
204 | - * @return bool |
|
205 | - */ |
|
206 | - public function has($var, $tableName = null) |
|
207 | - { |
|
208 | - if ($tableName === null) { |
|
209 | - if (count($this->dbRows) > 1) { |
|
210 | - throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
211 | - } elseif (count($this->dbRows) === 1) { |
|
212 | - $tableName = array_keys($this->dbRows)[0]; |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - if (!isset($this->dbRows[$tableName])) { |
|
217 | - if (count($this->dbRows[$tableName] === 0)) { |
|
218 | - throw new TDBMException('Object is not yet bound to any table.'); |
|
219 | - } else { |
|
220 | - throw new TDBMException('Unknown table "'.$tableName.'"" in object.'); |
|
221 | - } |
|
222 | - } |
|
223 | - |
|
224 | - return $this->dbRows[$tableName]->has($var); |
|
225 | - } |
|
226 | - |
|
227 | - public function set($var, $value, $tableName = null) |
|
228 | - { |
|
229 | - if ($tableName === null) { |
|
230 | - if (count($this->dbRows) > 1) { |
|
231 | - throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
232 | - } elseif (count($this->dbRows) === 1) { |
|
233 | - $tableName = array_keys($this->dbRows)[0]; |
|
234 | - } else { |
|
235 | - throw new TDBMException('Please specify a table for this object.'); |
|
236 | - } |
|
237 | - } |
|
238 | - |
|
239 | - if (!isset($this->dbRows[$tableName])) { |
|
240 | - $this->registerTable($tableName); |
|
241 | - } |
|
242 | - |
|
243 | - $this->dbRows[$tableName]->set($var, $value); |
|
244 | - if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
245 | - $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
246 | - } |
|
247 | - } |
|
248 | - |
|
249 | - /** |
|
250 | - * @param string $foreignKeyName |
|
251 | - * @param AbstractTDBMObject $bean |
|
252 | - */ |
|
253 | - public function setRef($foreignKeyName, AbstractTDBMObject $bean, $tableName = null) |
|
254 | - { |
|
255 | - if ($tableName === null) { |
|
256 | - if (count($this->dbRows) > 1) { |
|
257 | - throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
258 | - } elseif (count($this->dbRows) === 1) { |
|
259 | - $tableName = array_keys($this->dbRows)[0]; |
|
260 | - } else { |
|
261 | - throw new TDBMException('Please specify a table for this object.'); |
|
262 | - } |
|
263 | - } |
|
264 | - |
|
265 | - if (!isset($this->dbRows[$tableName])) { |
|
266 | - $this->registerTable($tableName); |
|
267 | - } |
|
268 | - |
|
269 | - $this->dbRows[$tableName]->setRef($foreignKeyName, $bean); |
|
270 | - if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
271 | - $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
272 | - } |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * @param string $foreignKeyName A unique name for this reference |
|
277 | - * |
|
278 | - * @return AbstractTDBMObject|null |
|
279 | - */ |
|
280 | - public function getRef($foreignKeyName, $tableName = null) |
|
281 | - { |
|
282 | - if ($tableName === null) { |
|
283 | - if (count($this->dbRows) > 1) { |
|
284 | - throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
285 | - } elseif (count($this->dbRows) === 1) { |
|
286 | - $tableName = array_keys($this->dbRows)[0]; |
|
287 | - } |
|
288 | - } |
|
289 | - |
|
290 | - if (!isset($this->dbRows[$tableName])) { |
|
291 | - if (count($this->dbRows[$tableName] === 0)) { |
|
292 | - throw new TDBMException('Object is not yet bound to any table.'); |
|
293 | - } else { |
|
294 | - throw new TDBMException('Unknown table "'.$tableName.'"" in object.'); |
|
295 | - } |
|
296 | - } |
|
297 | - |
|
298 | - return $this->dbRows[$tableName]->getRef($foreignKeyName); |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * Adds a many to many relationship to this bean. |
|
303 | - * |
|
304 | - * @param string $pivotTableName |
|
305 | - * @param AbstractTDBMObject $remoteBean |
|
306 | - */ |
|
307 | - protected function addRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
308 | - { |
|
309 | - $this->setRelationship($pivotTableName, $remoteBean, 'new'); |
|
310 | - } |
|
311 | - |
|
312 | - /** |
|
313 | - * Returns true if there is a relationship to this bean. |
|
314 | - * |
|
315 | - * @param string $pivotTableName |
|
316 | - * @param AbstractTDBMObject $remoteBean |
|
317 | - * |
|
318 | - * @return bool |
|
319 | - */ |
|
320 | - protected function hasRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
321 | - { |
|
322 | - $storage = $this->retrieveRelationshipsStorage($pivotTableName); |
|
323 | - |
|
324 | - if ($storage->contains($remoteBean)) { |
|
325 | - if ($storage[$remoteBean]['status'] !== 'delete') { |
|
326 | - return true; |
|
327 | - } |
|
328 | - } |
|
329 | - |
|
330 | - return false; |
|
331 | - } |
|
332 | - |
|
333 | - /** |
|
334 | - * Internal TDBM method. Removes a many to many relationship from this bean. |
|
335 | - * |
|
336 | - * @param string $pivotTableName |
|
337 | - * @param AbstractTDBMObject $remoteBean |
|
338 | - */ |
|
339 | - public function _removeRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
340 | - { |
|
341 | - if (isset($this->relationships[$pivotTableName][$remoteBean]) && $this->relationships[$pivotTableName][$remoteBean]['status'] === 'new') { |
|
342 | - unset($this->relationships[$pivotTableName][$remoteBean]); |
|
343 | - unset($remoteBean->relationships[$pivotTableName][$this]); |
|
344 | - } else { |
|
345 | - $this->setRelationship($pivotTableName, $remoteBean, 'delete'); |
|
346 | - } |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * Returns the list of objects linked to this bean via $pivotTableName. |
|
351 | - * |
|
352 | - * @param $pivotTableName |
|
353 | - * |
|
354 | - * @return \SplObjectStorage |
|
355 | - */ |
|
356 | - private function retrieveRelationshipsStorage($pivotTableName) |
|
357 | - { |
|
358 | - $storage = $this->getRelationshipStorage($pivotTableName); |
|
359 | - if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || isset($this->loadedRelationships[$pivotTableName]) && $this->loadedRelationships[$pivotTableName]) { |
|
360 | - return $storage; |
|
361 | - } |
|
362 | - |
|
363 | - $beans = $this->tdbmService->_getRelatedBeans($pivotTableName, $this); |
|
364 | - $this->loadedRelationships[$pivotTableName] = true; |
|
365 | - |
|
366 | - foreach ($beans as $bean) { |
|
367 | - if (isset($storage[$bean])) { |
|
368 | - $oldStatus = $storage[$bean]['status']; |
|
369 | - if ($oldStatus === 'delete') { |
|
370 | - // Keep deleted things deleted |
|
371 | - continue; |
|
372 | - } |
|
373 | - } |
|
374 | - $this->setRelationship($pivotTableName, $bean, 'loaded'); |
|
375 | - } |
|
376 | - |
|
377 | - return $storage; |
|
378 | - } |
|
379 | - |
|
380 | - /** |
|
381 | - * Internal TDBM method. Returns the list of objects linked to this bean via $pivotTableName. |
|
382 | - * |
|
383 | - * @param $pivotTableName |
|
384 | - * |
|
385 | - * @return AbstractTDBMObject[] |
|
386 | - */ |
|
387 | - public function _getRelationships($pivotTableName) |
|
388 | - { |
|
389 | - return $this->relationshipStorageToArray($this->retrieveRelationshipsStorage($pivotTableName)); |
|
390 | - } |
|
391 | - |
|
392 | - private function relationshipStorageToArray(\SplObjectStorage $storage) |
|
393 | - { |
|
394 | - $beans = []; |
|
395 | - foreach ($storage as $bean) { |
|
396 | - $statusArr = $storage[$bean]; |
|
397 | - if ($statusArr['status'] !== 'delete') { |
|
398 | - $beans[] = $bean; |
|
399 | - } |
|
400 | - } |
|
401 | - |
|
402 | - return $beans; |
|
403 | - } |
|
404 | - |
|
405 | - /** |
|
406 | - * Declares a relationship between. |
|
407 | - * |
|
408 | - * @param string $pivotTableName |
|
409 | - * @param AbstractTDBMObject $remoteBean |
|
410 | - * @param string $status |
|
411 | - */ |
|
412 | - private function setRelationship($pivotTableName, AbstractTDBMObject $remoteBean, $status) |
|
413 | - { |
|
414 | - $storage = $this->getRelationshipStorage($pivotTableName); |
|
415 | - $storage->attach($remoteBean, ['status' => $status, 'reverse' => false]); |
|
416 | - if ($this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
417 | - $this->_setStatus(TDBMObjectStateEnum::STATE_DIRTY); |
|
418 | - } |
|
419 | - |
|
420 | - $remoteStorage = $remoteBean->getRelationshipStorage($pivotTableName); |
|
421 | - $remoteStorage->attach($this, ['status' => $status, 'reverse' => true]); |
|
422 | - } |
|
423 | - |
|
424 | - /** |
|
425 | - * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
|
426 | - * |
|
427 | - * @param $pivotTableName |
|
428 | - * |
|
429 | - * @return \SplObjectStorage |
|
430 | - */ |
|
431 | - private function getRelationshipStorage($pivotTableName) |
|
432 | - { |
|
433 | - if (isset($this->relationships[$pivotTableName])) { |
|
434 | - $storage = $this->relationships[$pivotTableName]; |
|
435 | - } else { |
|
436 | - $storage = new \SplObjectStorage(); |
|
437 | - $this->relationships[$pivotTableName] = $storage; |
|
438 | - } |
|
439 | - |
|
440 | - return $storage; |
|
441 | - } |
|
442 | - |
|
443 | - /** |
|
444 | - * Reverts any changes made to the object and resumes it to its DB state. |
|
445 | - * This can only be called on objects that come from database and that have not been deleted. |
|
446 | - * Otherwise, this will throw an exception. |
|
447 | - * |
|
448 | - * @throws TDBMException |
|
449 | - */ |
|
450 | - public function discardChanges() |
|
451 | - { |
|
452 | - if ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
453 | - throw new TDBMException("You cannot call discardChanges() on an object that has been created with the 'new' keyword and that has not yet been saved."); |
|
454 | - } |
|
455 | - |
|
456 | - if ($this->status === TDBMObjectStateEnum::STATE_DELETED) { |
|
457 | - throw new TDBMException('You cannot call discardChanges() on an object that has been deleted.'); |
|
458 | - } |
|
459 | - |
|
460 | - $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED); |
|
461 | - } |
|
462 | - |
|
463 | - /** |
|
464 | - * Method used internally by TDBM. You should not use it directly. |
|
465 | - * This method returns the status of the TDBMObject. |
|
466 | - * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
467 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
468 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
469 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
470 | - * |
|
471 | - * @return string |
|
472 | - */ |
|
473 | - public function _getStatus() |
|
474 | - { |
|
475 | - return $this->status; |
|
476 | - } |
|
477 | - |
|
478 | - /** |
|
479 | - * Override the native php clone function for TDBMObjects. |
|
480 | - */ |
|
481 | - public function __clone() |
|
482 | - { |
|
483 | - // Let's clone the many to many relationships |
|
484 | - if ($this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
485 | - $pivotTableList = array_keys($this->relationships); |
|
486 | - } else { |
|
487 | - $pivotTableList = $this->tdbmService->_getPivotTablesLinkedToBean($this); |
|
488 | - } |
|
489 | - |
|
490 | - foreach ($pivotTableList as $pivotTable) { |
|
491 | - $storage = $this->retrieveRelationshipsStorage($pivotTable); |
|
492 | - |
|
493 | - // Let's duplicate the reverse side of the relationship |
|
494 | - foreach ($storage as $remoteBean) { |
|
495 | - $metadata = $storage[$remoteBean]; |
|
496 | - |
|
497 | - $remoteStorage = $remoteBean->getRelationshipStorage($pivotTable); |
|
498 | - $remoteStorage->attach($this, ['status' => $metadata['status'], 'reverse' => !$metadata['reverse']]); |
|
499 | - } |
|
500 | - } |
|
501 | - |
|
502 | - // Let's clone each row |
|
503 | - foreach ($this->dbRows as $key => $dbRow) { |
|
504 | - $this->dbRows[$key] = clone $dbRow; |
|
505 | - } |
|
506 | - |
|
507 | - // Let's set the status to new (to enter the save function) |
|
508 | - $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
509 | - } |
|
510 | - |
|
511 | - /** |
|
512 | - * Returns raw database rows. |
|
513 | - * |
|
514 | - * @return DbRow[] Key: table name, Value: DbRow object |
|
515 | - */ |
|
516 | - public function _getDbRows() |
|
517 | - { |
|
518 | - return $this->dbRows; |
|
519 | - } |
|
520 | - |
|
521 | - private function registerTable($tableName) |
|
522 | - { |
|
523 | - $dbRow = new DbRow($this, $tableName); |
|
524 | - |
|
525 | - if (in_array($this->status, [TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DIRTY])) { |
|
526 | - // Let's get the primary key for the new table |
|
527 | - $anotherDbRow = array_values($this->dbRows)[0]; |
|
528 | - /* @var $anotherDbRow DbRow */ |
|
529 | - $indexedPrimaryKeys = array_values($anotherDbRow->_getPrimaryKeys()); |
|
530 | - $primaryKeys = $this->tdbmService->_getPrimaryKeysFromIndexedPrimaryKeys($tableName, $indexedPrimaryKeys); |
|
531 | - $dbRow->_setPrimaryKeys($primaryKeys); |
|
532 | - } |
|
533 | - |
|
534 | - $dbRow->_setStatus($this->status); |
|
535 | - |
|
536 | - $this->dbRows[$tableName] = $dbRow; |
|
537 | - // TODO: look at status (if not new)=> get primary key from tdbmservice |
|
538 | - } |
|
539 | - |
|
540 | - /** |
|
541 | - * Internal function: return the list of relationships. |
|
542 | - * |
|
543 | - * @return \SplObjectStorage[] |
|
544 | - */ |
|
545 | - public function _getCachedRelationships() |
|
546 | - { |
|
547 | - return $this->relationships; |
|
548 | - } |
|
32 | + /** |
|
33 | + * The service this object is bound to. |
|
34 | + * |
|
35 | + * @var TDBMService |
|
36 | + */ |
|
37 | + protected $tdbmService; |
|
38 | + |
|
39 | + /** |
|
40 | + * An array of DbRow, indexed by table name. |
|
41 | + * |
|
42 | + * @var DbRow[] |
|
43 | + */ |
|
44 | + protected $dbRows = array(); |
|
45 | + |
|
46 | + /** |
|
47 | + * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
48 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
49 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
50 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
51 | + * |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + private $status; |
|
55 | + |
|
56 | + /** |
|
57 | + * Array storing beans related via many to many relationships (pivot tables). |
|
58 | + * |
|
59 | + * @var \SplObjectStorage[] Key: pivot table name, value: SplObjectStorage |
|
60 | + */ |
|
61 | + private $relationships = []; |
|
62 | + |
|
63 | + /** |
|
64 | + * @var bool[] Key: pivot table name, value: whether a query was performed to load the data. |
|
65 | + */ |
|
66 | + private $loadedRelationships = []; |
|
67 | + |
|
68 | + /** |
|
69 | + * Used with $primaryKeys when we want to retrieve an existing object |
|
70 | + * and $primaryKeys=[] if we want a new object. |
|
71 | + * |
|
72 | + * @param string $tableName |
|
73 | + * @param array $primaryKeys |
|
74 | + * @param TDBMService $tdbmService |
|
75 | + * |
|
76 | + * @throws TDBMException |
|
77 | + * @throws TDBMInvalidOperationException |
|
78 | + */ |
|
79 | + public function __construct($tableName = null, array $primaryKeys = array(), TDBMService $tdbmService = null) |
|
80 | + { |
|
81 | + // FIXME: lazy loading should be forbidden on tables with inheritance and dynamic type assignation... |
|
82 | + if (!empty($tableName)) { |
|
83 | + $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService); |
|
84 | + } |
|
85 | + |
|
86 | + if ($tdbmService === null) { |
|
87 | + $this->_setStatus(TDBMObjectStateEnum::STATE_DETACHED); |
|
88 | + } else { |
|
89 | + $this->_attach($tdbmService); |
|
90 | + if (!empty($primaryKeys)) { |
|
91 | + $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED); |
|
92 | + } else { |
|
93 | + $this->_setStatus(TDBMObjectStateEnum::STATE_NEW); |
|
94 | + } |
|
95 | + } |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Alternative constructor called when data is fetched from database via a SELECT. |
|
100 | + * |
|
101 | + * @param array $beanData array<table, array<column, value>> |
|
102 | + * @param TDBMService $tdbmService |
|
103 | + */ |
|
104 | + public function _constructFromData(array $beanData, TDBMService $tdbmService) |
|
105 | + { |
|
106 | + $this->tdbmService = $tdbmService; |
|
107 | + |
|
108 | + foreach ($beanData as $table => $columns) { |
|
109 | + $this->dbRows[$table] = new DbRow($this, $table, $tdbmService->_getPrimaryKeysFromObjectData($table, $columns), $tdbmService, $columns); |
|
110 | + } |
|
111 | + |
|
112 | + $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Alternative constructor called when bean is lazily loaded. |
|
117 | + * |
|
118 | + * @param string $tableName |
|
119 | + * @param array $primaryKeys |
|
120 | + * @param TDBMService $tdbmService |
|
121 | + */ |
|
122 | + public function _constructLazy($tableName, array $primaryKeys, TDBMService $tdbmService) |
|
123 | + { |
|
124 | + $this->tdbmService = $tdbmService; |
|
125 | + |
|
126 | + $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService); |
|
127 | + |
|
128 | + $this->status = TDBMObjectStateEnum::STATE_NOT_LOADED; |
|
129 | + } |
|
130 | + |
|
131 | + public function _attach(TDBMService $tdbmService) |
|
132 | + { |
|
133 | + if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) { |
|
134 | + throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.'); |
|
135 | + } |
|
136 | + $this->tdbmService = $tdbmService; |
|
137 | + |
|
138 | + // If we attach this object, we must work to make sure the tables are in ascending order (from low level to top level) |
|
139 | + $tableNames = array_keys($this->dbRows); |
|
140 | + $tableNames = $this->tdbmService->_getLinkBetweenInheritedTables($tableNames); |
|
141 | + $tableNames = array_reverse($tableNames); |
|
142 | + |
|
143 | + $newDbRows = []; |
|
144 | + |
|
145 | + foreach ($tableNames as $table) { |
|
146 | + if (!isset($this->dbRows[$table])) { |
|
147 | + $this->registerTable($table); |
|
148 | + } |
|
149 | + $newDbRows[$table] = $this->dbRows[$table]; |
|
150 | + } |
|
151 | + $this->dbRows = $newDbRows; |
|
152 | + |
|
153 | + $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
154 | + foreach ($this->dbRows as $dbRow) { |
|
155 | + $dbRow->_attach($tdbmService); |
|
156 | + } |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Sets the state of the TDBM Object |
|
161 | + * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
162 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
163 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
164 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
165 | + * |
|
166 | + * @param string $state |
|
167 | + */ |
|
168 | + public function _setStatus($state) |
|
169 | + { |
|
170 | + $this->status = $state; |
|
171 | + |
|
172 | + // TODO: we might ignore the loaded => dirty state here! dirty status comes from the db_row itself. |
|
173 | + foreach ($this->dbRows as $dbRow) { |
|
174 | + $dbRow->_setStatus($state); |
|
175 | + } |
|
176 | + } |
|
177 | + |
|
178 | + public function get($var, $tableName = null) |
|
179 | + { |
|
180 | + if ($tableName === null) { |
|
181 | + if (count($this->dbRows) > 1) { |
|
182 | + throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
183 | + } elseif (count($this->dbRows) === 1) { |
|
184 | + $tableName = array_keys($this->dbRows)[0]; |
|
185 | + } |
|
186 | + } |
|
187 | + |
|
188 | + if (!isset($this->dbRows[$tableName])) { |
|
189 | + if (count($this->dbRows[$tableName] === 0)) { |
|
190 | + throw new TDBMException('Object is not yet bound to any table.'); |
|
191 | + } else { |
|
192 | + throw new TDBMException('Unknown table "'.$tableName.'"" in object.'); |
|
193 | + } |
|
194 | + } |
|
195 | + |
|
196 | + return $this->dbRows[$tableName]->get($var); |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Returns true if a column is set, false otherwise. |
|
201 | + * |
|
202 | + * @param string $var |
|
203 | + * |
|
204 | + * @return bool |
|
205 | + */ |
|
206 | + public function has($var, $tableName = null) |
|
207 | + { |
|
208 | + if ($tableName === null) { |
|
209 | + if (count($this->dbRows) > 1) { |
|
210 | + throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
211 | + } elseif (count($this->dbRows) === 1) { |
|
212 | + $tableName = array_keys($this->dbRows)[0]; |
|
213 | + } |
|
214 | + } |
|
215 | + |
|
216 | + if (!isset($this->dbRows[$tableName])) { |
|
217 | + if (count($this->dbRows[$tableName] === 0)) { |
|
218 | + throw new TDBMException('Object is not yet bound to any table.'); |
|
219 | + } else { |
|
220 | + throw new TDBMException('Unknown table "'.$tableName.'"" in object.'); |
|
221 | + } |
|
222 | + } |
|
223 | + |
|
224 | + return $this->dbRows[$tableName]->has($var); |
|
225 | + } |
|
226 | + |
|
227 | + public function set($var, $value, $tableName = null) |
|
228 | + { |
|
229 | + if ($tableName === null) { |
|
230 | + if (count($this->dbRows) > 1) { |
|
231 | + throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
232 | + } elseif (count($this->dbRows) === 1) { |
|
233 | + $tableName = array_keys($this->dbRows)[0]; |
|
234 | + } else { |
|
235 | + throw new TDBMException('Please specify a table for this object.'); |
|
236 | + } |
|
237 | + } |
|
238 | + |
|
239 | + if (!isset($this->dbRows[$tableName])) { |
|
240 | + $this->registerTable($tableName); |
|
241 | + } |
|
242 | + |
|
243 | + $this->dbRows[$tableName]->set($var, $value); |
|
244 | + if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
245 | + $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
246 | + } |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * @param string $foreignKeyName |
|
251 | + * @param AbstractTDBMObject $bean |
|
252 | + */ |
|
253 | + public function setRef($foreignKeyName, AbstractTDBMObject $bean, $tableName = null) |
|
254 | + { |
|
255 | + if ($tableName === null) { |
|
256 | + if (count($this->dbRows) > 1) { |
|
257 | + throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
258 | + } elseif (count($this->dbRows) === 1) { |
|
259 | + $tableName = array_keys($this->dbRows)[0]; |
|
260 | + } else { |
|
261 | + throw new TDBMException('Please specify a table for this object.'); |
|
262 | + } |
|
263 | + } |
|
264 | + |
|
265 | + if (!isset($this->dbRows[$tableName])) { |
|
266 | + $this->registerTable($tableName); |
|
267 | + } |
|
268 | + |
|
269 | + $this->dbRows[$tableName]->setRef($foreignKeyName, $bean); |
|
270 | + if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
271 | + $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
272 | + } |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * @param string $foreignKeyName A unique name for this reference |
|
277 | + * |
|
278 | + * @return AbstractTDBMObject|null |
|
279 | + */ |
|
280 | + public function getRef($foreignKeyName, $tableName = null) |
|
281 | + { |
|
282 | + if ($tableName === null) { |
|
283 | + if (count($this->dbRows) > 1) { |
|
284 | + throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
285 | + } elseif (count($this->dbRows) === 1) { |
|
286 | + $tableName = array_keys($this->dbRows)[0]; |
|
287 | + } |
|
288 | + } |
|
289 | + |
|
290 | + if (!isset($this->dbRows[$tableName])) { |
|
291 | + if (count($this->dbRows[$tableName] === 0)) { |
|
292 | + throw new TDBMException('Object is not yet bound to any table.'); |
|
293 | + } else { |
|
294 | + throw new TDBMException('Unknown table "'.$tableName.'"" in object.'); |
|
295 | + } |
|
296 | + } |
|
297 | + |
|
298 | + return $this->dbRows[$tableName]->getRef($foreignKeyName); |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * Adds a many to many relationship to this bean. |
|
303 | + * |
|
304 | + * @param string $pivotTableName |
|
305 | + * @param AbstractTDBMObject $remoteBean |
|
306 | + */ |
|
307 | + protected function addRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
308 | + { |
|
309 | + $this->setRelationship($pivotTableName, $remoteBean, 'new'); |
|
310 | + } |
|
311 | + |
|
312 | + /** |
|
313 | + * Returns true if there is a relationship to this bean. |
|
314 | + * |
|
315 | + * @param string $pivotTableName |
|
316 | + * @param AbstractTDBMObject $remoteBean |
|
317 | + * |
|
318 | + * @return bool |
|
319 | + */ |
|
320 | + protected function hasRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
321 | + { |
|
322 | + $storage = $this->retrieveRelationshipsStorage($pivotTableName); |
|
323 | + |
|
324 | + if ($storage->contains($remoteBean)) { |
|
325 | + if ($storage[$remoteBean]['status'] !== 'delete') { |
|
326 | + return true; |
|
327 | + } |
|
328 | + } |
|
329 | + |
|
330 | + return false; |
|
331 | + } |
|
332 | + |
|
333 | + /** |
|
334 | + * Internal TDBM method. Removes a many to many relationship from this bean. |
|
335 | + * |
|
336 | + * @param string $pivotTableName |
|
337 | + * @param AbstractTDBMObject $remoteBean |
|
338 | + */ |
|
339 | + public function _removeRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
340 | + { |
|
341 | + if (isset($this->relationships[$pivotTableName][$remoteBean]) && $this->relationships[$pivotTableName][$remoteBean]['status'] === 'new') { |
|
342 | + unset($this->relationships[$pivotTableName][$remoteBean]); |
|
343 | + unset($remoteBean->relationships[$pivotTableName][$this]); |
|
344 | + } else { |
|
345 | + $this->setRelationship($pivotTableName, $remoteBean, 'delete'); |
|
346 | + } |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * Returns the list of objects linked to this bean via $pivotTableName. |
|
351 | + * |
|
352 | + * @param $pivotTableName |
|
353 | + * |
|
354 | + * @return \SplObjectStorage |
|
355 | + */ |
|
356 | + private function retrieveRelationshipsStorage($pivotTableName) |
|
357 | + { |
|
358 | + $storage = $this->getRelationshipStorage($pivotTableName); |
|
359 | + if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || isset($this->loadedRelationships[$pivotTableName]) && $this->loadedRelationships[$pivotTableName]) { |
|
360 | + return $storage; |
|
361 | + } |
|
362 | + |
|
363 | + $beans = $this->tdbmService->_getRelatedBeans($pivotTableName, $this); |
|
364 | + $this->loadedRelationships[$pivotTableName] = true; |
|
365 | + |
|
366 | + foreach ($beans as $bean) { |
|
367 | + if (isset($storage[$bean])) { |
|
368 | + $oldStatus = $storage[$bean]['status']; |
|
369 | + if ($oldStatus === 'delete') { |
|
370 | + // Keep deleted things deleted |
|
371 | + continue; |
|
372 | + } |
|
373 | + } |
|
374 | + $this->setRelationship($pivotTableName, $bean, 'loaded'); |
|
375 | + } |
|
376 | + |
|
377 | + return $storage; |
|
378 | + } |
|
379 | + |
|
380 | + /** |
|
381 | + * Internal TDBM method. Returns the list of objects linked to this bean via $pivotTableName. |
|
382 | + * |
|
383 | + * @param $pivotTableName |
|
384 | + * |
|
385 | + * @return AbstractTDBMObject[] |
|
386 | + */ |
|
387 | + public function _getRelationships($pivotTableName) |
|
388 | + { |
|
389 | + return $this->relationshipStorageToArray($this->retrieveRelationshipsStorage($pivotTableName)); |
|
390 | + } |
|
391 | + |
|
392 | + private function relationshipStorageToArray(\SplObjectStorage $storage) |
|
393 | + { |
|
394 | + $beans = []; |
|
395 | + foreach ($storage as $bean) { |
|
396 | + $statusArr = $storage[$bean]; |
|
397 | + if ($statusArr['status'] !== 'delete') { |
|
398 | + $beans[] = $bean; |
|
399 | + } |
|
400 | + } |
|
401 | + |
|
402 | + return $beans; |
|
403 | + } |
|
404 | + |
|
405 | + /** |
|
406 | + * Declares a relationship between. |
|
407 | + * |
|
408 | + * @param string $pivotTableName |
|
409 | + * @param AbstractTDBMObject $remoteBean |
|
410 | + * @param string $status |
|
411 | + */ |
|
412 | + private function setRelationship($pivotTableName, AbstractTDBMObject $remoteBean, $status) |
|
413 | + { |
|
414 | + $storage = $this->getRelationshipStorage($pivotTableName); |
|
415 | + $storage->attach($remoteBean, ['status' => $status, 'reverse' => false]); |
|
416 | + if ($this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
417 | + $this->_setStatus(TDBMObjectStateEnum::STATE_DIRTY); |
|
418 | + } |
|
419 | + |
|
420 | + $remoteStorage = $remoteBean->getRelationshipStorage($pivotTableName); |
|
421 | + $remoteStorage->attach($this, ['status' => $status, 'reverse' => true]); |
|
422 | + } |
|
423 | + |
|
424 | + /** |
|
425 | + * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
|
426 | + * |
|
427 | + * @param $pivotTableName |
|
428 | + * |
|
429 | + * @return \SplObjectStorage |
|
430 | + */ |
|
431 | + private function getRelationshipStorage($pivotTableName) |
|
432 | + { |
|
433 | + if (isset($this->relationships[$pivotTableName])) { |
|
434 | + $storage = $this->relationships[$pivotTableName]; |
|
435 | + } else { |
|
436 | + $storage = new \SplObjectStorage(); |
|
437 | + $this->relationships[$pivotTableName] = $storage; |
|
438 | + } |
|
439 | + |
|
440 | + return $storage; |
|
441 | + } |
|
442 | + |
|
443 | + /** |
|
444 | + * Reverts any changes made to the object and resumes it to its DB state. |
|
445 | + * This can only be called on objects that come from database and that have not been deleted. |
|
446 | + * Otherwise, this will throw an exception. |
|
447 | + * |
|
448 | + * @throws TDBMException |
|
449 | + */ |
|
450 | + public function discardChanges() |
|
451 | + { |
|
452 | + if ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
453 | + throw new TDBMException("You cannot call discardChanges() on an object that has been created with the 'new' keyword and that has not yet been saved."); |
|
454 | + } |
|
455 | + |
|
456 | + if ($this->status === TDBMObjectStateEnum::STATE_DELETED) { |
|
457 | + throw new TDBMException('You cannot call discardChanges() on an object that has been deleted.'); |
|
458 | + } |
|
459 | + |
|
460 | + $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED); |
|
461 | + } |
|
462 | + |
|
463 | + /** |
|
464 | + * Method used internally by TDBM. You should not use it directly. |
|
465 | + * This method returns the status of the TDBMObject. |
|
466 | + * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
467 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
468 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
469 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
470 | + * |
|
471 | + * @return string |
|
472 | + */ |
|
473 | + public function _getStatus() |
|
474 | + { |
|
475 | + return $this->status; |
|
476 | + } |
|
477 | + |
|
478 | + /** |
|
479 | + * Override the native php clone function for TDBMObjects. |
|
480 | + */ |
|
481 | + public function __clone() |
|
482 | + { |
|
483 | + // Let's clone the many to many relationships |
|
484 | + if ($this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
485 | + $pivotTableList = array_keys($this->relationships); |
|
486 | + } else { |
|
487 | + $pivotTableList = $this->tdbmService->_getPivotTablesLinkedToBean($this); |
|
488 | + } |
|
489 | + |
|
490 | + foreach ($pivotTableList as $pivotTable) { |
|
491 | + $storage = $this->retrieveRelationshipsStorage($pivotTable); |
|
492 | + |
|
493 | + // Let's duplicate the reverse side of the relationship |
|
494 | + foreach ($storage as $remoteBean) { |
|
495 | + $metadata = $storage[$remoteBean]; |
|
496 | + |
|
497 | + $remoteStorage = $remoteBean->getRelationshipStorage($pivotTable); |
|
498 | + $remoteStorage->attach($this, ['status' => $metadata['status'], 'reverse' => !$metadata['reverse']]); |
|
499 | + } |
|
500 | + } |
|
501 | + |
|
502 | + // Let's clone each row |
|
503 | + foreach ($this->dbRows as $key => $dbRow) { |
|
504 | + $this->dbRows[$key] = clone $dbRow; |
|
505 | + } |
|
506 | + |
|
507 | + // Let's set the status to new (to enter the save function) |
|
508 | + $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
509 | + } |
|
510 | + |
|
511 | + /** |
|
512 | + * Returns raw database rows. |
|
513 | + * |
|
514 | + * @return DbRow[] Key: table name, Value: DbRow object |
|
515 | + */ |
|
516 | + public function _getDbRows() |
|
517 | + { |
|
518 | + return $this->dbRows; |
|
519 | + } |
|
520 | + |
|
521 | + private function registerTable($tableName) |
|
522 | + { |
|
523 | + $dbRow = new DbRow($this, $tableName); |
|
524 | + |
|
525 | + if (in_array($this->status, [TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DIRTY])) { |
|
526 | + // Let's get the primary key for the new table |
|
527 | + $anotherDbRow = array_values($this->dbRows)[0]; |
|
528 | + /* @var $anotherDbRow DbRow */ |
|
529 | + $indexedPrimaryKeys = array_values($anotherDbRow->_getPrimaryKeys()); |
|
530 | + $primaryKeys = $this->tdbmService->_getPrimaryKeysFromIndexedPrimaryKeys($tableName, $indexedPrimaryKeys); |
|
531 | + $dbRow->_setPrimaryKeys($primaryKeys); |
|
532 | + } |
|
533 | + |
|
534 | + $dbRow->_setStatus($this->status); |
|
535 | + |
|
536 | + $this->dbRows[$tableName] = $dbRow; |
|
537 | + // TODO: look at status (if not new)=> get primary key from tdbmservice |
|
538 | + } |
|
539 | + |
|
540 | + /** |
|
541 | + * Internal function: return the list of relationships. |
|
542 | + * |
|
543 | + * @return \SplObjectStorage[] |
|
544 | + */ |
|
545 | + public function _getCachedRelationships() |
|
546 | + { |
|
547 | + return $this->relationships; |
|
548 | + } |
|
549 | 549 | } |
@@ -26,77 +26,77 @@ |
||
26 | 26 | */ |
27 | 27 | class TDBMObjectArray extends \ArrayObject implements \JsonSerializable |
28 | 28 | { |
29 | - public function __get($var) |
|
30 | - { |
|
31 | - $cnt = count($this); |
|
32 | - if ($cnt == 1) { |
|
33 | - return $this[0]->__get($var); |
|
34 | - } elseif ($cnt > 1) { |
|
35 | - throw new TDBMException('Array contains many objects! Use getarray_'.$var.' to retrieve an array of '.$var); |
|
36 | - } else { |
|
37 | - throw new TDBMException('Array contains no objects'); |
|
38 | - } |
|
39 | - } |
|
29 | + public function __get($var) |
|
30 | + { |
|
31 | + $cnt = count($this); |
|
32 | + if ($cnt == 1) { |
|
33 | + return $this[0]->__get($var); |
|
34 | + } elseif ($cnt > 1) { |
|
35 | + throw new TDBMException('Array contains many objects! Use getarray_'.$var.' to retrieve an array of '.$var); |
|
36 | + } else { |
|
37 | + throw new TDBMException('Array contains no objects'); |
|
38 | + } |
|
39 | + } |
|
40 | 40 | |
41 | - public function __set($var, $value) |
|
42 | - { |
|
43 | - $cnt = count($this); |
|
44 | - if ($cnt == 1) { |
|
45 | - return $this[0]->__set($var, $value); |
|
46 | - } elseif ($cnt > 1) { |
|
47 | - throw new TDBMException('Array contains many objects! Use setarray_'.$var.' to set the array of '.$var); |
|
48 | - } else { |
|
49 | - throw new TDBMException('Array contains no objects'); |
|
50 | - } |
|
51 | - } |
|
41 | + public function __set($var, $value) |
|
42 | + { |
|
43 | + $cnt = count($this); |
|
44 | + if ($cnt == 1) { |
|
45 | + return $this[0]->__set($var, $value); |
|
46 | + } elseif ($cnt > 1) { |
|
47 | + throw new TDBMException('Array contains many objects! Use setarray_'.$var.' to set the array of '.$var); |
|
48 | + } else { |
|
49 | + throw new TDBMException('Array contains no objects'); |
|
50 | + } |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * getarray_column_name returns an array containing the values of the column of the given objects. |
|
55 | - * setarray_column_name sets the value of the given column for all the objects. |
|
56 | - * |
|
57 | - * @param string $func_name |
|
58 | - * @param $values |
|
59 | - * |
|
60 | - * @return array|void |
|
61 | - * |
|
62 | - * @throws TDBMException |
|
63 | - */ |
|
64 | - public function __call($func_name, $values) |
|
65 | - { |
|
66 | - if (strpos($func_name, 'getarray_') === 0) { |
|
67 | - $column = substr($func_name, 9); |
|
53 | + /** |
|
54 | + * getarray_column_name returns an array containing the values of the column of the given objects. |
|
55 | + * setarray_column_name sets the value of the given column for all the objects. |
|
56 | + * |
|
57 | + * @param string $func_name |
|
58 | + * @param $values |
|
59 | + * |
|
60 | + * @return array|void |
|
61 | + * |
|
62 | + * @throws TDBMException |
|
63 | + */ |
|
64 | + public function __call($func_name, $values) |
|
65 | + { |
|
66 | + if (strpos($func_name, 'getarray_') === 0) { |
|
67 | + $column = substr($func_name, 9); |
|
68 | 68 | |
69 | - return $this->getarray($column); |
|
70 | - } elseif (strpos($func_name, 'setarray_') === 0) { |
|
71 | - $column = substr($func_name, 9); |
|
69 | + return $this->getarray($column); |
|
70 | + } elseif (strpos($func_name, 'setarray_') === 0) { |
|
71 | + $column = substr($func_name, 9); |
|
72 | 72 | |
73 | - return $this->setarray($column, $values[0]); |
|
74 | - } elseif (count($this) == 1) { |
|
75 | - $this[0]->__call($func_name, $values); |
|
76 | - } else { |
|
77 | - throw new TDBMException('Method '.$func_name.' not found'); |
|
78 | - } |
|
79 | - } |
|
73 | + return $this->setarray($column, $values[0]); |
|
74 | + } elseif (count($this) == 1) { |
|
75 | + $this[0]->__call($func_name, $values); |
|
76 | + } else { |
|
77 | + throw new TDBMException('Method '.$func_name.' not found'); |
|
78 | + } |
|
79 | + } |
|
80 | 80 | |
81 | - private function getarray($column) |
|
82 | - { |
|
83 | - $arr = array(); |
|
84 | - foreach ($this as $object) { |
|
85 | - $arr[] = $object->__get($column); |
|
86 | - } |
|
81 | + private function getarray($column) |
|
82 | + { |
|
83 | + $arr = array(); |
|
84 | + foreach ($this as $object) { |
|
85 | + $arr[] = $object->__get($column); |
|
86 | + } |
|
87 | 87 | |
88 | - return $arr; |
|
89 | - } |
|
88 | + return $arr; |
|
89 | + } |
|
90 | 90 | |
91 | - private function setarray($column, $value) |
|
92 | - { |
|
93 | - foreach ($this as $object) { |
|
94 | - $object->__set($column, $value); |
|
95 | - } |
|
96 | - } |
|
91 | + private function setarray($column, $value) |
|
92 | + { |
|
93 | + foreach ($this as $object) { |
|
94 | + $object->__set($column, $value); |
|
95 | + } |
|
96 | + } |
|
97 | 97 | |
98 | - public function jsonSerialize() |
|
99 | - { |
|
100 | - return (array) $this; |
|
101 | - } |
|
98 | + public function jsonSerialize() |
|
99 | + { |
|
100 | + return (array) $this; |
|
101 | + } |
|
102 | 102 | } |
@@ -29,230 +29,230 @@ |
||
29 | 29 | */ |
30 | 30 | class PageIterator implements Page, \ArrayAccess, \JsonSerializable |
31 | 31 | { |
32 | - /** |
|
33 | - * @var Statement |
|
34 | - */ |
|
35 | - protected $statement; |
|
32 | + /** |
|
33 | + * @var Statement |
|
34 | + */ |
|
35 | + protected $statement; |
|
36 | 36 | |
37 | - protected $fetchStarted = false; |
|
38 | - private $objectStorage; |
|
39 | - private $className; |
|
37 | + protected $fetchStarted = false; |
|
38 | + private $objectStorage; |
|
39 | + private $className; |
|
40 | 40 | |
41 | - private $parentResult; |
|
42 | - private $tdbmService; |
|
43 | - private $magicSql; |
|
44 | - private $parameters; |
|
45 | - private $limit; |
|
46 | - private $offset; |
|
47 | - private $columnDescriptors; |
|
48 | - private $magicQuery; |
|
41 | + private $parentResult; |
|
42 | + private $tdbmService; |
|
43 | + private $magicSql; |
|
44 | + private $parameters; |
|
45 | + private $limit; |
|
46 | + private $offset; |
|
47 | + private $columnDescriptors; |
|
48 | + private $magicQuery; |
|
49 | 49 | |
50 | - /** |
|
51 | - * The key of the current retrieved object. |
|
52 | - * |
|
53 | - * @var int |
|
54 | - */ |
|
55 | - protected $key = -1; |
|
50 | + /** |
|
51 | + * The key of the current retrieved object. |
|
52 | + * |
|
53 | + * @var int |
|
54 | + */ |
|
55 | + protected $key = -1; |
|
56 | 56 | |
57 | - protected $current = null; |
|
57 | + protected $current = null; |
|
58 | 58 | |
59 | - private $databasePlatform; |
|
59 | + private $databasePlatform; |
|
60 | 60 | |
61 | - private $innerResultIterator; |
|
61 | + private $innerResultIterator; |
|
62 | 62 | |
63 | - public function __construct(ResultIterator $parentResult, $magicSql, array $parameters, $limit, $offset, array $columnDescriptors, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, $mode) |
|
64 | - { |
|
65 | - $this->parentResult = $parentResult; |
|
66 | - $this->magicSql = $magicSql; |
|
67 | - $this->objectStorage = $objectStorage; |
|
68 | - $this->className = $className; |
|
69 | - $this->tdbmService = $tdbmService; |
|
70 | - $this->parameters = $parameters; |
|
71 | - $this->limit = $limit; |
|
72 | - $this->offset = $offset; |
|
73 | - $this->columnDescriptors = $columnDescriptors; |
|
74 | - $this->magicQuery = $magicQuery; |
|
75 | - $this->databasePlatform = $this->tdbmService->getConnection()->getDatabasePlatform(); |
|
76 | - $this->mode = $mode; |
|
77 | - } |
|
63 | + public function __construct(ResultIterator $parentResult, $magicSql, array $parameters, $limit, $offset, array $columnDescriptors, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, $mode) |
|
64 | + { |
|
65 | + $this->parentResult = $parentResult; |
|
66 | + $this->magicSql = $magicSql; |
|
67 | + $this->objectStorage = $objectStorage; |
|
68 | + $this->className = $className; |
|
69 | + $this->tdbmService = $tdbmService; |
|
70 | + $this->parameters = $parameters; |
|
71 | + $this->limit = $limit; |
|
72 | + $this->offset = $offset; |
|
73 | + $this->columnDescriptors = $columnDescriptors; |
|
74 | + $this->magicQuery = $magicQuery; |
|
75 | + $this->databasePlatform = $this->tdbmService->getConnection()->getDatabasePlatform(); |
|
76 | + $this->mode = $mode; |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Retrieve an external iterator. |
|
81 | - * |
|
82 | - * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
|
83 | - * |
|
84 | - * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or |
|
85 | - * <b>Traversable</b> |
|
86 | - * |
|
87 | - * @since 5.0.0 |
|
88 | - */ |
|
89 | - public function getIterator() |
|
90 | - { |
|
91 | - if ($this->innerResultIterator === null) { |
|
92 | - if ($this->mode === TDBMService::MODE_CURSOR) { |
|
93 | - $this->innerResultIterator = new InnerResultIterator($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery); |
|
94 | - } else { |
|
95 | - $this->innerResultIterator = new InnerResultArray($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery); |
|
96 | - } |
|
97 | - } |
|
79 | + /** |
|
80 | + * Retrieve an external iterator. |
|
81 | + * |
|
82 | + * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
|
83 | + * |
|
84 | + * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or |
|
85 | + * <b>Traversable</b> |
|
86 | + * |
|
87 | + * @since 5.0.0 |
|
88 | + */ |
|
89 | + public function getIterator() |
|
90 | + { |
|
91 | + if ($this->innerResultIterator === null) { |
|
92 | + if ($this->mode === TDBMService::MODE_CURSOR) { |
|
93 | + $this->innerResultIterator = new InnerResultIterator($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery); |
|
94 | + } else { |
|
95 | + $this->innerResultIterator = new InnerResultArray($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery); |
|
96 | + } |
|
97 | + } |
|
98 | 98 | |
99 | - return $this->innerResultIterator; |
|
100 | - } |
|
99 | + return $this->innerResultIterator; |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * @return int |
|
104 | - */ |
|
105 | - public function getCurrentOffset() |
|
106 | - { |
|
107 | - return $this->offset; |
|
108 | - } |
|
102 | + /** |
|
103 | + * @return int |
|
104 | + */ |
|
105 | + public function getCurrentOffset() |
|
106 | + { |
|
107 | + return $this->offset; |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * @return int |
|
112 | - */ |
|
113 | - public function getCurrentPage() |
|
114 | - { |
|
115 | - return floor($this->offset / $this->limit) + 1; |
|
116 | - } |
|
110 | + /** |
|
111 | + * @return int |
|
112 | + */ |
|
113 | + public function getCurrentPage() |
|
114 | + { |
|
115 | + return floor($this->offset / $this->limit) + 1; |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * @return int |
|
120 | - */ |
|
121 | - public function getCurrentLimit() |
|
122 | - { |
|
123 | - return $this->limit; |
|
124 | - } |
|
118 | + /** |
|
119 | + * @return int |
|
120 | + */ |
|
121 | + public function getCurrentLimit() |
|
122 | + { |
|
123 | + return $this->limit; |
|
124 | + } |
|
125 | 125 | |
126 | - /** |
|
127 | - * Return the number of results on the current page of the {@link Result}. |
|
128 | - * |
|
129 | - * @return int |
|
130 | - */ |
|
131 | - public function count() |
|
132 | - { |
|
133 | - return $this->getIterator()->count(); |
|
134 | - } |
|
126 | + /** |
|
127 | + * Return the number of results on the current page of the {@link Result}. |
|
128 | + * |
|
129 | + * @return int |
|
130 | + */ |
|
131 | + public function count() |
|
132 | + { |
|
133 | + return $this->getIterator()->count(); |
|
134 | + } |
|
135 | 135 | |
136 | - /** |
|
137 | - * Return the number of ALL results in the paginatable of {@link Result}. |
|
138 | - * |
|
139 | - * @return int |
|
140 | - */ |
|
141 | - public function totalCount() |
|
142 | - { |
|
143 | - return $this->parentResult->count(); |
|
144 | - } |
|
136 | + /** |
|
137 | + * Return the number of ALL results in the paginatable of {@link Result}. |
|
138 | + * |
|
139 | + * @return int |
|
140 | + */ |
|
141 | + public function totalCount() |
|
142 | + { |
|
143 | + return $this->parentResult->count(); |
|
144 | + } |
|
145 | 145 | |
146 | - /** |
|
147 | - * Casts the result set to a PHP array. |
|
148 | - * |
|
149 | - * @return array |
|
150 | - */ |
|
151 | - public function toArray() |
|
152 | - { |
|
153 | - return iterator_to_array($this->getIterator()); |
|
154 | - } |
|
146 | + /** |
|
147 | + * Casts the result set to a PHP array. |
|
148 | + * |
|
149 | + * @return array |
|
150 | + */ |
|
151 | + public function toArray() |
|
152 | + { |
|
153 | + return iterator_to_array($this->getIterator()); |
|
154 | + } |
|
155 | 155 | |
156 | - /** |
|
157 | - * Returns a new iterator mapping any call using the $callable function. |
|
158 | - * |
|
159 | - * @param callable $callable |
|
160 | - * |
|
161 | - * @return MapIterator |
|
162 | - */ |
|
163 | - public function map(callable $callable) |
|
164 | - { |
|
165 | - return new MapIterator($this->getIterator(), $callable); |
|
166 | - } |
|
156 | + /** |
|
157 | + * Returns a new iterator mapping any call using the $callable function. |
|
158 | + * |
|
159 | + * @param callable $callable |
|
160 | + * |
|
161 | + * @return MapIterator |
|
162 | + */ |
|
163 | + public function map(callable $callable) |
|
164 | + { |
|
165 | + return new MapIterator($this->getIterator(), $callable); |
|
166 | + } |
|
167 | 167 | |
168 | - /** |
|
169 | - * Whether a offset exists. |
|
170 | - * |
|
171 | - * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
172 | - * |
|
173 | - * @param mixed $offset <p> |
|
174 | - * An offset to check for. |
|
175 | - * </p> |
|
176 | - * |
|
177 | - * @return bool true on success or false on failure. |
|
178 | - * </p> |
|
179 | - * <p> |
|
180 | - * The return value will be casted to boolean if non-boolean was returned. |
|
181 | - * |
|
182 | - * @since 5.0.0 |
|
183 | - */ |
|
184 | - public function offsetExists($offset) |
|
185 | - { |
|
186 | - return $this->getIterator()->offsetExists($offset); |
|
187 | - } |
|
168 | + /** |
|
169 | + * Whether a offset exists. |
|
170 | + * |
|
171 | + * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
172 | + * |
|
173 | + * @param mixed $offset <p> |
|
174 | + * An offset to check for. |
|
175 | + * </p> |
|
176 | + * |
|
177 | + * @return bool true on success or false on failure. |
|
178 | + * </p> |
|
179 | + * <p> |
|
180 | + * The return value will be casted to boolean if non-boolean was returned. |
|
181 | + * |
|
182 | + * @since 5.0.0 |
|
183 | + */ |
|
184 | + public function offsetExists($offset) |
|
185 | + { |
|
186 | + return $this->getIterator()->offsetExists($offset); |
|
187 | + } |
|
188 | 188 | |
189 | - /** |
|
190 | - * Offset to retrieve. |
|
191 | - * |
|
192 | - * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
193 | - * |
|
194 | - * @param mixed $offset <p> |
|
195 | - * The offset to retrieve. |
|
196 | - * </p> |
|
197 | - * |
|
198 | - * @return mixed Can return all value types. |
|
199 | - * |
|
200 | - * @since 5.0.0 |
|
201 | - */ |
|
202 | - public function offsetGet($offset) |
|
203 | - { |
|
204 | - return $this->getIterator()->offsetGet($offset); |
|
205 | - } |
|
189 | + /** |
|
190 | + * Offset to retrieve. |
|
191 | + * |
|
192 | + * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
193 | + * |
|
194 | + * @param mixed $offset <p> |
|
195 | + * The offset to retrieve. |
|
196 | + * </p> |
|
197 | + * |
|
198 | + * @return mixed Can return all value types. |
|
199 | + * |
|
200 | + * @since 5.0.0 |
|
201 | + */ |
|
202 | + public function offsetGet($offset) |
|
203 | + { |
|
204 | + return $this->getIterator()->offsetGet($offset); |
|
205 | + } |
|
206 | 206 | |
207 | - /** |
|
208 | - * Offset to set. |
|
209 | - * |
|
210 | - * @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
211 | - * |
|
212 | - * @param mixed $offset <p> |
|
213 | - * The offset to assign the value to. |
|
214 | - * </p> |
|
215 | - * @param mixed $value <p> |
|
216 | - * The value to set. |
|
217 | - * </p> |
|
218 | - * |
|
219 | - * @since 5.0.0 |
|
220 | - */ |
|
221 | - public function offsetSet($offset, $value) |
|
222 | - { |
|
223 | - return $this->getIterator()->offsetSet($offset, $value); |
|
224 | - } |
|
207 | + /** |
|
208 | + * Offset to set. |
|
209 | + * |
|
210 | + * @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
211 | + * |
|
212 | + * @param mixed $offset <p> |
|
213 | + * The offset to assign the value to. |
|
214 | + * </p> |
|
215 | + * @param mixed $value <p> |
|
216 | + * The value to set. |
|
217 | + * </p> |
|
218 | + * |
|
219 | + * @since 5.0.0 |
|
220 | + */ |
|
221 | + public function offsetSet($offset, $value) |
|
222 | + { |
|
223 | + return $this->getIterator()->offsetSet($offset, $value); |
|
224 | + } |
|
225 | 225 | |
226 | - /** |
|
227 | - * Offset to unset. |
|
228 | - * |
|
229 | - * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
230 | - * |
|
231 | - * @param mixed $offset <p> |
|
232 | - * The offset to unset. |
|
233 | - * </p> |
|
234 | - * |
|
235 | - * @since 5.0.0 |
|
236 | - */ |
|
237 | - public function offsetUnset($offset) |
|
238 | - { |
|
239 | - return $this->getIterator()->offsetUnset($offset); |
|
240 | - } |
|
226 | + /** |
|
227 | + * Offset to unset. |
|
228 | + * |
|
229 | + * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
230 | + * |
|
231 | + * @param mixed $offset <p> |
|
232 | + * The offset to unset. |
|
233 | + * </p> |
|
234 | + * |
|
235 | + * @since 5.0.0 |
|
236 | + */ |
|
237 | + public function offsetUnset($offset) |
|
238 | + { |
|
239 | + return $this->getIterator()->offsetUnset($offset); |
|
240 | + } |
|
241 | 241 | |
242 | - /** |
|
243 | - * Specify data which should be serialized to JSON. |
|
244 | - * |
|
245 | - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
246 | - * |
|
247 | - * @return mixed data which can be serialized by <b>json_encode</b>, |
|
248 | - * which is a value of any type other than a resource. |
|
249 | - * |
|
250 | - * @since 5.4.0 |
|
251 | - */ |
|
252 | - public function jsonSerialize() |
|
253 | - { |
|
254 | - return array_map(function (AbstractTDBMObject $item) { |
|
255 | - return $item->jsonSerialize(); |
|
256 | - }, $this->toArray()); |
|
257 | - } |
|
242 | + /** |
|
243 | + * Specify data which should be serialized to JSON. |
|
244 | + * |
|
245 | + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
246 | + * |
|
247 | + * @return mixed data which can be serialized by <b>json_encode</b>, |
|
248 | + * which is a value of any type other than a resource. |
|
249 | + * |
|
250 | + * @since 5.4.0 |
|
251 | + */ |
|
252 | + public function jsonSerialize() |
|
253 | + { |
|
254 | + return array_map(function (AbstractTDBMObject $item) { |
|
255 | + return $item->jsonSerialize(); |
|
256 | + }, $this->toArray()); |
|
257 | + } |
|
258 | 258 | } |