ArrayStatement.php 2.86 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Cache;

5
use ArrayIterator;
6
use Doctrine\DBAL\Driver\ResultStatement;
7
use Doctrine\DBAL\FetchMode;
8 9 10
use InvalidArgumentException;
use IteratorAggregate;
use PDO;
11 12 13 14
use function array_merge;
use function array_values;
use function count;
use function reset;
15

16
class ArrayStatement implements IteratorAggregate, ResultStatement
17
{
18
    /** @var mixed[] */
19
    private $data;
Benjamin Morel's avatar
Benjamin Morel committed
20

21
    /** @var int */
22
    private $columnCount = 0;
Benjamin Morel's avatar
Benjamin Morel committed
23

24
    /** @var int */
25
    private $num = 0;
Benjamin Morel's avatar
Benjamin Morel committed
26

27
    /** @var int */
28
    private $defaultFetchMode = FetchMode::MIXED;
29

Benjamin Morel's avatar
Benjamin Morel committed
30
    /**
31
     * @param mixed[] $data
Benjamin Morel's avatar
Benjamin Morel committed
32
     */
33 34 35
    public function __construct(array $data)
    {
        $this->data = $data;
36 37
        if (! count($data)) {
            return;
38
        }
39 40

        $this->columnCount = count($data[0]);
41 42
    }

Benjamin Morel's avatar
Benjamin Morel committed
43 44 45
    /**
     * {@inheritdoc}
     */
46 47
    public function closeCursor()
    {
48
        unset($this->data);
49 50
    }

Benjamin Morel's avatar
Benjamin Morel committed
51 52 53
    /**
     * {@inheritdoc}
     */
54 55 56 57 58
    public function columnCount()
    {
        return $this->columnCount;
    }

Benjamin Morel's avatar
Benjamin Morel committed
59 60 61
    /**
     * {@inheritdoc}
     */
62
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
63
    {
64
        if ($arg2 !== null || $arg3 !== null) {
65
            throw new InvalidArgumentException('Caching layer does not support 2nd/3rd argument to setFetchMode()');
66 67
        }

68
        $this->defaultFetchMode = $fetchMode;
Benjamin Morel's avatar
Benjamin Morel committed
69 70

        return true;
71 72
    }

Benjamin Morel's avatar
Benjamin Morel committed
73 74 75
    /**
     * {@inheritdoc}
     */
76 77
    public function getIterator()
    {
78
        $data = $this->fetchAll();
Benjamin Morel's avatar
Benjamin Morel committed
79

80
        return new ArrayIterator($data);
81 82
    }

Benjamin Morel's avatar
Benjamin Morel committed
83 84 85
    /**
     * {@inheritdoc}
     */
86
    public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
87
    {
Sergei Morozov's avatar
Sergei Morozov committed
88
        if (! isset($this->data[$this->num])) {
89
            return false;
90
        }
Benjamin Morel's avatar
Benjamin Morel committed
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        $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);
        }

111
        throw new InvalidArgumentException('Invalid fetch-style given for fetching result.');
112 113
    }

Benjamin Morel's avatar
Benjamin Morel committed
114 115 116
    /**
     * {@inheritdoc}
     */
117
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
118
    {
119
        $rows = [];
120
        while ($row = $this->fetch($fetchMode)) {
121 122
            $rows[] = $row;
        }
Benjamin Morel's avatar
Benjamin Morel committed
123

124 125 126
        return $rows;
    }

Benjamin Morel's avatar
Benjamin Morel committed
127 128 129
    /**
     * {@inheritdoc}
     */
130 131
    public function fetchColumn($columnIndex = 0)
    {
132
        $row = $this->fetch(FetchMode::NUMERIC);
Benjamin Morel's avatar
Benjamin Morel committed
133

134
        // TODO: verify that return false is the correct behavior
135
        return $row[$columnIndex] ?? false;
136
    }
137
}