Completed
Push — 2.0 ( 143803...4e64fa )
by grégoire
08:42 queued 04:22
created

ResultIterator::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of the Pomm's Foundation package.
4
 *
5
 * (c) 2014 - 2015 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
 * Iterator on database results.
18
 *
19
 * @package   Foundation
20
 * @copyright 2014 - 2015 Grégoire HUBERT
21
 * @author    Grégoire HUBERT
22
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
23
 * @see       \Iterator
24
 * @see       \Countable
25
 * @see       \JsonSerializable
26
 */
27
class ResultIterator implements \Iterator, \Countable, \JsonSerializable, \SeekableIterator
28
{
29
    private $position;
30
    protected $result;
31
32
    /**
33
     * __construct
34
     *
35
     * Constructor
36
     *
37
     * @access public
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
        return $this->result->countRows();
106
    }
107
108
    /**
109
     * rewind
110
     *
111
     * @see \Iterator
112
     */
113
    public function rewind()
114
    {
115
        $this->position = 0;
116
    }
117
118
    /**
119
     * current
120
     *
121
     * @see \Iterator
122
     */
123
    public function current()
124
    {
125
        return !$this->isEmpty()
126
            ? $this->get($this->position)
127
            : null
128
            ;
129
    }
130
131
    /**
132
     * key
133
     *
134
     * @see \Iterator
135
     */
136
    public function key()
137
    {
138
        return $this->position;
139
    }
140
141
    /**
142
     * next
143
     *
144
     * @see \Iterator
145
     */
146
    public function next()
147
    {
148
        ++$this->position;
149
    }
150
151
    /**
152
     * valid
153
     *
154
     * @see \Iterator
155
     * @return boolean
156
     */
157
    public function valid()
158
    {
159
        return $this->has($this->position);
160
    }
161
162
    /**
163
     * isFirst
164
     * Is the iterator on the first element ?
165
     * Returns null if the iterator is empty.
166
     *
167
     * @return boolean|null
168
     */
169
    public function isFirst()
170
    {
171
        return !$this->isEmpty()
172
            ? $this->position === 0
173
            : null
174
            ;
175
    }
176
177
    /**
178
     * isLast
179
     *
180
     * Is the iterator on the last element ?
181
     * Returns null if the iterator is empty.
182
     *
183
     * @return boolean|null
184
     */
185
    public function isLast()
186
    {
187
        return !$this->isEmpty()
188
            ? $this->position === $this->count() - 1
189
            : null
190
            ;
191
    }
192
193
    /**
194
     * isEmpty
195
     *
196
     * Is the collection empty (no element) ?
197
     *
198
     * @return boolean
199
     */
200
    public function isEmpty()
201
    {
202
        return $this->result->countRows() === 0;
203
    }
204
205
    /**
206
     * isEven
207
     *
208
     * Is the iterator on an even position ?
209
     *
210
     * @return boolean
211
     */
212
    public function isEven()
213
    {
214
        return ($this->position % 2) === 0;
215
    }
216
217
    /**
218
     * isOdd
219
     *
220
     * Is the iterator on an odd position ?
221
     *
222
     * @return boolean
223
     */
224
    public function isOdd()
225
    {
226
        return ($this->position % 2) === 1;
227
    }
228
229
    /**
230
     * getOddEven
231
     *
232
     * Return 'odd' or 'even' depending on the element index position.
233
     * Useful to style list elements when printing lists to do
234
     * <li class="line_<?php $list->getOddEven() ?>">.
235
     *
236
     * @return String
237
     */
238
    public function getOddEven()
239
    {
240
        return $this->position % 2 === 1 ? 'odd' : 'even';
241
    }
242
243
    /**
244
     * slice
245
     *
246
     * Extract an array of values for one column.
247
     *
248
     * @param  string $field
249
     * @return array  values
250
     */
251
    public function slice($field)
252
    {
253
        if ($this->isEmpty()) {
254
            return [];
255
        }
256
257
        return $this->result->fetchColumn($field);
258
    }
259
260
    /**
261
     * extract
262
     *
263
     * Dump an iterator.
264
     * This actually stores all the results in PHP allocated memory.
265
     * THIS MAY USE A LOT OF MEMORY.
266
     *
267
     * @access public
268
     * @return array
269
     */
270
    public function extract()
271
    {
272
        $results = [];
273
274
        foreach ($this as $result) {
275
            $results[] = $result;
276
        }
277
278
        return $results;
279
    }
280
281
    /**
282
     * jsonSerialize
283
     *
284
     * @see \JsonSerializable
285
     */
286
    public function jsonSerialize()
287
    {
288
        return $this->extract();
289
    }
290
}
291