ArrayStatement.php 3.66 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 23 24
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Cache;

use Doctrine\DBAL\Driver\ResultStatement;
use PDO;

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

    /**
     * @var integer
     */
35
    private $columnCount = 0;
Benjamin Morel's avatar
Benjamin Morel committed
36 37 38 39

    /**
     * @var integer
     */
40
    private $num = 0;
Benjamin Morel's avatar
Benjamin Morel committed
41 42 43 44

    /**
     * @var integer
     */
45
    private $defaultFetchMode = PDO::FETCH_BOTH;
46

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

Benjamin Morel's avatar
Benjamin Morel committed
58 59 60
    /**
     * {@inheritdoc}
     */
61 62 63 64 65
    public function closeCursor()
    {
        unset ($this->data);
    }

Benjamin Morel's avatar
Benjamin Morel committed
66 67 68
    /**
     * {@inheritdoc}
     */
69 70 71 72 73
    public function columnCount()
    {
        return $this->columnCount;
    }

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

83
        $this->defaultFetchMode = $fetchMode;
Benjamin Morel's avatar
Benjamin Morel committed
84 85

        return true;
86 87
    }

Benjamin Morel's avatar
Benjamin Morel committed
88 89 90
    /**
     * {@inheritdoc}
     */
91 92
    public function getIterator()
    {
93
        $data = $this->fetchAll();
Benjamin Morel's avatar
Benjamin Morel committed
94

95 96 97
        return new \ArrayIterator($data);
    }

Benjamin Morel's avatar
Benjamin Morel committed
98 99 100
    /**
     * {@inheritdoc}
     */
101
    public function fetch($fetchMode = null)
102 103 104
    {
        if (isset($this->data[$this->num])) {
            $row = $this->data[$this->num++];
105 106
            $fetchMode = $fetchMode ?: $this->defaultFetchMode;
            if ($fetchMode === PDO::FETCH_ASSOC) {
107
                return $row;
Steve Müller's avatar
Steve Müller committed
108
            } elseif ($fetchMode === PDO::FETCH_NUM) {
109
                return array_values($row);
Steve Müller's avatar
Steve Müller committed
110
            } elseif ($fetchMode === PDO::FETCH_BOTH) {
111
                return array_merge($row, array_values($row));
Steve Müller's avatar
Steve Müller committed
112
            } elseif ($fetchMode === PDO::FETCH_COLUMN) {
113 114 115
                return reset($row);
            } else {
                throw new \InvalidArgumentException("Invalid fetch-style given for fetching result.");
116 117
            }
        }
Benjamin Morel's avatar
Benjamin Morel committed
118

119 120 121
        return false;
    }

Benjamin Morel's avatar
Benjamin Morel committed
122 123 124
    /**
     * {@inheritdoc}
     */
125
    public function fetchAll($fetchMode = null)
126 127
    {
        $rows = array();
128
        while ($row = $this->fetch($fetchMode)) {
129 130
            $rows[] = $row;
        }
Benjamin Morel's avatar
Benjamin Morel committed
131

132 133 134
        return $rows;
    }

Benjamin Morel's avatar
Benjamin Morel committed
135 136 137
    /**
     * {@inheritdoc}
     */
138 139 140 141 142 143 144
    public function fetchColumn($columnIndex = 0)
    {
        $row = $this->fetch(PDO::FETCH_NUM);
        if (!isset($row[$columnIndex])) {
            // TODO: verify this is correct behavior
            return false;
        }
Benjamin Morel's avatar
Benjamin Morel committed
145

146 147
        return $row[$columnIndex];
    }
148
}