1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NwLaravel\Resultset; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
use Iterator; |
7
|
|
|
use Countable; |
8
|
|
|
use PDOStatement; |
9
|
|
|
use RuntimeException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class AbstractResultset |
13
|
|
|
*/ |
14
|
|
|
abstract class AbstractResultset implements Iterator, Countable |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \PDOStatement |
18
|
|
|
*/ |
19
|
|
|
protected $statement = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array Result options |
23
|
|
|
*/ |
24
|
|
|
protected $options; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Is the current complete? |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
protected $rewindComplete = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Is initialized |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected $initialized = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Track current item in recordset |
40
|
|
|
* @var mixed |
41
|
|
|
*/ |
42
|
|
|
protected $currentData = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Current position of scrollable statement |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
protected $position = -1; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var int |
52
|
|
|
*/ |
53
|
|
|
protected $rowCount = null; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $fields = null; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Initialize |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
4 |
|
public function initialize() |
66
|
|
|
{ |
67
|
4 |
|
$this->initialized = true; |
68
|
4 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get statement |
72
|
|
|
* |
73
|
|
|
* @throws RuntimeException |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
9 |
|
public function getStatement() |
77
|
|
|
{ |
78
|
9 |
|
if (!$this->statement instanceof PDOStatement) { |
79
|
1 |
|
throw new RuntimeException("PDOStatement not defined, not initialize resultset"); |
80
|
|
|
} |
81
|
|
|
|
82
|
8 |
|
return $this->statement; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the data |
87
|
|
|
* |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
7 |
|
public function current() |
91
|
|
|
{ |
92
|
7 |
|
return $this->currentData; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Next |
97
|
|
|
* |
98
|
|
|
* @throws RuntimeException |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
1 |
|
public function next() |
102
|
|
|
{ |
103
|
1 |
|
$this->position++; |
104
|
1 |
|
return $this->fetch(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Fetch |
109
|
|
|
* |
110
|
|
|
* @throws RuntimeException |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
6 |
|
private function fetch() |
114
|
|
|
{ |
115
|
6 |
|
return $this->currentData = $this->getStatement()->fetch(PDO::FETCH_ASSOC); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Key |
121
|
|
|
* |
122
|
|
|
* @return mixed |
123
|
|
|
*/ |
124
|
1 |
|
public function key() |
125
|
|
|
{ |
126
|
1 |
|
return $this->position; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Reset interator |
131
|
|
|
* |
132
|
|
|
* @throws RuntimeException |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
5 |
|
public function rewind() |
136
|
|
|
{ |
137
|
5 |
|
$this->initialize(); |
138
|
|
|
|
139
|
5 |
|
if ($this->rewindComplete) { |
140
|
1 |
|
throw new RuntimeException("Rewind only once."); |
141
|
|
|
} |
142
|
|
|
|
143
|
5 |
|
if (!$this->currentData) { |
144
|
5 |
|
$this->fetch(); |
145
|
5 |
|
} |
146
|
|
|
|
147
|
5 |
|
$this->rewindComplete = true; |
148
|
5 |
|
$this->position = 0; |
149
|
5 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Valid |
153
|
|
|
* |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
1 |
|
public function valid() |
157
|
|
|
{ |
158
|
1 |
|
return ($this->currentData !== false && !is_null($this->currentData)); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Count |
163
|
|
|
* |
164
|
|
|
* @throws RuntimeException |
165
|
|
|
* @return int |
166
|
|
|
*/ |
167
|
1 |
|
public function count() |
168
|
|
|
{ |
169
|
1 |
|
if (is_int($this->rowCount)) { |
170
|
1 |
|
return $this->rowCount; |
171
|
|
|
} |
172
|
|
|
|
173
|
1 |
|
$this->rowCount = (int) $this->getStatement()->rowCount(); |
174
|
|
|
|
175
|
1 |
|
return $this->rowCount; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Fields |
180
|
|
|
* |
181
|
|
|
* @throws RuntimeException |
182
|
|
|
* @return array |
183
|
|
|
*/ |
184
|
2 |
|
public function getFields() |
185
|
|
|
{ |
186
|
2 |
|
if (is_array($this->fields)) { |
187
|
2 |
|
return $this->fields; |
188
|
|
|
} |
189
|
|
|
|
190
|
2 |
|
$fields = array(); |
191
|
|
|
|
192
|
2 |
|
if (!$this->currentData) { |
193
|
1 |
|
$this->fetch(); |
194
|
1 |
|
} |
195
|
|
|
|
196
|
2 |
|
$current = $this->current(); |
197
|
|
|
|
198
|
2 |
|
if (is_array($current)) { |
199
|
1 |
|
$fields = array_keys($current); |
200
|
|
|
|
201
|
2 |
|
} elseif (is_object($current) && method_exists($current, 'toArray')) { |
202
|
1 |
|
$fields = array_keys($current->toArray()); |
203
|
1 |
|
} |
204
|
|
|
|
205
|
2 |
|
$this->fields = $fields; |
206
|
2 |
|
return $fields; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Return numbers de fields |
211
|
|
|
* |
212
|
|
|
* @throws RuntimeException |
213
|
|
|
* @return int |
214
|
|
|
*/ |
215
|
2 |
|
public function getFieldCount() |
216
|
|
|
{ |
217
|
2 |
|
return count($this->getFields()); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|