|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Connection\Result; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Prime\Exception\DBALException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Wrap simple associative array to ResultSet |
|
9
|
|
|
* This result is usefull for caches |
|
10
|
|
|
*/ |
|
11
|
|
|
final class ArrayResultSet extends \ArrayIterator implements ResultSetInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $fetchMode = self::FETCH_ASSOC; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var mixed |
|
20
|
|
|
*/ |
|
21
|
|
|
private $fetchOptions; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private $columns; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var \ReflectionClass |
|
30
|
|
|
*/ |
|
31
|
|
|
private $reflectionClass; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var \ReflectionProperty[] |
|
35
|
|
|
*/ |
|
36
|
|
|
private $reflectionProperties; |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
|
|
|
|
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
5 |
|
public function fetchMode($mode, $options = null) |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
5 |
|
$this->fetchMode = $mode; |
|
45
|
5 |
|
$this->fetchOptions = $options; |
|
46
|
|
|
|
|
47
|
5 |
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function asAssociative(): ResultSetInterface |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->fetchMode(self::FETCH_ASSOC); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function asList(): ResultSetInterface |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->fetchMode(self::FETCH_NUM); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function asObject(): ResultSetInterface |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->fetchMode(self::FETCH_OBJECT); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
|
|
|
|
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function asClass(string $className): ResultSetInterface |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->fetchMode(self::FETCH_CLASS, $className); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
|
|
|
|
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function asColumn(int $column = 0): ResultSetInterface |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->fetchMode(self::FETCH_COLUMN, $column); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
5 |
|
public function all() |
|
94
|
|
|
{ |
|
95
|
5 |
|
return $this->fetchMode === self::FETCH_ASSOC |
|
|
|
|
|
|
96
|
1 |
|
? $this->getArrayCopy() |
|
|
|
|
|
|
97
|
5 |
|
: array_map([$this, 'fetchValue'], $this->getArrayCopy()) |
|
|
|
|
|
|
98
|
|
|
; |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
3 |
|
public function current() |
|
105
|
|
|
{ |
|
106
|
3 |
|
return $this->fetchValue(parent::current()); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Transform the value according to the fetch mode |
|
111
|
|
|
* |
|
112
|
|
|
* @param array $current |
|
113
|
|
|
* |
|
114
|
|
|
* @return mixed |
|
115
|
|
|
*/ |
|
|
|
|
|
|
116
|
7 |
|
private function fetchValue($current) |
|
|
|
|
|
|
117
|
|
|
{ |
|
118
|
7 |
|
switch ($this->fetchMode) { |
|
119
|
7 |
|
case self::FETCH_ASSOC: |
|
|
|
|
|
|
120
|
2 |
|
return $current; |
|
|
|
|
|
|
121
|
|
|
|
|
122
|
5 |
|
case self::FETCH_NUM: |
|
|
|
|
|
|
123
|
1 |
|
return array_values($current); |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
4 |
|
case self::FETCH_COLUMN: |
|
|
|
|
|
|
126
|
2 |
|
return $this->fetchColum($current); |
|
|
|
|
|
|
127
|
|
|
|
|
128
|
2 |
|
case self::FETCH_OBJECT: |
|
|
|
|
|
|
129
|
1 |
|
return (object) $current; |
|
|
|
|
|
|
130
|
|
|
|
|
131
|
1 |
|
case self::FETCH_CLASS: |
|
|
|
|
|
|
132
|
1 |
|
return $this->fetchClass($current); |
|
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
default: |
|
135
|
|
|
throw new DBALException('Unsupported fetch mode '.$this->fetchMode); |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* {@inheritdoc} |
|
141
|
|
|
*/ |
|
142
|
|
|
public function isRead(): bool |
|
143
|
|
|
{ |
|
144
|
|
|
return true; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* {@inheritdoc} |
|
149
|
|
|
*/ |
|
150
|
|
|
public function isWrite(): bool |
|
151
|
|
|
{ |
|
152
|
|
|
return false; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* {@inheritdoc} |
|
157
|
|
|
*/ |
|
158
|
|
|
public function hasWrite(): bool |
|
159
|
|
|
{ |
|
160
|
|
|
return false; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
2 |
|
private function fetchColum($current) |
|
|
|
|
|
|
164
|
|
|
{ |
|
165
|
2 |
|
if (!$this->fetchOptions) { |
|
166
|
2 |
|
return reset($current); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
1 |
|
if (!$this->columns) { |
|
|
|
|
|
|
170
|
1 |
|
$this->columns = array_keys($current); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
1 |
|
return $current[$this->columns[$this->fetchOptions]]; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
1 |
|
private function fetchClass($current) |
|
|
|
|
|
|
177
|
|
|
{ |
|
178
|
1 |
|
if (!$this->reflectionClass) { |
|
179
|
1 |
|
$this->reflectionClass = new \ReflectionClass($this->fetchOptions); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
1 |
|
$object = $this->reflectionClass->newInstance(); |
|
183
|
|
|
|
|
184
|
1 |
|
foreach ($current as $property => $value) { |
|
185
|
1 |
|
if (!isset($this->reflectionProperties[$property])) { |
|
186
|
1 |
|
$this->reflectionProperties[$property] = $this->reflectionClass->getProperty($property); |
|
187
|
1 |
|
$this->reflectionProperties[$property]->setAccessible(true); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
1 |
|
$this->reflectionProperties[$property]->setValue($object, $value); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
1 |
|
return $object; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|