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