-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
ResultSet.php
228 lines (204 loc) · 5.11 KB
/
ResultSet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\ORM;
use Cake\Collection\CollectionTrait;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\ResultSetInterface;
use SplFixedArray;
/**
* Represents the results obtained after executing a query for a specific table
* This object is responsible for correctly nesting result keys reported from
* the query, casting each field to the correct type and executing the extra
* queries required for eager loading external associations.
*
* @template T of \Cake\Datasource\EntityInterface|array
* @implements \Cake\Datasource\ResultSetInterface<T>
*/
class ResultSet implements ResultSetInterface
{
use CollectionTrait;
/**
* Points to the next record number that should be fetched
*
* @var int
*/
protected int $_index = 0;
/**
* Last record fetched from the statement
*
* @var \Cake\Datasource\EntityInterface|array|null
* @psalm-var T|null
*/
protected EntityInterface|array|null $_current;
/**
* Holds the count of records in this result set
*
* @var int
*/
protected int $_count = 0;
/**
* Results that have been fetched or hydrated into the results.
*
* @var \SplFixedArray<T>
*/
protected SplFixedArray $_results;
/**
* Constructor
*
* @param array $results Results array.
*/
public function __construct(array $results)
{
$this->__unserialize($results);
}
/**
* Returns the current record in the result iterator.
*
* Part of Iterator interface.
*
* @return \Cake\Datasource\EntityInterface|array|null
* @psalm-return T|null
*/
public function current(): EntityInterface|array|null
{
return $this->_current;
}
/**
* Returns the key of the current record in the iterator.
*
* Part of Iterator interface.
*
* @return int
*/
public function key(): int
{
return $this->_index;
}
/**
* Advances the iterator pointer to the next record.
*
* Part of Iterator interface.
*
* @return void
*/
public function next(): void
{
$this->_index++;
}
/**
* Rewinds a ResultSet.
*
* Part of Iterator interface.
*
* @return void
*/
public function rewind(): void
{
$this->_index = 0;
}
/**
* Whether there are more results to be fetched from the iterator.
*
* Part of Iterator interface.
*
* @return bool
*/
public function valid(): bool
{
if ($this->_index < $this->_count) {
$this->_current = $this->_results[$this->_index];
return true;
}
return false;
}
/**
* Get the first record from a result set.
*
* This method will also close the underlying statement cursor.
*
* @return \Cake\Datasource\EntityInterface|array|null
* @psalm-return T|null
*/
public function first(): EntityInterface|array|null
{
foreach ($this as $result) {
return $result;
}
return null;
}
/**
* Serializes a resultset.
*
* @return array
*/
public function __serialize(): array
{
return $this->_results->toArray();
}
/**
* Unserializes a resultset.
*
* @param array $data Data array.
* @return void
*/
public function __unserialize(array $data): void
{
$this->_results = SplFixedArray::fromArray($data);
$this->_count = $this->_results->count();
}
/**
* Gives the number of rows in the result set.
*
* Part of the Countable interface.
*
* @return int
*/
public function count(): int
{
return $this->_count;
}
/**
* @inheritDoc
*/
public function countKeys(): int
{
// This is an optimization over the implementation provided by CollectionTrait::countKeys()
return $this->_count;
}
/**
* @inheritDoc
*/
public function isEmpty(): bool
{
return !$this->_count;
}
/**
* Returns an array that can be used to describe the internal state of this
* object.
*
* @return array<string, mixed>
*/
public function __debugInfo(): array
{
$currentIndex = $this->_index;
// toArray() adjusts the current index, so we have to reset it
$items = $this->toArray();
$this->_index = $currentIndex;
return [
'items' => $items,
];
}
}