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 PDO; |
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
|
69 |
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
138 |
|
$this->columnCount = count($data[0]); |
42
|
138 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
161 |
|
public function closeCursor() |
48
|
|
|
{ |
49
|
161 |
|
unset($this->data); |
50
|
161 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
92 |
|
public function columnCount() |
56
|
|
|
{ |
57
|
92 |
|
return $this->columnCount; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
207 |
|
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) |
64
|
|
|
{ |
65
|
207 |
|
if ($arg2 !== null || $arg3 !== null) { |
66
|
|
|
throw new InvalidArgumentException('Caching layer does not support 2nd/3rd argument to setFetchMode()'); |
67
|
|
|
} |
68
|
|
|
|
69
|
207 |
|
$this->defaultFetchMode = $fetchMode; |
70
|
|
|
|
71
|
207 |
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
23 |
|
public function getIterator() |
78
|
|
|
{ |
79
|
23 |
|
$data = $this->fetchAll(); |
80
|
|
|
|
81
|
23 |
|
return new ArrayIterator($data); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
161 |
|
public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) |
88
|
|
|
{ |
89
|
161 |
|
if (! isset($this->data[$this->num])) { |
90
|
161 |
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
138 |
|
$row = $this->data[$this->num++]; |
94
|
138 |
|
$fetchMode = $fetchMode ?: $this->defaultFetchMode; |
95
|
|
|
|
96
|
138 |
|
if ($fetchMode === FetchMode::ASSOCIATIVE) { |
97
|
46 |
|
return $row; |
98
|
|
|
} |
99
|
|
|
|
100
|
115 |
|
if ($fetchMode === FetchMode::NUMERIC) { |
101
|
69 |
|
return array_values($row); |
102
|
|
|
} |
103
|
|
|
|
104
|
69 |
|
if ($fetchMode === FetchMode::MIXED) { |
105
|
46 |
|
return array_merge($row, array_values($row)); |
106
|
|
|
} |
107
|
|
|
|
108
|
23 |
|
if ($fetchMode === FetchMode::COLUMN) { |
109
|
23 |
|
return reset($row); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
throw new InvalidArgumentException('Invalid fetch-style given for fetching result.'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
23 |
|
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) |
119
|
|
|
{ |
120
|
23 |
|
$rows = []; |
121
|
23 |
|
while ($row = $this->fetch($fetchMode)) { |
122
|
23 |
|
$rows[] = $row; |
123
|
|
|
} |
124
|
|
|
|
125
|
23 |
|
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 ($columnIndex >= count($row)) { |
140
|
|
|
throw DBALException::invalidColumnIndex($columnIndex, count($row)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $row[$columnIndex]; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|