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