PageIterator::map()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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