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

InnerResultArray::rewind()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4286
cc 2
eloc 5
nc 2
nop 0
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
8
/*
9
 Copyright (C) 2006-2015 David Négrier - THE CODING MACHINE
10
11
 This program is free software; you can redistribute it and/or modify
12
 it under the terms of the GNU General Public License as published by
13
 the Free Software Foundation; either version 2 of the License, or
14
 (at your option) any later version.
15
16
 This program is distributed in the hope that it will be useful,
17
 but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 GNU General Public License for more details.
20
21
 You should have received a copy of the GNU General Public License
22
 along with this program; if not, write to the Free Software
23
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24
 */
25
26
27
/**
28
 * Iterator used to retrieve results. It behaves like an array.
29
 *
30
 */
31
class InnerResultArray extends InnerResultIterator {
32
33
	/**
34
	 * The list of results already fetched.
35
	 *
36
	 * @var AbstractTDBMObject[]
37
	 */
38
	private $results = [];
39
40
	/**
41
	 * Whether a offset exists
42
	 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
43
	 * @param mixed $offset <p>
44
	 * An offset to check for.
45
	 * </p>
46
	 * @return boolean true on success or false on failure.
47
	 * </p>
48
	 * <p>
49
	 * The return value will be casted to boolean if non-boolean was returned.
50
	 * @since 5.0.0
51
	 */
52
	public function offsetExists($offset)
53
	{
54
		try {
55
			$this->toIndex($offset);
56
		} catch (TDBMInvalidOffsetException $e) {
57
			return false;
58
		}
59
60
		return true;
61
	}
62
63
	/**
64
	 * Offset to retrieve
65
	 * @link http://php.net/manual/en/arrayaccess.offsetget.php
66
	 * @param mixed $offset <p>
67
	 * The offset to retrieve.
68
	 * </p>
69
	 * @return mixed Can return all value types.
70
	 * @since 5.0.0
71
	 */
72
	public function offsetGet($offset)
73
	{
74
		$this->toIndex($offset);
75
		return $this->results[$offset];
76
	}
77
78
79
	private function toIndex($offset) {
80
		if ($offset < 0 || filter_var($offset, FILTER_VALIDATE_INT) === false) {
81
			throw new TDBMInvalidOffsetException('Trying to access result set using offset "'.$offset.'". An offset must be a positive integer.');
82
		}
83
		if ($this->statement === null) {
84
			$this->executeQuery();
85
		}
86
		while (!isset($this->results[$offset])) {
87
			$this->next();
88
			if ($this->current === null) {
89
				throw new TDBMInvalidOffsetException('Offset "'.$offset.'" does not exist in result set.');
90
			}
91
		}
92
	}
93
94
	public function next() {
95
		// Let's overload the next() method to store the result.
96
		if (isset($this->results[$this->key+1])) {
97
			$this->key++;
98
			$this->current = $this->results[$this->key];
99
		} else {
100
			parent::next();
101
			if ($this->current !== null) {
102
				$this->results[$this->key] = $this->current;
103
			}
104
		}
105
	}
106
107
	/**
108
	 * Overloads the rewind implementation.
109
	 * Do not reexecute the query.
110
	 */
111
	public function rewind()
112
	{
113
		if (!$this->fetchStarted) {
114
			$this->executeQuery();
115
		}
116
		$this->key = -1;
117
		$this->next();
118
	}
119
}
120