1
|
|
|
<?php |
2
|
|
|
namespace Suricate; |
3
|
|
|
|
4
|
|
|
class DBCollection extends Collection |
5
|
|
|
{ |
6
|
|
|
/* @var string SQL table name */ |
7
|
|
|
protected $tableName = ''; |
8
|
|
|
/* @var string Item type stored in collection */ |
9
|
|
|
protected $itemsType = ''; |
10
|
|
|
/* @var string Database configuration identifier */ |
11
|
|
|
protected $DBConfig = ''; |
12
|
|
|
/* @var string Name of parent identifier field */ |
13
|
|
|
protected $parentIdField = 'parent_id'; |
14
|
|
|
/** @var string Parent Object type */ |
15
|
|
|
protected $parentType = ''; |
16
|
|
|
|
17
|
|
|
protected $mapping = []; |
18
|
|
|
protected $lazyLoad = false; |
19
|
|
|
protected $parentId; // Id of the parent |
20
|
|
|
protected $parentFilterName; // Name of field used for filtering |
21
|
|
|
protected $parentFilterType; // Value of filter |
22
|
|
|
|
23
|
|
|
protected $dbLink = false; |
24
|
|
|
protected $itemOffset = 0; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get table name |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
2 |
|
public function getTableName(): string |
32
|
|
|
{ |
33
|
2 |
|
return $this->tableName; |
34
|
|
|
} |
35
|
|
|
|
36
|
3 |
|
public function getItemsType(): string |
37
|
|
|
{ |
38
|
3 |
|
return $this->itemsType; |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
public function getDBConfig(): string |
42
|
|
|
{ |
43
|
1 |
|
return $this->DBConfig; |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
public function getParentIdField(): string |
47
|
|
|
{ |
48
|
1 |
|
return $this->parentIdField; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
public function getParentType() |
52
|
|
|
{ |
53
|
1 |
|
return $this->parentType; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
public function getParentId() |
57
|
|
|
{ |
58
|
1 |
|
return $this->parentId; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set lazyload flag |
63
|
|
|
* |
64
|
|
|
* @param bool $lazyLoad |
65
|
|
|
* @return DBCollection |
66
|
|
|
*/ |
67
|
1 |
|
public function setLazyLoad($lazyLoad) |
68
|
|
|
{ |
69
|
1 |
|
$this->lazyLoad = $lazyLoad; |
70
|
|
|
|
71
|
1 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get lazyload flag |
76
|
|
|
* |
77
|
|
|
* @return boolean |
78
|
|
|
*/ |
79
|
1 |
|
public function getLazyLoad(): bool |
80
|
|
|
{ |
81
|
1 |
|
return $this->lazyLoad; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function purgeItems() |
85
|
|
|
{ |
86
|
|
|
$this->items = []; |
87
|
|
|
$this->mapping = []; |
88
|
|
|
$this->itemOffset = 0; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Load entire table into collection |
93
|
|
|
* @return Collection Loaded collection |
94
|
|
|
*/ |
95
|
1 |
|
public static function loadAll() |
96
|
|
|
{ |
97
|
1 |
|
$calledClass = get_called_class(); |
98
|
1 |
|
$collection = new $calledClass; |
99
|
|
|
|
100
|
1 |
|
$sqlParams = []; |
101
|
|
|
|
102
|
1 |
|
$sql = "SELECT *"; |
103
|
1 |
|
$sql .= " FROM `" . $collection->getTableName() . "`"; |
104
|
|
|
|
105
|
1 |
|
if ($collection->parentFilterType !== '' && $collection->parentFilterType != null) { |
106
|
|
|
$sql .= "WHERE " . $collection->parentFilterName . "=:type"; |
107
|
|
|
$sqlParams['type'] = $collection->parentFilterType; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
$collection->loadFromSql($sql, $sqlParams); |
111
|
|
|
|
112
|
1 |
|
return $collection; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Static wrapper for loadFromSql |
117
|
|
|
* @param string $sql SQL Statement |
118
|
|
|
* @param array $sqlParams SQL Parameters |
119
|
|
|
* @return Collection Loaded collection |
120
|
|
|
*/ |
121
|
|
|
public static function buildFromSql($sql, $sqlParams = []) |
122
|
|
|
{ |
123
|
|
|
$calledClass = get_called_class(); |
124
|
|
|
$collection = new $calledClass; |
125
|
|
|
|
126
|
|
|
$collection->loadFromSql($sql, $sqlParams); |
127
|
|
|
|
128
|
|
|
return $collection; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Load collection from SQL query |
133
|
|
|
* |
134
|
|
|
* @param string $sql SQL query |
135
|
|
|
* @param array $sqlParams associative array of SQL params |
136
|
|
|
* @return DBCollection |
137
|
|
|
*/ |
138
|
2 |
|
public function loadFromSql($sql, $sqlParams = []) |
139
|
|
|
{ |
140
|
2 |
|
if (!in_array(Interfaces\IDBObject::class, class_implements($this->itemsType))) { |
141
|
|
|
throw new \BadMethodCallException('Item type does not implement IDBObject interface'); |
142
|
|
|
} |
143
|
|
|
|
144
|
2 |
|
$this->connectDB(); |
145
|
2 |
|
$results = $this->dbLink->query($sql, $sqlParams)->fetchAll(); |
146
|
|
|
|
147
|
2 |
|
if ($results !== false) { |
148
|
2 |
|
foreach ($results as $currentResult) { |
149
|
2 |
|
$itemName = $this->getItemsType(); |
150
|
2 |
|
$this->addItem($itemName::instanciate($currentResult)); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
2 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function addItemLink($linkId) |
158
|
|
|
{ |
159
|
|
|
$this->items[$this->itemOffset] = $linkId; |
160
|
|
|
// add mapping between item->index and $position in items pool |
161
|
|
|
$this->mapping[$this->itemOffset] = $linkId; |
162
|
|
|
$this->itemOffset++; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function lazyLoadFromSql($sql, $sqlParams = array()) |
166
|
|
|
{ |
167
|
|
|
$dbLink = Suricate::Database(); |
|
|
|
|
168
|
|
|
if ($this->DBConfig !== '') { |
169
|
|
|
$dbLink->setConfig($this->DBConfig); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$results = $dbLink |
173
|
|
|
->query($sql, $sqlParams) |
174
|
|
|
->fetchAll(); |
175
|
|
|
|
176
|
|
|
if ($results !== false) { |
177
|
|
|
foreach ($results as $currentResult) { |
178
|
|
|
$this->addItemLink(current($currentResult)); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Load items linked to a parentId |
187
|
|
|
* @param mixed $parentId Parent id description |
188
|
|
|
* @param string $parentIdField Name of parent id referencing field |
189
|
|
|
* @param \Closure|null $validate Callback use to validate add to items collection |
190
|
|
|
*/ |
191
|
|
|
public static function loadForParentId($parentId, $parentIdField = null, $validate = null) |
192
|
|
|
{ |
193
|
|
|
$calledClass = get_called_class(); |
194
|
|
|
$collection = new $calledClass; |
195
|
|
|
|
196
|
|
|
if ($parentId != '') { |
197
|
|
|
$sqlParams = []; |
198
|
|
|
$dbHandler = Suricate::Database(true); |
199
|
|
|
|
200
|
|
|
if ($collection->getDBConfig() !== '') { |
201
|
|
|
$dbHandler->setConfig($collection->getDBConfig()); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$sql = "SELECT *"; |
205
|
|
|
$sql .= " FROM `" . $collection->getTableName() . "`"; |
206
|
|
|
$sql .= " WHERE"; |
207
|
|
|
if ($parentIdField !== null) { |
208
|
|
|
$sql .= "`" . $parentIdField . "`=:parent_id"; |
209
|
|
|
} else { |
210
|
|
|
$sql .= "`" . $collection->getParentIdField() . "`=:parent_id"; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
if ($collection->parentFilterType !== null) { |
214
|
|
|
$sql .= " AND " . $collection->parentFilterName . "=:parent_type"; |
215
|
|
|
$sqlParams['parent_type'] = $collection->parentFilterType; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$sqlParams['parent_id'] = $parentId; |
219
|
|
|
$results = $dbHandler->query($sql, $sqlParams)->fetchAll(); |
220
|
|
|
|
221
|
|
|
if ($results !== false) { |
222
|
|
|
foreach ($results as $currentResult) { |
223
|
|
|
$itemName = $collection->getItemsType(); |
224
|
|
|
$item = $itemName::instanciate($currentResult); |
225
|
|
|
if ($validate === null || $validate($item)) { |
226
|
|
|
$collection->addItem($item); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$collection->parentId = $parentId; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return $collection; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function setParentIdForAll($parentId) |
238
|
|
|
{ |
239
|
|
|
$this->parentId = $parentId; |
240
|
|
|
foreach (array_keys($this->items) as $key) { |
241
|
|
|
$this->items[$key]->{$this->parentIdField} = $parentId; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Load Parent Item, if item type is defined |
247
|
|
|
*/ |
248
|
|
|
public function loadParent() |
249
|
|
|
{ |
250
|
|
|
if ($this->parentType !== '' && $this->parentId != '') { |
251
|
|
|
$parentObjectType = $this->parentType; |
252
|
|
|
$parent = new $parentObjectType(); |
253
|
|
|
if (method_exists($parent, 'load')) { |
254
|
|
|
$parent->load($this->parentId); |
255
|
|
|
|
256
|
|
|
return $parent; |
257
|
|
|
} else { |
258
|
|
|
throw new \BadMethodCallException('Parent object does not have a load($id) method'); |
259
|
|
|
} |
260
|
|
|
} else { |
261
|
|
|
throw new \BadMethodCallException('self::$parentType is not defined'); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function craftItem($itemData) |
266
|
|
|
{ |
267
|
|
|
$itemName = $this->itemsType; |
268
|
|
|
|
269
|
|
|
foreach ($itemData as $data) { |
270
|
|
|
$newItem = new $itemName(); |
271
|
|
|
$newItem->{$this->parentIdField} = $this->parentId; |
272
|
|
|
$hasData = false; |
273
|
|
|
foreach ($data as $field => $value) { |
274
|
|
|
$newItem->$field = $value; |
275
|
|
|
if ($value != '') { |
276
|
|
|
$hasData = true; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
// Only add item if there's data inside |
281
|
|
|
if ($hasData) { |
282
|
|
|
$this->addItem($newItem); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
public function save() |
288
|
|
|
{ |
289
|
|
|
// 1st step : delete all records for current parentId |
290
|
|
|
$sql = "DELETE FROM `" . $this->tableName . "`"; |
291
|
|
|
if ($this->parentIdField !== '') { |
292
|
|
|
$sql .= " WHERE"; |
293
|
|
|
$sql .= " `" . $this->parentIdField . "`=:parent_id"; |
294
|
|
|
|
295
|
|
|
$sqlParams = array('parent_id' => $this->parentId); |
296
|
|
|
} else { |
297
|
|
|
$sqlParams = array(); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
$dbLink = Suricate::Database(); |
301
|
|
|
if ($this->DBConfig !== '') { |
302
|
|
|
$dbLink->setConfig($this->DBConfig); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
$dbLink->query($sql, $sqlParams); |
306
|
|
|
|
307
|
|
|
// 2nd step : save all current items |
308
|
|
|
foreach ($this->items as $currentItem) { |
309
|
|
|
$currentItem->save(true); // Force insert |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
2 |
|
public function addItem(Interfaces\IDBObject $item) |
314
|
|
|
{ |
315
|
2 |
|
$key = $item->getTableIndex(); |
316
|
|
|
// Add item to items pool |
317
|
2 |
|
$this->items[$this->itemOffset] = $item; |
318
|
|
|
|
319
|
|
|
// add mapping between item->index and $position in items pool |
320
|
2 |
|
$this->mapping[$this->itemOffset] = $item->$key; |
321
|
|
|
|
322
|
2 |
|
$this->itemOffset++; |
323
|
|
|
|
324
|
2 |
|
return $this; |
325
|
|
|
} |
326
|
|
|
|
327
|
2 |
|
protected function connectDB() |
328
|
|
|
{ |
329
|
2 |
|
if (!$this->dbLink) { |
330
|
|
|
$this->dbLink = Suricate::Database(); |
331
|
|
|
if ($this->getDBConfig() !== '') { |
332
|
|
|
$this->dbLink->setConfig($this->getDBConfig()); |
333
|
|
|
} |
334
|
|
|
} |
335
|
2 |
|
} |
336
|
|
|
} |
337
|
|
|
|