1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Goldoni\Builder; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class QueryResult. |
7
|
|
|
*/ |
8
|
|
|
class QueryResult implements \ArrayAccess, \Iterator, \Countable |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
private $records; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var int |
17
|
|
|
*/ |
18
|
|
|
private $index = 0; |
19
|
|
|
/** |
20
|
|
|
* @var null|string |
21
|
|
|
*/ |
22
|
|
|
private $entity; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The cache of studly-cased words. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected static $hydrateCache = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* QueryResult constructor. |
33
|
|
|
* |
34
|
|
|
* @param array $records |
35
|
|
|
* @param null|string $entity |
36
|
|
|
*/ |
37
|
|
|
public function __construct(array $records, ?string $entity = null) |
38
|
|
|
{ |
39
|
|
|
$this->records = $records; |
40
|
|
|
$this->entity = $entity; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function get(int $index) |
44
|
|
|
{ |
45
|
|
|
if ($this->entity) { |
46
|
|
|
if (isset(static::$hydrateCache[$index])) { |
47
|
|
|
return static::$hydrateCache[$index]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return static::$hydrateCache[$index] = Builder::hydrate($this->records[$index], $this->entity); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->entity; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Return the current element. |
58
|
|
|
* |
59
|
|
|
* @see https://php.net/manual/en/iterator.current.php |
60
|
|
|
* |
61
|
|
|
* @return mixed can return any type |
62
|
|
|
* |
63
|
|
|
* @since 5.0.0 |
64
|
|
|
*/ |
65
|
|
|
public function current() |
66
|
|
|
{ |
67
|
|
|
return $this->get($this->index); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Move forward to next element. |
72
|
|
|
* |
73
|
|
|
* @see https://php.net/manual/en/iterator.next.php |
74
|
|
|
* @since 5.0.0 |
75
|
|
|
*/ |
76
|
|
|
public function next(): void |
77
|
|
|
{ |
78
|
|
|
++$this->index; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return the key of the current element. |
83
|
|
|
* |
84
|
|
|
* @see https://php.net/manual/en/iterator.key.php |
85
|
|
|
* |
86
|
|
|
* @return mixed scalar on success, or null on failure |
87
|
|
|
* |
88
|
|
|
* @since 5.0.0 |
89
|
|
|
*/ |
90
|
|
|
public function key() |
91
|
|
|
{ |
92
|
|
|
return $this->index; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Checks if current position is valid. |
97
|
|
|
* |
98
|
|
|
* @see https://php.net/manual/en/iterator.valid.php |
99
|
|
|
* |
100
|
|
|
* @return bool The return value will be casted to boolean and then evaluated. |
101
|
|
|
* Returns true on success or false on failure. |
102
|
|
|
* |
103
|
|
|
* @since 5.0.0 |
104
|
|
|
*/ |
105
|
|
|
public function valid() |
106
|
|
|
{ |
107
|
|
|
return isset($this->records[$this->index]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Rewind the Iterator to the first element. |
112
|
|
|
* |
113
|
|
|
* @see https://php.net/manual/en/iterator.rewind.php |
114
|
|
|
* @since 5.0.0 |
115
|
|
|
*/ |
116
|
|
|
public function rewind() |
117
|
|
|
{ |
118
|
|
|
$this->index = 0; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Whether a offset exists. |
123
|
|
|
* |
124
|
|
|
* @see https://php.net/manual/en/arrayaccess.offsetexists.php |
125
|
|
|
* |
126
|
|
|
* @param mixed $offset <p> |
127
|
|
|
* An offset to check for. |
128
|
|
|
* </p> |
129
|
|
|
* |
130
|
|
|
* @return bool true on success or false on failure. |
131
|
|
|
* </p> |
132
|
|
|
* <p> |
133
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
134
|
|
|
* |
135
|
|
|
* @since 5.0.0 |
136
|
|
|
*/ |
137
|
|
|
public function offsetExists($offset) |
138
|
|
|
{ |
139
|
|
|
return isset($this->records[$offset]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Offset to retrieve. |
144
|
|
|
* |
145
|
|
|
* @see https://php.net/manual/en/arrayaccess.offsetget.php |
146
|
|
|
* |
147
|
|
|
* @param mixed $offset <p> |
148
|
|
|
* The offset to retrieve. |
149
|
|
|
* </p> |
150
|
|
|
* |
151
|
|
|
* @return mixed can return all value types |
152
|
|
|
* |
153
|
|
|
* @since 5.0.0 |
154
|
|
|
*/ |
155
|
|
|
public function offsetGet($offset) |
156
|
|
|
{ |
157
|
|
|
return $this->get($offset); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Offset to set. |
162
|
|
|
* |
163
|
|
|
* @see https://php.net/manual/en/arrayaccess.offsetset.php |
164
|
|
|
* |
165
|
|
|
* @param mixed $offset <p> |
166
|
|
|
* The offset to assign the value to. |
167
|
|
|
* </p> |
168
|
|
|
* @param mixed $value <p> |
169
|
|
|
* The value to set. |
170
|
|
|
* </p> |
171
|
|
|
* |
172
|
|
|
* @throws \Exception |
173
|
|
|
* |
174
|
|
|
* @since 5.0.0 |
175
|
|
|
*/ |
176
|
|
|
public function offsetSet($offset, $value) |
177
|
|
|
{ |
178
|
|
|
throw new \Exception("Can't alter records"); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Offset to unset. |
183
|
|
|
* |
184
|
|
|
* @see https://php.net/manual/en/arrayaccess.offsetunset.php |
185
|
|
|
* |
186
|
|
|
* @param mixed $offset <p> |
187
|
|
|
* The offset to unset. |
188
|
|
|
* </p> |
189
|
|
|
* |
190
|
|
|
* @throws \Exception |
191
|
|
|
* |
192
|
|
|
* @since 5.0.0 |
193
|
|
|
*/ |
194
|
|
|
public function offsetUnset($offset) |
195
|
|
|
{ |
196
|
|
|
throw new \Exception("Can't alter records"); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Count elements of an object. |
201
|
|
|
* |
202
|
|
|
* @see https://php.net/manual/en/countable.count.php |
203
|
|
|
* |
204
|
|
|
* @return int The custom count as an integer. |
205
|
|
|
* </p> |
206
|
|
|
* <p> |
207
|
|
|
* The return value is cast to an integer. |
208
|
|
|
* |
209
|
|
|
* @since 5.1.0 |
210
|
|
|
*/ |
211
|
|
|
public function count() |
212
|
|
|
{ |
213
|
|
|
return \count($this->records); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|