InnerResultArray::rewind()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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