ArrayStatement.php 3.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17 18 19 20 21 22
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Cache;

use Doctrine\DBAL\Driver\ResultStatement;
23
use Doctrine\DBAL\FetchMode;
24 25 26 27
use function array_merge;
use function array_values;
use function count;
use function reset;
28

29
class ArrayStatement implements \IteratorAggregate, ResultStatement
30
{
Benjamin Morel's avatar
Benjamin Morel committed
31 32 33
    /**
     * @var array
     */
34
    private $data;
Benjamin Morel's avatar
Benjamin Morel committed
35 36

    /**
37
     * @var int
Benjamin Morel's avatar
Benjamin Morel committed
38
     */
39
    private $columnCount = 0;
Benjamin Morel's avatar
Benjamin Morel committed
40 41

    /**
42
     * @var int
Benjamin Morel's avatar
Benjamin Morel committed
43
     */
44
    private $num = 0;
Benjamin Morel's avatar
Benjamin Morel committed
45 46

    /**
47
     * @var int
Benjamin Morel's avatar
Benjamin Morel committed
48
     */
49
    private $defaultFetchMode = FetchMode::MIXED;
50

Benjamin Morel's avatar
Benjamin Morel committed
51 52 53
    /**
     * @param array $data
     */
54 55 56 57 58 59 60 61
    public function __construct(array $data)
    {
        $this->data = $data;
        if (count($data)) {
            $this->columnCount = count($data[0]);
        }
    }

Benjamin Morel's avatar
Benjamin Morel committed
62 63 64
    /**
     * {@inheritdoc}
     */
65 66 67 68 69
    public function closeCursor()
    {
        unset ($this->data);
    }

Benjamin Morel's avatar
Benjamin Morel committed
70 71 72
    /**
     * {@inheritdoc}
     */
73 74 75 76 77
    public function columnCount()
    {
        return $this->columnCount;
    }

Benjamin Morel's avatar
Benjamin Morel committed
78 79 80
    /**
     * {@inheritdoc}
     */
81
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
82
    {
83 84 85 86
        if ($arg2 !== null || $arg3 !== null) {
            throw new \InvalidArgumentException("Caching layer does not support 2nd/3rd argument to setFetchMode()");
        }

87
        $this->defaultFetchMode = $fetchMode;
Benjamin Morel's avatar
Benjamin Morel committed
88 89

        return true;
90 91
    }

Benjamin Morel's avatar
Benjamin Morel committed
92 93 94
    /**
     * {@inheritdoc}
     */
95 96
    public function getIterator()
    {
97
        $data = $this->fetchAll();
Benjamin Morel's avatar
Benjamin Morel committed
98

99 100 101
        return new \ArrayIterator($data);
    }

Benjamin Morel's avatar
Benjamin Morel committed
102 103 104
    /**
     * {@inheritdoc}
     */
105
    public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
106
    {
Sergei Morozov's avatar
Sergei Morozov committed
107
        if (! isset($this->data[$this->num])) {
108
            return false;
109
        }
Benjamin Morel's avatar
Benjamin Morel committed
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        $row       = $this->data[$this->num++];
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;

        if ($fetchMode === FetchMode::ASSOCIATIVE) {
            return $row;
        }

        if ($fetchMode === FetchMode::NUMERIC) {
            return array_values($row);
        }

        if ($fetchMode === FetchMode::MIXED) {
            return array_merge($row, array_values($row));
        }

        if ($fetchMode === FetchMode::COLUMN) {
            return reset($row);
        }

Sergei Morozov's avatar
Sergei Morozov committed
130
        throw new \InvalidArgumentException('Invalid fetch-style given for fetching result.');
131 132
    }

Benjamin Morel's avatar
Benjamin Morel committed
133 134 135
    /**
     * {@inheritdoc}
     */
136
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
137
    {
138
        $rows = [];
139
        while ($row = $this->fetch($fetchMode)) {
140 141
            $rows[] = $row;
        }
Benjamin Morel's avatar
Benjamin Morel committed
142

143 144 145
        return $rows;
    }

Benjamin Morel's avatar
Benjamin Morel committed
146 147 148
    /**
     * {@inheritdoc}
     */
149 150
    public function fetchColumn($columnIndex = 0)
    {
151
        $row = $this->fetch(FetchMode::NUMERIC);
Benjamin Morel's avatar
Benjamin Morel committed
152

153
        // TODO: verify that return false is the correct behavior
154
        return $row[$columnIndex] ?? false;
155
    }
156
}