1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Pomm's Foundation package. |
4
|
|
|
* |
5
|
|
|
* (c) 2014 - 2017 Grégoire HUBERT <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace PommProject\Foundation; |
11
|
|
|
|
12
|
|
|
use PommProject\Foundation\Session\ResultHandler; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* ResultIterator |
16
|
|
|
* |
17
|
|
|
* This is a simple iterator on database results. It manages a result database |
18
|
|
|
* resource. |
19
|
|
|
* |
20
|
|
|
* @package Foundation |
21
|
|
|
* @copyright 2014 - 2017 Grégoire HUBERT |
22
|
|
|
* @author Grégoire HUBERT |
23
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
24
|
|
|
@see ResultIteratorInterface |
25
|
|
|
* @see \JsonSerializable |
26
|
|
|
*/ |
27
|
|
|
class ResultIterator implements ResultIteratorInterface, \JsonSerializable |
28
|
|
|
{ |
29
|
|
|
private $position; |
30
|
|
|
protected $result; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* __construct |
34
|
|
|
* |
35
|
|
|
* Constructor |
36
|
|
|
* |
37
|
|
|
* @param ResultHandler $result |
38
|
|
|
*/ |
39
|
|
|
public function __construct(ResultHandler $result) |
40
|
|
|
{ |
41
|
|
|
$this->result = $result; |
42
|
|
|
$this->position = 0; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* __destruct |
47
|
|
|
* |
48
|
|
|
* Closes the cursor when the collection is cleared. |
49
|
|
|
*/ |
50
|
|
|
public function __destruct() |
51
|
|
|
{ |
52
|
|
|
$this->result->free(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* seek |
57
|
|
|
* |
58
|
|
|
* Alias for get(), required to be a Seekable iterator. |
59
|
|
|
* |
60
|
|
|
* @param int $index |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function seek($index) |
64
|
|
|
{ |
65
|
|
|
return $this->get($index); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* get |
70
|
|
|
* |
71
|
|
|
* Return a particular result. An array with converted values is returned. |
72
|
|
|
* pg_fetch_array is muted because it produces untrappable warnings on |
73
|
|
|
* errors. |
74
|
|
|
* |
75
|
|
|
* @param integer $index |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public function get($index) |
79
|
|
|
{ |
80
|
|
|
return $this->result->fetchRow($index); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* has |
85
|
|
|
* |
86
|
|
|
* Return true if the given index exists false otherwise. |
87
|
|
|
* |
88
|
|
|
* @param integer $index |
89
|
|
|
* @return boolean |
90
|
|
|
*/ |
91
|
|
|
public function has($index) |
92
|
|
|
{ |
93
|
|
|
return (bool) ($index < $this->count()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* count |
98
|
|
|
* |
99
|
|
|
* @see \Countable |
100
|
|
|
* @return integer |
101
|
|
|
*/ |
102
|
|
|
public function count() |
103
|
|
|
{ |
104
|
|
|
return $this->result->countRows(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* rewind |
109
|
|
|
* |
110
|
|
|
* @see \Iterator |
111
|
|
|
*/ |
112
|
|
|
public function rewind() |
113
|
|
|
{ |
114
|
|
|
$this->position = 0; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* current |
119
|
|
|
* |
120
|
|
|
* @see \Iterator |
121
|
|
|
*/ |
122
|
|
|
public function current() |
123
|
|
|
{ |
124
|
|
|
return !$this->isEmpty() |
125
|
|
|
? $this->get($this->position) |
126
|
|
|
: null |
127
|
|
|
; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* key |
132
|
|
|
* |
133
|
|
|
* @see \Iterator |
134
|
|
|
*/ |
135
|
|
|
public function key() |
136
|
|
|
{ |
137
|
|
|
return $this->position; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* next |
142
|
|
|
* |
143
|
|
|
* @see \Iterator |
144
|
|
|
*/ |
145
|
|
|
public function next() |
146
|
|
|
{ |
147
|
|
|
++$this->position; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* valid |
152
|
|
|
* |
153
|
|
|
* @see \Iterator |
154
|
|
|
* @return boolean |
155
|
|
|
*/ |
156
|
|
|
public function valid() |
157
|
|
|
{ |
158
|
|
|
return $this->has($this->position); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* isFirst |
163
|
|
|
* Is the iterator on the first element ? |
164
|
|
|
* Returns null if the iterator is empty. |
165
|
|
|
* |
166
|
|
|
* @return boolean|null |
167
|
|
|
*/ |
168
|
|
|
public function isFirst() |
169
|
|
|
{ |
170
|
|
|
return !$this->isEmpty() |
171
|
|
|
? $this->position === 0 |
172
|
|
|
: null |
173
|
|
|
; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* isLast |
178
|
|
|
* |
179
|
|
|
* Is the iterator on the last element ? |
180
|
|
|
* Returns null if the iterator is empty. |
181
|
|
|
* |
182
|
|
|
* @return boolean|null |
183
|
|
|
*/ |
184
|
|
|
public function isLast() |
185
|
|
|
{ |
186
|
|
|
return !$this->isEmpty() |
187
|
|
|
? $this->position === $this->count() - 1 |
188
|
|
|
: null |
189
|
|
|
; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* isEmpty |
194
|
|
|
* |
195
|
|
|
* Is the collection empty (no element) ? |
196
|
|
|
* |
197
|
|
|
* @return boolean |
198
|
|
|
*/ |
199
|
|
|
public function isEmpty() |
200
|
|
|
{ |
201
|
|
|
return $this->result->countRows() === 0; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* isEven |
206
|
|
|
* |
207
|
|
|
* Is the iterator on an even position ? |
208
|
|
|
* |
209
|
|
|
* @return boolean |
210
|
|
|
*/ |
211
|
|
|
public function isEven() |
212
|
|
|
{ |
213
|
|
|
return ($this->position % 2) === 0; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* isOdd |
218
|
|
|
* |
219
|
|
|
* Is the iterator on an odd position ? |
220
|
|
|
* |
221
|
|
|
* @return boolean |
222
|
|
|
*/ |
223
|
|
|
public function isOdd() |
224
|
|
|
{ |
225
|
|
|
return ($this->position % 2) === 1; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* getOddEven |
230
|
|
|
* |
231
|
|
|
* Return 'odd' or 'even' depending on the element index position. |
232
|
|
|
* Useful to style list elements when printing lists to do |
233
|
|
|
* <li class="line_<?php $list->getOddEven() ?>">. |
234
|
|
|
* |
235
|
|
|
* @return String |
236
|
|
|
*/ |
237
|
|
|
public function getOddEven() |
238
|
|
|
{ |
239
|
|
|
return $this->position % 2 === 1 ? 'odd' : 'even'; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* slice |
244
|
|
|
* |
245
|
|
|
* Extract an array of values for one column. |
246
|
|
|
* |
247
|
|
|
* @param string $field |
248
|
|
|
* @return array values |
249
|
|
|
*/ |
250
|
|
|
public function slice($field) |
251
|
|
|
{ |
252
|
|
|
if ($this->isEmpty()) { |
253
|
|
|
return []; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return $this->result->fetchColumn($field); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* extract |
261
|
|
|
* |
262
|
|
|
* Dump an iterator. |
263
|
|
|
* This actually stores all the results in PHP allocated memory. |
264
|
|
|
* THIS MAY USE A LOT OF MEMORY. |
265
|
|
|
* |
266
|
|
|
* @return array |
267
|
|
|
* @deprecated |
268
|
|
|
*/ |
269
|
|
|
public function extract() |
270
|
|
|
{ |
271
|
|
|
@trigger_error('The extract function is deprecated since version 2.1 and will be removed in 3.0. Use the extractGenerator method instead.', E_USER_DEPRECATED); |
|
|
|
|
272
|
|
|
|
273
|
|
|
$results = []; |
274
|
|
|
|
275
|
|
|
foreach ($this as $result) { |
276
|
|
|
$results[] = $result; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
return $results; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* extractGenerator |
284
|
|
|
* |
285
|
|
|
* Dump an iterator |
286
|
|
|
* |
287
|
|
|
* @return \Generator |
288
|
|
|
*/ |
289
|
|
|
public function extractGenerator() |
290
|
|
|
{ |
291
|
|
|
foreach ($this as $result) { |
292
|
|
|
yield $result; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* jsonSerialize |
298
|
|
|
* |
299
|
|
|
* @see \JsonSerializable |
300
|
|
|
*/ |
301
|
|
|
public function jsonSerialize() |
302
|
|
|
{ |
303
|
|
|
$values = []; |
304
|
|
|
|
305
|
|
|
foreach ($this->extractGenerator() as $item) { |
306
|
|
|
$values[] = $item; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return $values; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: