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
|
|
|
private $rows_count; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* __construct |
35
|
|
|
* |
36
|
|
|
* Constructor |
37
|
|
|
* |
38
|
|
|
* @param ResultHandler $result |
39
|
|
|
*/ |
40
|
|
|
public function __construct(ResultHandler $result) |
41
|
|
|
{ |
42
|
|
|
$this->result = $result; |
43
|
|
|
$this->position = 0; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* __destruct |
48
|
|
|
* |
49
|
|
|
* Closes the cursor when the collection is cleared. |
50
|
|
|
*/ |
51
|
|
|
public function __destruct() |
52
|
|
|
{ |
53
|
|
|
$this->result->free(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* seek |
58
|
|
|
* |
59
|
|
|
* Alias for get(), required to be a Seekable iterator. |
60
|
|
|
* |
61
|
|
|
* @param int $index |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function seek($index) |
65
|
|
|
{ |
66
|
|
|
return $this->get($index); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* get |
71
|
|
|
* |
72
|
|
|
* Return a particular result. An array with converted values is returned. |
73
|
|
|
* pg_fetch_array is muted because it produces untrappable warnings on |
74
|
|
|
* errors. |
75
|
|
|
* |
76
|
|
|
* @param integer $index |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function get($index) |
80
|
|
|
{ |
81
|
|
|
return $this->result->fetchRow($index); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* has |
86
|
|
|
* |
87
|
|
|
* Return true if the given index exists false otherwise. |
88
|
|
|
* |
89
|
|
|
* @param integer $index |
90
|
|
|
* @return boolean |
91
|
|
|
*/ |
92
|
|
|
public function has($index) |
93
|
|
|
{ |
94
|
|
|
return (bool) ($index < $this->count()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* count |
99
|
|
|
* |
100
|
|
|
* @see \Countable |
101
|
|
|
* @return integer |
102
|
|
|
*/ |
103
|
|
|
public function count() |
104
|
|
|
{ |
105
|
|
|
if ($this->rows_count == null) { |
106
|
|
|
$this->rows_count = $this->result->countRows(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->rows_count; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* rewind |
114
|
|
|
* |
115
|
|
|
* @see \Iterator |
116
|
|
|
*/ |
117
|
|
|
public function rewind() |
118
|
|
|
{ |
119
|
|
|
$this->position = 0; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* current |
124
|
|
|
* |
125
|
|
|
* @see \Iterator |
126
|
|
|
*/ |
127
|
|
|
public function current() |
128
|
|
|
{ |
129
|
|
|
return (($this->rows_count != null && $this->rows_count > 0 ) || !$this->isEmpty()) |
130
|
|
|
? $this->get($this->position) |
131
|
|
|
: null |
132
|
|
|
; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* key |
137
|
|
|
* |
138
|
|
|
* @see \Iterator |
139
|
|
|
*/ |
140
|
|
|
public function key() |
141
|
|
|
{ |
142
|
|
|
return $this->position; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* next |
147
|
|
|
* |
148
|
|
|
* @see \Iterator |
149
|
|
|
*/ |
150
|
|
|
public function next() |
151
|
|
|
{ |
152
|
|
|
++$this->position; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* valid |
157
|
|
|
* |
158
|
|
|
* @see \Iterator |
159
|
|
|
* @return boolean |
160
|
|
|
*/ |
161
|
|
|
public function valid() |
162
|
|
|
{ |
163
|
|
|
return $this->has($this->position); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* isFirst |
168
|
|
|
* Is the iterator on the first element ? |
169
|
|
|
* Returns null if the iterator is empty. |
170
|
|
|
* |
171
|
|
|
* @return boolean|null |
172
|
|
|
*/ |
173
|
|
|
public function isFirst() |
174
|
|
|
{ |
175
|
|
|
return !$this->isEmpty() |
176
|
|
|
? $this->position === 0 |
177
|
|
|
: null |
178
|
|
|
; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* isLast |
183
|
|
|
* |
184
|
|
|
* Is the iterator on the last element ? |
185
|
|
|
* Returns null if the iterator is empty. |
186
|
|
|
* |
187
|
|
|
* @return boolean|null |
188
|
|
|
*/ |
189
|
|
|
public function isLast() |
190
|
|
|
{ |
191
|
|
|
return !$this->isEmpty() |
192
|
|
|
? $this->position === $this->count() - 1 |
193
|
|
|
: null |
194
|
|
|
; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* isEmpty |
199
|
|
|
* |
200
|
|
|
* Is the collection empty (no element) ? |
201
|
|
|
* |
202
|
|
|
* @return boolean |
203
|
|
|
*/ |
204
|
|
|
public function isEmpty() |
205
|
|
|
{ |
206
|
|
|
return (($this->rows_count != null && $this->rows_count === 0 ) || $this->count() === 0); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* isEven |
211
|
|
|
* |
212
|
|
|
* Is the iterator on an even position ? |
213
|
|
|
* |
214
|
|
|
* @return boolean |
215
|
|
|
*/ |
216
|
|
|
public function isEven() |
217
|
|
|
{ |
218
|
|
|
return ($this->position % 2) === 0; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* isOdd |
223
|
|
|
* |
224
|
|
|
* Is the iterator on an odd position ? |
225
|
|
|
* |
226
|
|
|
* @return boolean |
227
|
|
|
*/ |
228
|
|
|
public function isOdd() |
229
|
|
|
{ |
230
|
|
|
return ($this->position % 2) === 1; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* getOddEven |
235
|
|
|
* |
236
|
|
|
* Return 'odd' or 'even' depending on the element index position. |
237
|
|
|
* Useful to style list elements when printing lists to do |
238
|
|
|
* <li class="line_<?php $list->getOddEven() ?>">. |
239
|
|
|
* |
240
|
|
|
* @return String |
241
|
|
|
*/ |
242
|
|
|
public function getOddEven() |
243
|
|
|
{ |
244
|
|
|
return $this->position % 2 === 1 ? 'odd' : 'even'; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* slice |
249
|
|
|
* |
250
|
|
|
* Extract an array of values for one column. |
251
|
|
|
* |
252
|
|
|
* @param string $field |
253
|
|
|
* @return array values |
254
|
|
|
*/ |
255
|
|
|
public function slice($field) |
256
|
|
|
{ |
257
|
|
|
if ($this->isEmpty()) { |
258
|
|
|
return []; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return $this->result->fetchColumn($field); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* extract |
266
|
|
|
* |
267
|
|
|
* Dump an iterator. |
268
|
|
|
* This actually stores all the results in PHP allocated memory. |
269
|
|
|
* THIS MAY USE A LOT OF MEMORY. |
270
|
|
|
* |
271
|
|
|
* @return array |
272
|
|
|
*/ |
273
|
|
|
public function extract() |
274
|
|
|
{ |
275
|
|
|
$results = []; |
276
|
|
|
|
277
|
|
|
foreach ($this as $result) { |
278
|
|
|
$results[] = $result; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
return $results; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* jsonSerialize |
286
|
|
|
* |
287
|
|
|
* @see \JsonSerializable |
288
|
|
|
*/ |
289
|
|
|
public function jsonSerialize() |
290
|
|
|
{ |
291
|
|
|
return $this->extract(); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|