Completed
Push — master ( 42e2f4...06fa71 )
by David
15s queued 12s
created

PageIterator::createEmpyIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace TheCodingMachine\TDBM;
5
6
use Doctrine\DBAL\Statement;
7
use Mouf\Database\MagicQuery;
8
use Porpaginas\Page;
9
use Psr\Log\LoggerInterface;
10
use Psr\Log\NullLogger;
11
12
/*
13
 Copyright (C) 2006-2017 David Négrier - THE CODING MACHINE
14
15
 This program is free software; you can redistribute it and/or modify
16
 it under the terms of the GNU General Public License as published by
17
 the Free Software Foundation; either version 2 of the License, or
18
 (at your option) any later version.
19
20
 This program is distributed in the hope that it will be useful,
21
 but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 GNU General Public License for more details.
24
25
 You should have received a copy of the GNU General Public License
26
 along with this program; if not, write to the Free Software
27
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28
 */
29
30
/**
31
 * Iterator used to retrieve results.
32
 */
33
class PageIterator implements Page, \ArrayAccess, \JsonSerializable
34
{
35
    /**
36
     * @var Statement
37
     */
38
    protected $statement;
39
40
    protected $fetchStarted = false;
41
    private $objectStorage;
42
    private $className;
43
44
    private $parentResult;
45
    private $tdbmService;
46
    private $magicSql;
47
    private $parameters;
48
    private $limit;
49
    private $offset;
50
    private $columnDescriptors;
51
    private $magicQuery;
52
53
    /**
54
     * The key of the current retrieved object.
55
     *
56
     * @var int
57
     */
58
    protected $key = -1;
59
60
    protected $current;
61
62
    private $innerResultIterator;
63
64
    private $mode;
65
66
    /**
67
     * @var LoggerInterface
68
     */
69
    private $logger;
70
71
    private function __construct()
72
    {
73
    }
74
75
    /**
76
     * @param mixed[] $parameters
77
     * @param array[] $columnDescriptors
78
     */
79
    public static function createResultIterator(ResultIterator $parentResult, string $magicSql, array $parameters, int $limit, int $offset, array $columnDescriptors, ObjectStorageInterface $objectStorage, ?string $className, TDBMService $tdbmService, MagicQuery $magicQuery, int $mode, LoggerInterface $logger): self
80
    {
81
        $iterator =  new self();
82
        $iterator->parentResult = $parentResult;
83
        $iterator->magicSql = $magicSql;
84
        $iterator->objectStorage = $objectStorage;
85
        $iterator->className = $className;
86
        $iterator->tdbmService = $tdbmService;
87
        $iterator->parameters = $parameters;
88
        $iterator->limit = $limit;
89
        $iterator->offset = $offset;
90
        $iterator->columnDescriptors = $columnDescriptors;
91
        $iterator->magicQuery = $magicQuery;
92
        $iterator->mode = $mode;
93
        $iterator->logger = $logger;
94
        return $iterator;
95
    }
96
97
    public static function createEmpyIterator(ResultIterator $parentResult): self
98
    {
99
        $iterator = new self();
100
        $iterator->parentResult = $parentResult;
101
        $iterator->logger = new NullLogger();
102
        return $iterator;
103
    }
104
105
    /**
106
     * Retrieve an external iterator.
107
     *
108
     * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
109
     *
110
     * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or
111
     *                             <b>Traversable</b>
112
     *
113
     * @since 5.0.0
114
     */
115
    public function getIterator()
116
    {
117
        if ($this->innerResultIterator === null) {
118
            if ($this->parentResult->count() === 0) {
119
                $this->innerResultIterator = InnerResultIterator::createEmpyIterator();
120
            } elseif ($this->mode === TDBMService::MODE_CURSOR) {
121
                $this->innerResultIterator = InnerResultIterator::createInnerResultIterator($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
122
            } else {
123
                $this->innerResultIterator = InnerResultArray::createInnerResultIterator($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
124
            }
125
        }
126
127
        return $this->innerResultIterator;
128
    }
129
130
    /**
131
     * @return int
132
     */
133
    public function getCurrentOffset()
134
    {
135
        return $this->offset;
136
    }
137
138
    /**
139
     * @return int
140
     */
141
    public function getCurrentPage()
142
    {
143
        return (int) floor($this->offset / $this->limit) + 1;
144
    }
145
146
    /**
147
     * @return int
148
     */
149
    public function getCurrentLimit()
150
    {
151
        return $this->limit;
152
    }
153
154
    /**
155
     * Return the number of results on the current page of the {@link Result}.
156
     *
157
     * @return int
158
     */
159
    public function count()
160
    {
161
        return $this->getIterator()->count();
162
    }
163
164
    /**
165
     * Return the number of ALL results in the paginatable of {@link Result}.
166
     *
167
     * @return int
168
     */
169
    public function totalCount()
170
    {
171
        return $this->parentResult->count();
172
    }
173
174
    /**
175
     * Casts the result set to a PHP array.
176
     *
177
     * @return AbstractTDBMObject[]
178
     */
179
    public function toArray(): array
180
    {
181
        return iterator_to_array($this->getIterator());
182
    }
183
184
    /**
185
     * Returns a new iterator mapping any call using the $callable function.
186
     *
187
     * @param callable $callable
188
     *
189
     * @return MapIterator
190
     */
191
    public function map(callable $callable): MapIterator
192
    {
193
        if ($this->count() === 0) {
194
            return new MapIterator([], $callable);
195
        }
196
        return new MapIterator($this->getIterator(), $callable);
197
    }
198
199
    /**
200
     * Whether a offset exists.
201
     *
202
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
203
     *
204
     * @param mixed $offset <p>
205
     *                      An offset to check for.
206
     *                      </p>
207
     *
208
     * @return bool true on success or false on failure.
209
     *              </p>
210
     *              <p>
211
     *              The return value will be casted to boolean if non-boolean was returned
212
     *
213
     * @since 5.0.0
214
     */
215
    public function offsetExists($offset)
216
    {
217
        return $this->getIterator()->offsetExists($offset);
218
    }
219
220
    /**
221
     * Offset to retrieve.
222
     *
223
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
224
     *
225
     * @param mixed $offset <p>
226
     *                      The offset to retrieve.
227
     *                      </p>
228
     *
229
     * @return mixed Can return all value types
230
     *
231
     * @since 5.0.0
232
     */
233
    public function offsetGet($offset)
234
    {
235
        return $this->getIterator()->offsetGet($offset);
236
    }
237
238
    /**
239
     * Offset to set.
240
     *
241
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
242
     *
243
     * @param mixed $offset <p>
244
     *                      The offset to assign the value to.
245
     *                      </p>
246
     * @param mixed $value  <p>
247
     *                      The value to set.
248
     *                      </p>
249
     *
250
     * @since 5.0.0
251
     */
252
    public function offsetSet($offset, $value)
253
    {
254
        return $this->getIterator()->offsetSet($offset, $value);
255
    }
256
257
    /**
258
     * Offset to unset.
259
     *
260
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
261
     *
262
     * @param mixed $offset <p>
263
     *                      The offset to unset.
264
     *                      </p>
265
     *
266
     * @since 5.0.0
267
     */
268
    public function offsetUnset($offset)
269
    {
270
        return $this->getIterator()->offsetUnset($offset);
271
    }
272
273
    /**
274
     * Specify data which should be serialized to JSON.
275
     *
276
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
277
     *
278
     * @return mixed data which can be serialized by <b>json_encode</b>,
279
     *               which is a value of any type other than a resource
280
     *
281
     * @since 5.4.0
282
     */
283
    public function jsonSerialize()
284
    {
285
        return array_map(function (AbstractTDBMObject $item) {
286
            return $item->jsonSerialize();
287
        }, $this->toArray());
288
    }
289
}
290