1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soluble\DbWrapper\Result; |
4
|
|
|
|
5
|
|
|
use ArrayObject; |
6
|
|
|
use Soluble\DbWrapper\Exception; |
7
|
|
|
|
8
|
|
|
class Resultset implements ResultInterface |
9
|
|
|
{ |
10
|
|
|
const TYPE_ARRAYOBJECT = 'arrayobject'; |
11
|
|
|
const TYPE_ARRAY = 'array'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Allowed return types. |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $allowedReturnTypes = [ |
19
|
|
|
self::TYPE_ARRAYOBJECT, |
20
|
|
|
self::TYPE_ARRAY, |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Return type to use when returning an object from the set. |
25
|
|
|
* |
26
|
|
|
* @var string ResultSet::TYPE_ARRAYOBJECT|ResultSet::TYPE_ARRAY |
27
|
|
|
*/ |
28
|
|
|
protected $returnType = self::TYPE_ARRAY; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
protected $position = 0; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
protected $count = 0; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array|ArrayObject |
42
|
|
|
*/ |
43
|
|
|
protected $storage; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor. |
47
|
|
|
* |
48
|
|
|
* @throws Exception\InvalidArgumentException |
49
|
|
|
* |
50
|
|
|
* @param string $returnType |
51
|
|
|
*/ |
52
|
23 |
|
public function __construct($returnType = self::TYPE_ARRAY) |
53
|
|
|
{ |
54
|
23 |
|
if (!in_array($returnType, $this->allowedReturnTypes)) { |
55
|
1 |
|
throw new Exception\InvalidArgumentException("Unsupported returnType argument ($returnType)"); |
56
|
|
|
} |
57
|
22 |
|
$this->returnType = $returnType; |
58
|
22 |
|
if ($this->returnType === self::TYPE_ARRAYOBJECT) { |
59
|
7 |
|
$this->storage = new ArrayObject([]); |
60
|
|
|
} else { |
61
|
17 |
|
$this->storage = []; |
62
|
|
|
} |
63
|
22 |
|
$this->position = 0; |
64
|
22 |
|
$this->count = count($this->storage); |
65
|
22 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
* |
70
|
|
|
* @return array|ArrayObject |
71
|
|
|
*/ |
72
|
3 |
|
public function current() |
73
|
|
|
{ |
74
|
3 |
|
return $this->storage[$this->position]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
* |
80
|
|
|
* @return int position |
81
|
|
|
*/ |
82
|
1 |
|
public function key() |
83
|
|
|
{ |
84
|
1 |
|
return $this->position; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
3 |
|
public function next() |
91
|
|
|
{ |
92
|
3 |
|
++$this->position; |
93
|
3 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
3 |
|
public function rewind() |
99
|
|
|
{ |
100
|
3 |
|
$this->position = 0; |
101
|
3 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
3 |
|
public function valid() |
107
|
|
|
{ |
108
|
3 |
|
return isset($this->storage[$this->position]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
6 |
|
public function count() |
115
|
|
|
{ |
116
|
6 |
|
return $this->count; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Append a row to the end of resultset. |
121
|
|
|
* |
122
|
|
|
* @param array $row an associative array |
123
|
|
|
*/ |
124
|
16 |
|
public function append(array $row) |
125
|
|
|
{ |
126
|
16 |
|
if ($this->returnType == self::TYPE_ARRAYOBJECT) { |
127
|
6 |
|
$this->storage[] = new ArrayObject($row); |
128
|
|
|
} else { |
129
|
11 |
|
$this->storage[] = $row; |
130
|
|
|
} |
131
|
16 |
|
++$this->count; |
132
|
16 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
1 |
|
public function offsetExists($position) |
138
|
|
|
{ |
139
|
1 |
|
return isset($this->storage[$position]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
10 |
|
public function offsetGet($position) |
146
|
|
|
{ |
147
|
10 |
|
return isset($this->storage[$position]) ? $this->storage[$position] : null; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
1 |
|
public function offsetSet($position, $row) |
154
|
|
|
{ |
155
|
1 |
|
throw new \Exception('Resultsets are immutable'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
1 |
|
public function offsetUnset($position) |
162
|
|
|
{ |
163
|
1 |
|
throw new \Exception('Resultsets are immutable'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Return underlying stored resultset as array. |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
2 |
|
public function getArray() |
172
|
|
|
{ |
173
|
2 |
|
return (array) $this->storage; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Return underlying stored resultset as ArrayObject. |
178
|
|
|
* |
179
|
|
|
* Depending on the $returnType Resultset::TYPE_ARRAY|Resultset::TYPE_ARRAYOBJECT you can modify |
180
|
|
|
* the internal storage |
181
|
|
|
* |
182
|
|
|
* @return ArrayObject |
183
|
|
|
*/ |
184
|
2 |
|
public function getArrayObject() |
185
|
|
|
{ |
186
|
2 |
|
if ($this->returnType == self::TYPE_ARRAY) { |
187
|
1 |
|
return new ArrayObject($this->storage); |
188
|
|
|
} else { |
189
|
|
|
/** @var ArrayObject $storageAsArrayObject to silent static code analyzers */ |
190
|
1 |
|
$storageAsArrayObject = $this->storage; |
191
|
|
|
|
192
|
1 |
|
return $storageAsArrayObject; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Return the currently set return type. |
198
|
|
|
* |
199
|
|
|
* @see Resultset::TYPE_ARRAY|Resultset::TYPE_ARRAYOBJECT |
200
|
|
|
* |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
1 |
|
public function getReturnType() |
204
|
|
|
{ |
205
|
1 |
|
return $this->returnType; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|