/
Resultset.php
207 lines (183 loc) · 4.15 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
<?php
namespace Soluble\DbWrapper\Result;
use ArrayObject;
use Soluble\DbWrapper\Exception;
class Resultset implements ResultInterface
{
const TYPE_ARRAYOBJECT = 'arrayobject';
const TYPE_ARRAY = 'array';
/**
* Allowed return types.
*
* @var array
*/
protected $allowedReturnTypes = [
self::TYPE_ARRAYOBJECT,
self::TYPE_ARRAY,
];
/**
* Return type to use when returning an object from the set.
*
* @var string ResultSet::TYPE_ARRAYOBJECT|ResultSet::TYPE_ARRAY
*/
protected $returnType = self::TYPE_ARRAY;
/**
* @var int
*/
protected $position = 0;
/**
* @var int
*/
protected $count = 0;
/**
* @var array|ArrayObject
*/
protected $storage;
/**
* Constructor.
*
* @throws Exception\InvalidArgumentException
*
* @param string $returnType
*/
public function __construct($returnType = self::TYPE_ARRAY)
{
if (!in_array($returnType, $this->allowedReturnTypes)) {
throw new Exception\InvalidArgumentException("Unsupported returnType argument ($returnType)");
}
$this->returnType = $returnType;
if ($this->returnType === self::TYPE_ARRAYOBJECT) {
$this->storage = new ArrayObject([]);
} else {
$this->storage = [];
}
$this->position = 0;
$this->count = count($this->storage);
}
/**
* {@inheritdoc}
*
* @return array|ArrayObject
*/
public function current()
{
return $this->storage[$this->position];
}
/**
* {@inheritdoc}
*
* @return int position
*/
public function key()
{
return $this->position;
}
/**
* {@inheritdoc}
*/
public function next()
{
++$this->position;
}
/**
* {@inheritdoc}
*/
public function rewind()
{
$this->position = 0;
}
/**
* {@inheritdoc}
*/
public function valid()
{
return isset($this->storage[$this->position]);
}
/**
* {@inheritdoc}
*/
public function count()
{
return $this->count;
}
/**
* Append a row to the end of resultset.
*
* @param array $row an associative array
*/
public function append(array $row)
{
if ($this->returnType == self::TYPE_ARRAYOBJECT) {
$this->storage[] = new ArrayObject($row);
} else {
$this->storage[] = $row;
}
++$this->count;
}
/**
* {@inheritdoc}
*/
public function offsetExists($position)
{
return isset($this->storage[$position]);
}
/**
* {@inheritdoc}
*/
public function offsetGet($position)
{
return isset($this->storage[$position]) ? $this->storage[$position] : null;
}
/**
* {@inheritdoc}
*/
public function offsetSet($position, $row)
{
throw new \Exception('Resultsets are immutable');
}
/**
* {@inheritdoc}
*/
public function offsetUnset($position)
{
throw new \Exception('Resultsets are immutable');
}
/**
* Return underlying stored resultset as array.
*
* @return array
*/
public function getArray()
{
return (array) $this->storage;
}
/**
* Return underlying stored resultset as ArrayObject.
*
* Depending on the $returnType Resultset::TYPE_ARRAY|Resultset::TYPE_ARRAYOBJECT you can modify
* the internal storage
*
* @return ArrayObject
*/
public function getArrayObject()
{
if ($this->returnType == self::TYPE_ARRAY) {
return new ArrayObject($this->storage);
} else {
/** @var ArrayObject $storageAsArrayObject to silent static code analyzers */
$storageAsArrayObject = $this->storage;
return $storageAsArrayObject;
}
}
/**
* Return the currently set return type.
*
* @see Resultset::TYPE_ARRAY|Resultset::TYPE_ARRAYOBJECT
*
* @return string
*/
public function getReturnType()
{
return $this->returnType;
}
}