Failed Conditions
Push — master ( ac0e13...24dbc4 )
by Sergei
22s queued 15s
created

ArrayStatement::fetch()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 6
nop 2
dl 0
loc 27
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Cache;
6
7
use ArrayIterator;
8
use Doctrine\DBAL\Driver\ResultStatement;
9
use Doctrine\DBAL\Exception\InvalidColumnIndex;
10
use Doctrine\DBAL\FetchMode;
11
use InvalidArgumentException;
12
use IteratorAggregate;
13
use function array_key_exists;
14
use function array_merge;
15
use function array_values;
16
use function count;
17
use function reset;
18
use function sprintf;
19
20
final class ArrayStatement implements IteratorAggregate, ResultStatement
21
{
22
    /** @var mixed[] */
23
    private $data;
24
25
    /** @var int */
26
    private $columnCount = 0;
27
28
    /** @var int */
29
    private $num = 0;
30
31
    /** @var int */
32
    private $defaultFetchMode = FetchMode::MIXED;
33
34
    /**
35
     * @param mixed[] $data
36
     */
37
    public function __construct(array $data)
38
    {
39
        $this->data = $data;
40
        if (count($data) === 0) {
41
            return;
42
        }
43
44
        $this->columnCount = count($data[0]);
45
    }
46
47
    public function closeCursor() : void
48
    {
49
        $this->data = [];
50
    }
51
52
    public function columnCount() : int
53
    {
54
        return $this->columnCount;
55
    }
56
57
    public function rowCount() : int
58
    {
59
        return count($this->data);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function setFetchMode(int $fetchMode, ...$args) : void
66
    {
67
        if (count($args) > 0) {
68
            throw new InvalidArgumentException('Caching layer does not support 2nd/3rd argument to setFetchMode().');
69
        }
70
71
        $this->defaultFetchMode = $fetchMode;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getIterator()
78
    {
79
        $data = $this->fetchAll();
80
81
        return new ArrayIterator($data);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function fetch(?int $fetchMode = null, ...$args)
88
    {
89
        if (! isset($this->data[$this->num])) {
90
            return false;
91
        }
92
93
        $row       = $this->data[$this->num++];
94
        $fetchMode = $fetchMode ?? $this->defaultFetchMode;
95
96
        if ($fetchMode === FetchMode::ASSOCIATIVE) {
97
            return $row;
98
        }
99
100
        if ($fetchMode === FetchMode::NUMERIC) {
101
            return array_values($row);
102
        }
103
104
        if ($fetchMode === FetchMode::MIXED) {
105
            return array_merge($row, array_values($row));
106
        }
107
108
        if ($fetchMode === FetchMode::COLUMN) {
109
            return reset($row);
110
        }
111
112
        throw new InvalidArgumentException(
113
            sprintf('Invalid fetch mode given for fetching result, %d given.', $fetchMode)
114
        );
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function fetchAll(?int $fetchMode = null, ...$args) : array
121
    {
122
        $rows = [];
123
        while ($row = $this->fetch($fetchMode, ...$args)) {
124
            $rows[] = $row;
125
        }
126
127
        return $rows;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function fetchColumn(int $columnIndex = 0)
134
    {
135
        $row = $this->fetch(FetchMode::NUMERIC);
136
137
        if ($row === false) {
138
            return false;
139
        }
140
141
        if (! array_key_exists($columnIndex, $row)) {
142
            throw InvalidColumnIndex::new($columnIndex, count($row));
143
        }
144
145
        return $row[$columnIndex];
146
    }
147
}
148