Passed
Pull Request — develop (#3453)
by Evgeniy
19:01
created

ArrayStatement::fetchColumn()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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