Completed
Pull Request — develop (#3483)
by Sergei
64:56 queued 62:07
created

ArrayStatement::rowCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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