Passed
Pull Request — master (#3833)
by Benjamin
08:37
created

ArrayStatement::setFetchMode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
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 648
    public function __construct(array $data)
38
    {
39 648
        $this->data = $data;
40 648
        if (! count($data)) {
41 108
            return;
42
        }
43
44 540
        $this->columnCount = count($data[0]);
45 540
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 216
    public function closeCursor() : void
51
    {
52 216
        $this->data = [];
53 216
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 135
    public function columnCount() : int
59
    {
60 135
        return $this->columnCount;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 54
    public function rowCount() : int
67
    {
68 54
        return count($this->data);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 378
    public function setFetchMode(int $fetchMode, ...$args) : void
75
    {
76 378
        if (count($args) > 0) {
77 27
            throw new InvalidArgumentException('Caching layer does not support 2nd/3rd argument to setFetchMode().');
78
        }
79
80 351
        $this->defaultFetchMode = $fetchMode;
81 351
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 54
    public function getIterator()
87
    {
88 54
        $data = $this->fetchAll();
89
90 54
        return new ArrayIterator($data);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 459
    public function fetch(?int $fetchMode = null, ...$args)
97
    {
98 459
        if (! isset($this->data[$this->num])) {
99 270
            return false;
100
        }
101
102 432
        $row       = $this->data[$this->num++];
103 432
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
104
105 432
        if ($fetchMode === FetchMode::ASSOCIATIVE) {
106 162
            return $row;
107
        }
108
109 297
        if ($fetchMode === FetchMode::NUMERIC) {
110 135
            return array_values($row);
111
        }
112
113 189
        if ($fetchMode === FetchMode::MIXED) {
114 81
            return array_merge($row, array_values($row));
115
        }
116
117 108
        if ($fetchMode === FetchMode::COLUMN) {
118 81
            return reset($row);
119
        }
120
121 27
        throw new InvalidArgumentException(
122 27
            sprintf('Invalid fetch mode given for fetching result, %d given.', $fetchMode)
123
        );
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 108
    public function fetchAll(?int $fetchMode = null, ...$args) : array
130
    {
131 108
        $rows = [];
132 108
        while ($row = $this->fetch($fetchMode, ...$args)) {
133 108
            $rows[] = $row;
134
        }
135
136 108
        return $rows;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 27
    public function fetchColumn(int $columnIndex = 0)
143
    {
144 27
        $row = $this->fetch(FetchMode::NUMERIC);
145
146 27
        if ($row === false) {
147
            return false;
148
        }
149
150 27
        if (! array_key_exists($columnIndex, $row)) {
151
            throw InvalidColumnIndex::new($columnIndex, count($row));
152
        }
153
154 27
        return $row[$columnIndex];
155
    }
156
}
157