-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpudlResult.php
385 lines (243 loc) · 10.5 KB
/
pudlResult.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
require_once(is_owner(__DIR__.'/pudlData.php'));
abstract class pudlResult
implements pudlData {
use pudlData_shim;
////////////////////////////////////////////////////////////////////////////
// CONSTRUCTOR. PASS IN A $PUDL OBJECT, AND THE $RESULT IF AVAIL
////////////////////////////////////////////////////////////////////////////
public function __construct(pudl $pudl, $result=false) {
$this->result = $result;
$this->pudl = $pudl;
$this->query = $pudl->query();
$this->string = $pudl->isString();
}
////////////////////////////////////////////////////////////////////////////
// DESTRUCTOR - REQUIRED FOR INHERITANCE
////////////////////////////////////////////////////////////////////////////
public function __destruct() {
}
////////////////////////////////////////////////////////////////////////////
// SHORTCUT METHOD FOR ACCESSING CURRENT ROW DATA
////////////////////////////////////////////////////////////////////////////
public function __invoke() {
return $this->row();
}
////////////////////////////////////////////////////////////////////////////
// PHP'S COUNTABLE - GET THE NUMBER OF ROWS FROM THIS RESULT
// http://php.net/manual/en/countable.count.php
////////////////////////////////////////////////////////////////////////////
abstract public function _count();
////////////////////////////////////////////////////////////////////////////
// PHP'S SEEKABLEITERATOR - JUMP TO A ROW IN THIS RESULT
// http://php.net/manual/en/seekableiterator.seek.php
////////////////////////////////////////////////////////////////////////////
abstract public function _seek($row);
////////////////////////////////////////////////////////////////////////////
// PHP'S ITERATOR - GET THE CURRENT ROW IN THIS RESULT
// http://php.net/manual/en/iterator.current.php
////////////////////////////////////////////////////////////////////////////
public function _current() {
if ($this->row === false) $this->row();
return $this->data;
}
////////////////////////////////////////////////////////////////////////////
// PHP'S ITERATOR - GET THE KEY FOR THE CURRENT ROW IN THIS RESULT
// http://php.net/manual/en/iterator.key.php
////////////////////////////////////////////////////////////////////////////
public function _key() {
return ($this->row === false) ? 0 : $this->row;
}
////////////////////////////////////////////////////////////////////////////
// PHP'S ITERATOR - MOVE TO THE NEXT ROW IN THIS RESULT AND RETURN THAT ROW
// http://php.net/manual/en/iterator.next.php
////////////////////////////////////////////////////////////////////////////
public function _next() {
$this->row();
}
////////////////////////////////////////////////////////////////////////////
// PHP'S ITERATOR - MOVE TO THE FIRST ROW IN THIS RESULT
// http://php.net/manual/en/iterator.rewind.php
////////////////////////////////////////////////////////////////////////////
public function _rewind() {
$this->seek(0);
}
////////////////////////////////////////////////////////////////////////////
// PHP'S ITERATOR - TRUE IF THE CURRENT ROW IN THIS RESULT IS VALID
// http://php.net/manual/en/iterator.valid.php
////////////////////////////////////////////////////////////////////////////
public function _valid() {
if ($this->row === false) $this->row();
return pudl_array($this->data);
}
////////////////////////////////////////////////////////////////////////////
// GET THE NUMBER OF FIELD COLUMNS IN THIS RESULT
////////////////////////////////////////////////////////////////////////////
abstract public function fields();
////////////////////////////////////////////////////////////////////////////
// GET DETAILS ON A PARTICULAR FIELD COLUMN IN THIS RESULT
////////////////////////////////////////////////////////////////////////////
abstract public function getField($column);
////////////////////////////////////////////////////////////////////////////
// GET DETAILS ON ALL FIELD COLUMNS IN THIS RESULT
////////////////////////////////////////////////////////////////////////////
public function listFields() {
if (!$this->result) return false;
if ($this->fields === false) {
$this->fields = [];
$total = $this->fields();
for ($i=0; $i<$total; $i++) {
$this->fields[] = $this->getField($i);
}
}
return $this->fields;
}
////////////////////////////////////////////////////////////////////////////
// TRUE IF THIS IS A STRING RESULT
////////////////////////////////////////////////////////////////////////////
public function isString() {
return $this->string;
}
////////////////////////////////////////////////////////////////////////////
// TRUE IF THIS RESULT CONTAINS DATA
////////////////////////////////////////////////////////////////////////////
public function hasRows() {
return ($this->count() > 0);
}
////////////////////////////////////////////////////////////////////////////
// FREE RESOURCES ASSOCIATED WITH THIS RESULT
////////////////////////////////////////////////////////////////////////////
abstract public function free();
////////////////////////////////////////////////////////////////////////////
// GET A SINGLE CELL FROM THIS RESULT
////////////////////////////////////////////////////////////////////////////
abstract public function cell($row=0, $column=0);
////////////////////////////////////////////////////////////////////////////
// GET A SINGLE CELL FROM THIS RESULT, AND FREE THIS RESULT
////////////////////////////////////////////////////////////////////////////
public function completeCell($row=0, $column=0) {
$cell = $this->cell($row, $column);
$this->free();
return $cell;
}
////////////////////////////////////////////////////////////////////////////
// MOVE TO THE NEXT ROW IN THIS RESULT AND RETURN THAT ROW'S DATA
////////////////////////////////////////////////////////////////////////////
abstract public function row();
////////////////////////////////////////////////////////////////////////////
// GET ALL ROWS FROM THIS RESULT
////////////////////////////////////////////////////////////////////////////
public function rows() {
if (!$this->result) return false;
$rows = [];
while ($data = $this->row()) {
$rows[] = $data;
}
return $rows;
}
////////////////////////////////////////////////////////////////////////////
// GET ALL ROWS FROM THIS RESULT, AND FREE THIS RESULT
////////////////////////////////////////////////////////////////////////////
public function complete() {
$rows = $this->rows();
$this->free();
return $rows;
}
////////////////////////////////////////////////////////////////////////////
// COMBINE TWO RESULT COLUMNS INTO KEY-VALUE PAIR
////////////////////////////////////////////////////////////////////////////
public function collection() {
$return = [];
while ($data = $this->row()) {
$return[reset($data)] = end($data);
}
$this->free();
return $return;
}
////////////////////////////////////////////////////////////////////////////
// CONVERT RESULT DATA INTO A TREE
////////////////////////////////////////////////////////////////////////////
public function tree($separator='.') {
$return = [];
while ($data = $this->row()) {
$keys = explode($separator, reset($data));
$node = &$return;
foreach ($keys as $count => $key) {
if ($count === count($keys)-1) break;
if (!array_key_exists($key, $node)) $node[$key] = [];
if (!pudl_array($node[$key])) $node[$key] = [$node[$key]];
$node = &$node[$key];
}
if (!array_key_exists($key, $node)) {
$node[$key] = end($data);
} else {
$node[$key][] = end($data);
}
}
$this->free();
return $return;
}
////////////////////////////////////////////////////////////////////////////
// RETURNS JSON SERIALIZABLE DATA
// http://php.net/manual/en/jsonserializable.jsonserialize.php
////////////////////////////////////////////////////////////////////////////
public function _jsonSerialize() {
return $this->rows();
}
////////////////////////////////////////////////////////////////////////////
// RETURNS THE JSON REPRESENTATION OF THIS RESULT
// http://php.net/manual/en/function.json-encode.php
////////////////////////////////////////////////////////////////////////////
public function json() {
return pudl::jsonEncode($this);
}
////////////////////////////////////////////////////////////////////////////
// RETURNS THE JSON REPRESENTATION OF THIS RESULT, AND FREE THIS RESULT
// http://php.net/manual/en/function.json-encode.php
////////////////////////////////////////////////////////////////////////////
public function completeJson() {
$json = $this->json();
$this->free();
return $json;
}
////////////////////////////////////////////////////////////////////////////
// GET SQL QUERY THAT GENERATED THIS RESULT
////////////////////////////////////////////////////////////////////////////
public function query() {
return $this->query;
}
////////////////////////////////////////////////////////////////////////////
// GET THE RAW PHP RESOURCE FOR THIS REUSLT
////////////////////////////////////////////////////////////////////////////
public function result() {
return $this->result;
}
////////////////////////////////////////////////////////////////////////////
// GET THE PUDL INSTANCE ASSOCIATED WITH THIS RESULT
////////////////////////////////////////////////////////////////////////////
public function pudl() {
return $this->pudl;
}
////////////////////////////////////////////////////////////////////////////
// GET THE ERROR CODE FOR THIS RESULT - 0, FALSE, NULL ALL MEAN NO ERROR
////////////////////////////////////////////////////////////////////////////
public function errno() {
return ($this->result === false) || ($this->result === NULL);
}
////////////////////////////////////////////////////////////////////////////
// GET THE ERROR MESSAGE FOR THIS RESULT
////////////////////////////////////////////////////////////////////////////
public function error() {
return $this->errno() ? 'Unknown Error' : '';
}
////////////////////////////////////////////////////////////////////////////
// MEMBER VARIABLES
////////////////////////////////////////////////////////////////////////////
/** @var pudl */ protected $pudl;
/** @var mixed */ protected $result;
/** @var string */ protected $query;
/** @var bool */ protected $string;
/** @var array|false */ protected $fields = false;
/** @var int|false */ protected $row = false;
/** @var array|false */ protected $data = false;
}