1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* soluble-flexstore library |
5
|
|
|
* |
6
|
|
|
* @author Vanvelthem Sébastien |
7
|
|
|
* @link https://github.com/belgattitude/soluble-flexstore |
8
|
|
|
* @copyright Copyright (c) 2016-2017 Vanvelthem Sébastien |
9
|
|
|
* @license MIT License https://github.com/belgattitude/soluble-flexstore/blob/master/LICENSE.md |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Soluble\FlexStore\ResultSet; |
14
|
|
|
|
15
|
|
|
use ArrayObject; |
16
|
|
|
//use Soluble\DbWrapper\Result\ResultInterface; |
17
|
|
|
use Zend\Db\ResultSet\ResultSet as ZFResultSet; |
18
|
|
|
|
19
|
|
|
abstract class AbstractResultSet implements ResultSetInterface |
20
|
|
|
{ |
21
|
|
|
const TYPE_ARRAYOBJECT = 'arrayobject'; |
22
|
|
|
const TYPE_ARRAY = 'array'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Return type to use when returning an object from the set. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $returnType = self::TYPE_ARRAYOBJECT; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Allowed return types. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $allowedReturnTypes = [ |
37
|
|
|
self::TYPE_ARRAYOBJECT, |
38
|
|
|
self::TYPE_ARRAY, |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var ArrayObject |
43
|
|
|
*/ |
44
|
|
|
protected $arrayObjectPrototype = null; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var ZFResultSet |
48
|
|
|
*/ |
49
|
|
|
protected $zfResultSet; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param ZFResultSet $resultSet |
53
|
|
|
* @param string $returnType |
54
|
|
|
*/ |
55
|
40 |
|
public function __construct($resultSet, $returnType = self::TYPE_ARRAYOBJECT) |
56
|
|
|
{ |
57
|
40 |
|
$this->zfResultSet = $resultSet; |
58
|
40 |
|
$this->returnType = (in_array($returnType, [self::TYPE_ARRAY, self::TYPE_ARRAYOBJECT], true)) ? $returnType : self::TYPE_ARRAYOBJECT; |
59
|
40 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set the row object prototype. |
63
|
|
|
* |
64
|
|
|
* @param ArrayObject $arrayObjectPrototype |
65
|
|
|
* |
66
|
|
|
* @throws Exception\InvalidArgumentException |
67
|
|
|
* |
68
|
|
|
* @return AbstractResultSet |
69
|
|
|
*/ |
70
|
|
|
public function setArrayObjectPrototype($arrayObjectPrototype) |
71
|
|
|
{ |
72
|
|
|
$this->zfResultSet->setArrayObjectPrototype($arrayObjectPrototype); |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the row object prototype. |
79
|
|
|
* |
80
|
|
|
* @return ArrayObject |
81
|
|
|
*/ |
82
|
|
|
public function getArrayObjectPrototype() |
83
|
|
|
{ |
84
|
|
|
return $this->zfResultSet->getArrayObjectPrototype(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the return type to use when returning objects from the set. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getReturnType() |
93
|
|
|
{ |
94
|
|
|
return $this->zfResultSet->getReturnType(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return AbstractResultSet |
99
|
|
|
*/ |
100
|
|
|
public function buffer() |
101
|
|
|
{ |
102
|
|
|
$this->zfResultSet->buffer(); |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public function isBuffered() |
111
|
|
|
{ |
112
|
|
|
return $this->zfResultSet->isBuffered(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the data source used to create the result set. |
117
|
|
|
* |
118
|
|
|
* @return null|\Iterator |
119
|
|
|
*/ |
120
|
|
|
public function getDataSource() |
121
|
|
|
{ |
122
|
|
|
return $this->zfResultSet->getDataSource(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Retrieve count of fields in individual rows of the result set. |
127
|
|
|
* |
128
|
|
|
* @return int |
129
|
|
|
*/ |
130
|
1 |
|
public function getFieldCount() |
131
|
|
|
{ |
132
|
1 |
|
return $this->zfResultSet->getFieldCount(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Iterator: move pointer to next item. |
137
|
|
|
*/ |
138
|
24 |
|
public function next() |
139
|
|
|
{ |
140
|
24 |
|
$this->zfResultSet->next(); |
141
|
24 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Iterator: retrieve current key. |
145
|
|
|
* |
146
|
|
|
* @return mixed |
147
|
|
|
*/ |
148
|
1 |
|
public function key() |
149
|
|
|
{ |
150
|
1 |
|
return $this->zfResultSet->key(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Iterator: get current item. |
155
|
|
|
* |
156
|
|
|
* @return array|\ArrayObject|null |
157
|
|
|
*/ |
158
|
|
|
public function current() |
159
|
|
|
{ |
160
|
|
|
return $this->zfResultSet->current(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Iterator: is pointer valid? |
165
|
|
|
* |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
|
|
public function valid() |
169
|
|
|
{ |
170
|
|
|
return $this->zfResultSet->valid(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Iterator: rewind. |
175
|
|
|
*/ |
176
|
|
|
public function rewind() |
177
|
|
|
{ |
178
|
|
|
$this->zfResultSet->rewind(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Countable: return count of rows. |
183
|
|
|
* |
184
|
|
|
* @return int |
185
|
|
|
*/ |
186
|
38 |
|
public function count() |
187
|
|
|
{ |
188
|
38 |
|
return $this->zfResultSet->count(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Cast result set to array of arrays. |
193
|
|
|
* |
194
|
|
|
* @return array |
195
|
|
|
* |
196
|
|
|
* @throws Exception\RuntimeException if any row is not castable to an array |
197
|
|
|
*/ |
198
|
|
|
public function toArray() |
199
|
|
|
{ |
200
|
|
|
return $this->zfResultSet->toArray(); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|