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