-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpudlCollection.php
More file actions
219 lines (145 loc) · 5.46 KB
/
Copy pathpudlCollection.php
File metadata and controls
219 lines (145 loc) · 5.46 KB
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
<?php
require_once(is_owner(__DIR__.'/pudlObject.php'));
////////////////////////////////////////////////////////////////////////////////
// HANDLE PHP VERSION SPECIFIC IMPLEMENTATIONS
////////////////////////////////////////////////////////////////////////////////
if (version_compare(PHP_VERSION, '8.0.0') >= 0) {
require_once(is_owner(__DIR__.'/pudlCollection.modern.php'));
} else {
require_once(is_owner(__DIR__.'/pudlCollection.legacy.php'));
}
class pudlCollection
extends pudlObject
implements OuterIterator {
use pudlCollection_trait;
////////////////////////////////////////////////////////////////////////////
// CONSTRUCTOR
////////////////////////////////////////////////////////////////////////////
public function __construct(pudl $pudl, $classname, $list=NULL) {
// INIT PUDLOBJECT
parent::__construct();
// SET OUR LOCAL VARIABLES
$this->pudl = $pudl;
$this->classname = $classname;
$this->first = true;
// PROCESS THE LIST OF DATA
if ($list instanceof pudlResult) {
$list = $list->complete();
}
if (!pudl_array($list)) return;
foreach ($list as $item) {
$this[] = ($item instanceof $classname)
? $item
: new $classname($pudl, $item);
}
}
////////////////////////////////////////////////////////////////////////////
// INVOKE, ALIAS FOR NEXT ITEM IN COLLECTION. EASY WAY TO WALK THE LIST
////////////////////////////////////////////////////////////////////////////
public function __invoke() {
if (!$this->first) {
$this->next();
} else {
$this->first = false;
}
return $this->current();
}
////////////////////////////////////////////////////////////////////////////
// RESET INTERNAL POINTER TO FIRST OBJECT IN COLLECTION
// https://www.php.net/manual/en/iterator.rewind.php
////////////////////////////////////////////////////////////////////////////
public function _rewind() {
$this->first = true;
return pudlObject::_rewind();
}
////////////////////////////////////////////////////////////////////////////
// MOVE INTERNAL POINTER TO SPECIFIC ITEM WITHIN COLLECTION
////////////////////////////////////////////////////////////////////////////
public function _seek($row) {
if (!$row) $this->first = true;
pudlObject::_seek($row);
}
////////////////////////////////////////////////////////////////////////////
// NAME OF CLASS WE'RE COLLECTING
////////////////////////////////////////////////////////////////////////////
public function classname() {
return $this->classname;
}
////////////////////////////////////////////////////////////////////////////
// FORWARD METHOD CALL TO ALL OBJECTS WITHIN COLLECTION
////////////////////////////////////////////////////////////////////////////
public function __call($name, $arguments) {
if (empty($name)) return;
// ALLOW FORWARDING CALLS TO PUDLOBJECT METHEDS BY UNDERSCORE PREFIXING
if ($name[0] === '_') {
$newname = substr($name, 1);
if (method_exists($this->classname, $newname)) {
$name = $newname;
}
}
// ALLOW FORWARDING TO STATIC FUNCTION, ONLY CALLED ONCE PER COLLECTION
// INSTEAD OF ONCE PER OBJECT INSTANCE
$method = new ReflectionMethod($this->classname, $name);
if ($method->isStatic()) {
return call_user_func_array(
[$this->classname, $name],
$arguments
);
}
// FORWARD CALL TO ALL OBJECTS WITHIN COLLECTION
$return = [];
$list = $this->raw();
foreach ($list as $key => $item) {
if (!($item instanceof pudlOrm)) continue;
$return[$key] = call_user_func_array(
[$item, $name],
$arguments
);
}
return $return;
}
////////////////////////////////////////////////////////////////////////////
// CLOSURE STYLE PROCESSING
////////////////////////////////////////////////////////////////////////////
public function each(callable $callback) {
$return = [];
$list = $this->raw();
foreach ($list as $key => &$item) {
if (!($item instanceof pudlOrm)) continue;
$return[$key] = call_user_func_array(
$callback,
[&$item, $key, $this]
);
} unset($item);
return $return;
}
////////////////////////////////////////////////////////////////////////////
// PULL ALL INSTANCES OF A PARTICULAR COLUMN
////////////////////////////////////////////////////////////////////////////
public function column($column) {
$return = [];
$list = $this->raw();
foreach ($list as $key => &$item) {
if (!($item instanceof pudlOrm)) continue;
if (empty($item[$column])) continue;
$return[$key] = $item[$column];
} unset($item);
return $return;
}
////////////////////////////////////////////////////////////////////////////
// RETURNS THE INNER ITERATOR FOR THE CURRENT ENTRY.
// http://php.net/manual/en/outeriterator.getinneriterator.php
////////////////////////////////////////////////////////////////////////////
public function _getInnerIterator() {
return $this->current();
}
////////////////////////////////////////////////////////////////////////////
// LOCAL METHOD VARIABLES
////////////////////////////////////////////////////////////////////////////
protected $pudl;
////////////////////////////////////////////////////////////////////////////
// PRIVATE MEMBER VARIABLES
////////////////////////////////////////////////////////////////////////////
private $classname;
private $first;
}