1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System PHP 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\Reactor\Models\Files\Traits; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Reactor\Models\Sql\DataObjects; |
19
|
|
|
use O2System\Spl\DataStructures\SplArrayObject; |
20
|
|
|
use O2System\Spl\Iterators\ArrayIterator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class FinderTrait |
24
|
|
|
* |
25
|
|
|
* @package O2System\Reactor\Models\Sql\Traits |
26
|
|
|
*/ |
27
|
|
|
trait FinderTrait |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* FinderTrait::all |
31
|
|
|
* |
32
|
|
|
* @param null $fields |
|
|
|
|
33
|
|
|
* @param null $limit |
|
|
|
|
34
|
|
|
* |
35
|
|
|
* @return bool|DataObjects\Result |
36
|
|
|
*/ |
37
|
|
|
public function all($fields = null, $limit = null) |
38
|
|
|
{ |
39
|
|
|
$result = $this->storage; |
40
|
|
|
|
41
|
|
|
if (isset($limit)) { |
42
|
|
|
$result = array_slice($this->storage, $limit); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (empty($fields)) { |
46
|
|
|
return $this->result = new ArrayIterator($result); |
|
|
|
|
47
|
|
|
} else { |
48
|
|
|
$this->result = new ArrayIterator(); |
49
|
|
|
|
50
|
|
|
foreach ($result as $row) { |
51
|
|
|
$item = new SplArrayObject(); |
52
|
|
|
foreach ($fields as $field) { |
53
|
|
|
if ($row->offsetExists($field)) { |
54
|
|
|
$item[ $field ] = $row->offsetGet($field); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->result[] = $item; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this->result; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return false; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
// ------------------------------------------------------------------------ |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* FinderTrait::page |
70
|
|
|
* |
71
|
|
|
* Find record by page. |
72
|
|
|
* |
73
|
|
|
* @param int $page |
74
|
|
|
*/ |
75
|
|
|
public function page($fields = null, $page = 1, $entries = 5) |
76
|
|
|
{ |
77
|
|
|
$chunks = array_chunk($this->storage, $entries); |
78
|
|
|
$offset = $page - 1; |
79
|
|
|
|
80
|
|
|
if (isset($chunks[ $offset ])) { |
81
|
|
|
$result = new ArrayIterator($chunks[ $offset ]); |
82
|
|
|
|
83
|
|
|
if (empty($fields)) { |
84
|
|
|
return $this->result = $result; |
|
|
|
|
85
|
|
|
} else { |
86
|
|
|
foreach ($result as $row) { |
87
|
|
|
$item = new SplArrayObject(); |
88
|
|
|
foreach ($fields as $field) { |
89
|
|
|
if ($row->offsetExists($field)) { |
90
|
|
|
$item[ $field ] = $row->offsetGet($field); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->result[] = $item; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->result; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
// ------------------------------------------------------------------------ |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Find |
107
|
|
|
* |
108
|
|
|
* Find single or many record base on criteria by specific field |
109
|
|
|
* |
110
|
|
|
* @param string $criteria Criteria value |
111
|
|
|
* @param string|null $field Table column field name | set to primary key by default |
112
|
|
|
* |
113
|
|
|
* @return DataObjects\Result|DataObjects\Result\Row|bool Returns FALSE if failed. |
114
|
|
|
*/ |
115
|
|
|
public function find($criteria, $field = null, $limit = null) |
116
|
|
|
{ |
117
|
|
|
if (is_array($criteria)) { |
|
|
|
|
118
|
|
|
return $this->findIn($criteria, $field); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$field = isset($field) ? $field : $this->primaryKey; |
122
|
|
|
|
123
|
|
|
$result = new ArrayIterator(); |
124
|
|
|
|
125
|
|
|
$counter = 0; |
126
|
|
|
foreach ($this->storage as $row) { |
127
|
|
|
if ($row->offsetExists($field)) { |
128
|
|
|
if ($row->offsetGet($field) === $criteria) { |
129
|
|
|
$result[] = $row; |
130
|
|
|
$counter++; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (isset($limit)) { |
135
|
|
|
if ($counter == $limit) { |
136
|
|
|
break; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ($result->count() > 0) { |
142
|
|
|
$this->result = $result; |
|
|
|
|
143
|
|
|
|
144
|
|
|
if ($result->count() == 1) { |
145
|
|
|
return $result->first(); |
146
|
|
|
} else { |
147
|
|
|
return $this->result; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
// ------------------------------------------------------------------------ |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Find In |
157
|
|
|
* |
158
|
|
|
* Find many records within criteria on specific field |
159
|
|
|
* |
160
|
|
|
* @param array $inCriteria List of criteria |
161
|
|
|
* @param string $field Table column field name | set to primary key by default |
162
|
|
|
* |
163
|
|
|
* @return DataObjects\Result|bool Returns FALSE if failed. |
164
|
|
|
*/ |
165
|
|
|
public function findIn(array $inCriteria, $field = null) |
166
|
|
|
{ |
167
|
|
|
$field = isset($field) ? $field : $this->primaryKey; |
168
|
|
|
|
169
|
|
|
$result = new ArrayIterator(); |
170
|
|
|
|
171
|
|
|
$counter = 0; |
172
|
|
|
foreach ($this->storage as $row) { |
173
|
|
|
if ($row->offsetExists($field)) { |
174
|
|
|
if (in_array($row->offsetGet($field), $inCriteria)) { |
175
|
|
|
$result[] = $row; |
176
|
|
|
$counter++; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if (isset($limit)) { |
|
|
|
|
181
|
|
|
if ($counter == $limit) { |
|
|
|
|
182
|
|
|
break; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if ($result->count() > 0) { |
188
|
|
|
$this->result = $result; |
|
|
|
|
189
|
|
|
|
190
|
|
|
if ($result->count() == 1) { |
191
|
|
|
return $result->first(); |
192
|
|
|
} else { |
193
|
|
|
return $this->result; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return false; |
198
|
|
|
} |
199
|
|
|
// ------------------------------------------------------------------------ |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Find By |
203
|
|
|
* |
204
|
|
|
* Find single record based on certain conditions |
205
|
|
|
* |
206
|
|
|
* @param array $conditions List of conditions with criteria |
207
|
|
|
* |
208
|
|
|
* @access protected |
209
|
|
|
* @return DataObjects\Result|bool Returns FALSE if failed. |
210
|
|
|
*/ |
211
|
|
|
public function findWhere(array $conditions, $limit = null) |
212
|
|
|
{ |
213
|
|
|
$result = new ArrayIterator(); |
214
|
|
|
|
215
|
|
|
$counter = 0; |
216
|
|
|
foreach ($this->storage as $row) { |
217
|
|
|
foreach ($conditions as $field => $criteria) { |
218
|
|
|
if ($row->offsetGet($field) === $criteria) { |
219
|
|
|
$result[] = $row; |
220
|
|
|
$counter++; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
if (isset($limit)) { |
225
|
|
|
if ($counter == $limit) { |
226
|
|
|
break; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
if ($result->count() > 0) { |
232
|
|
|
$this->result = $result; |
|
|
|
|
233
|
|
|
|
234
|
|
|
if ($result->count() == 1) { |
235
|
|
|
return $result->first(); |
236
|
|
|
} else { |
237
|
|
|
return $this->result; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return false; |
242
|
|
|
} |
243
|
|
|
// ------------------------------------------------------------------------ |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Find In |
247
|
|
|
* |
248
|
|
|
* Find many records not within criteria on specific field |
249
|
|
|
* |
250
|
|
|
* @param array $notInCriteria List of criteria |
251
|
|
|
* @param string $field Table column field name | set to primary key by default |
252
|
|
|
* |
253
|
|
|
* @return DataObjects\Result|bool Returns FALSE if failed. |
254
|
|
|
*/ |
255
|
|
|
public function findNotIn(array $notInCriteria, $field = null) |
256
|
|
|
{ |
257
|
|
|
$field = isset($field) ? $field : $this->primaryKey; |
258
|
|
|
|
259
|
|
|
$result = new ArrayIterator(); |
260
|
|
|
|
261
|
|
|
$counter = 0; |
262
|
|
|
foreach ($this->storage as $row) { |
263
|
|
|
if ($row->offsetExists($field)) { |
264
|
|
|
if ( ! in_array($row->offsetGet($field), $notInCriteria)) { |
265
|
|
|
$result[] = $row; |
266
|
|
|
$counter++; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
if (isset($limit)) { |
|
|
|
|
271
|
|
|
if ($counter == $limit) { |
|
|
|
|
272
|
|
|
break; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
if ($result->count() > 0) { |
278
|
|
|
$this->result = $result; |
|
|
|
|
279
|
|
|
|
280
|
|
|
if ($result->count() == 1) { |
281
|
|
|
return $result->first(); |
282
|
|
|
} else { |
283
|
|
|
return $this->result; |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return false; |
288
|
|
|
} |
289
|
|
|
} |