3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\DBAL\Cache;
22 use Doctrine\DBAL\Driver\ResultStatement;
25 class ArrayStatement implements \IteratorAggregate, ResultStatement
28 private $columnCount = 0;
30 private $defaultFetchMode = PDO::FETCH_BOTH;
32 public function __construct(array $data)
36 $this->columnCount = count($data[0]);
40 public function closeCursor()
45 public function columnCount()
47 return $this->columnCount;
50 public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
52 if ($arg2 !== null || $arg3 !== null) {
53 throw new \InvalidArgumentException("Caching layer does not support 2nd/3rd argument to setFetchMode()");
56 $this->defaultFetchMode = $fetchMode;
59 public function getIterator()
61 $data = $this->fetchAll();
62 return new \ArrayIterator($data);
65 public function fetch($fetchMode = null)
67 if (isset($this->data[$this->num])) {
68 $row = $this->data[$this->num++];
69 $fetchMode = $fetchMode ?: $this->defaultFetchMode;
70 if ($fetchMode === PDO::FETCH_ASSOC) {
72 } else if ($fetchMode === PDO::FETCH_NUM) {
73 return array_values($row);
74 } else if ($fetchMode === PDO::FETCH_BOTH) {
75 return array_merge($row, array_values($row));
76 } else if ($fetchMode === PDO::FETCH_COLUMN) {
79 throw new \InvalidArgumentException("Invalid fetch-style given for fetching result.");
85 public function fetchAll($fetchMode = null)
88 while ($row = $this->fetch($fetchMode)) {
94 public function fetchColumn($columnIndex = 0)
96 $row = $this->fetch(PDO::FETCH_NUM);
97 if (!isset($row[$columnIndex])) {
98 // TODO: verify this is correct behavior
101 return $row[$columnIndex];