Completed
Pull Request — 3.4 (#46)
by David
07:34 queued 01:29
created

PageIterator::map()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Mouf\Database\TDBM;
3
4
use Doctrine\DBAL\Driver\Connection;
5
use Doctrine\DBAL\Statement;
6
use Mouf\Database\MagicQuery;
7
use Porpaginas\Page;
8
9
/*
10
 Copyright (C) 2006-2015 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
/**
29
 * Iterator used to retrieve results.
30
 *
31
 */
32
class PageIterator implements Page, \ArrayAccess {
33
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 = null;
61
62
	private $databasePlatform;
63
64
	private $innerResultIterator;
65
66
	public function __construct(ResultIterator $parentResult, $magicSql, array $parameters, $limit, $offset, array $columnDescriptors, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, $mode)
67
	{
68
		$this->parentResult = $parentResult;
69
		$this->magicSql = $magicSql;
70
		$this->objectStorage = $objectStorage;
71
		$this->className = $className;
72
		$this->tdbmService = $tdbmService;
73
		$this->parameters = $parameters;
74
		$this->limit = $limit;
75
		$this->offset = $offset;
76
		$this->columnDescriptors = $columnDescriptors;
77
		$this->magicQuery = $magicQuery;
78
		$this->databasePlatform = $this->tdbmService->getConnection()->getDatabasePlatform();
79
		$this->mode = $mode;
0 ignored issues
show
Bug introduced by
The property mode does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
80
	}
81
82
	/**
83
	 * Retrieve an external iterator
84
	 * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
85
	 * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or
86
	 * <b>Traversable</b>
87
	 * @since 5.0.0
88
	 */
89
	public function getIterator()
90
	{
91
		if ($this->innerResultIterator === null) {
92
			if ($this->mode === TDBMService::MODE_CURSOR) {
93
				$this->innerResultIterator = new InnerResultIterator($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery);
94
			} else {
95
				$this->innerResultIterator = new InnerResultArray($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery);
96
			}
97
98
		}
99
		return $this->innerResultIterator;
100
	}
101
102
	/**
103
	 * @return int
104
	 */
105
	public function getCurrentOffset()
106
	{
107
		return $this->offset;
108
	}
109
110
	/**
111
	 * @return int
112
	 */
113
	public function getCurrentPage()
114
	{
115
		return floor($this->offset / $this->limit) + 1;
116
	}
117
118
	/**
119
	 * @return int
120
	 */
121
	public function getCurrentLimit()
122
	{
123
		return $this->limit;
124
	}
125
126
	/**
127
	 * Return the number of results on the current page of the {@link Result}.
128
	 *
129
	 * @return int
130
	 */
131
	public function count()
132
	{
133
		return $this->getIterator()->count();
134
	}
135
136
	/**
137
	 * Return the number of ALL results in the paginatable of {@link Result}.
138
	 *
139
	 * @return int
140
	 */
141
	public function totalCount()
142
	{
143
		return $this->parentResult->count();
144
	}
145
146
	/**
147
	 * Casts the result set to a PHP array.
148
	 *
149
	 * @return array
150
	 */
151
	public function toArray() {
152
		return iterator_to_array($this->getIterator());
153
	}
154
155
	/**
156
	 * Returns a new iterator mapping any call using the $callable function.
157
	 *
158
	 * @param callable $callable
159
	 * @return MapIterator
160
	 */
161
	public function map(callable $callable) {
162
		return new MapIterator($this->getIterator(), $callable);
163
	}
164
165
	/**
166
	 * Whether a offset exists
167
	 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
168
	 * @param mixed $offset <p>
169
	 * An offset to check for.
170
	 * </p>
171
	 * @return boolean true on success or false on failure.
172
	 * </p>
173
	 * <p>
174
	 * The return value will be casted to boolean if non-boolean was returned.
175
	 * @since 5.0.0
176
	 */
177
	public function offsetExists($offset)
178
	{
179
		return $this->getIterator()->offsetExists($offset);
180
	}
181
182
	/**
183
	 * Offset to retrieve
184
	 * @link http://php.net/manual/en/arrayaccess.offsetget.php
185
	 * @param mixed $offset <p>
186
	 * The offset to retrieve.
187
	 * </p>
188
	 * @return mixed Can return all value types.
189
	 * @since 5.0.0
190
	 */
191
	public function offsetGet($offset)
192
	{
193
		return $this->getIterator()->offsetGet($offset);
194
	}
195
196
	/**
197
	 * Offset to set
198
	 * @link http://php.net/manual/en/arrayaccess.offsetset.php
199
	 * @param mixed $offset <p>
200
	 * The offset to assign the value to.
201
	 * </p>
202
	 * @param mixed $value <p>
203
	 * The value to set.
204
	 * </p>
205
	 * @return void
206
	 * @since 5.0.0
207
	 */
208
	public function offsetSet($offset, $value)
209
	{
210
		return $this->getIterator()->offsetSet($offset, $value);
211
	}
212
213
	/**
214
	 * Offset to unset
215
	 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
216
	 * @param mixed $offset <p>
217
	 * The offset to unset.
218
	 * </p>
219
	 * @return void
220
	 * @since 5.0.0
221
	 */
222
	public function offsetUnset($offset)
223
	{
224
		return $this->getIterator()->offsetUnset($offset);
225
	}
226
}
227