1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\utils\models; |
4
|
|
|
|
5
|
|
|
use Ubiquity\orm\OrmUtils; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Ubiquity\utils\models$UArrayModels |
9
|
|
|
* This class is part of Ubiquity |
10
|
|
|
* @author jc |
11
|
|
|
* @version 1.0.2 |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
class UArrayModels { |
15
|
|
|
/** |
16
|
|
|
* Returns a sorted array using a user defined comparison function. |
17
|
|
|
* @param array $array |
18
|
|
|
* @param callable $callback |
19
|
|
|
* @return array |
20
|
|
|
*/ |
21
|
2 |
|
public static function sort(array $array , callable $callback):array{ |
22
|
2 |
|
\usort($array,$callback); |
23
|
2 |
|
return $array; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Groups an array using an array of user defined comparison functions. |
28
|
|
|
* @param array $objects |
29
|
|
|
* @param array $gbCallbacks |
30
|
|
|
* @param callable|null $lineCallback |
31
|
|
|
* @param bool|null $sort |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
1 |
|
public static function groupsBy(array $objects, array $gbCallbacks, ?callable $lineCallback=null, ?bool $sort=null):array { |
35
|
1 |
|
if(\count($gbCallbacks)==0) { |
36
|
1 |
|
return $objects; |
37
|
|
|
} |
38
|
1 |
|
$objects=self::groupBy($objects, current($gbCallbacks), $lineCallback, $sort); |
39
|
1 |
|
\array_shift($gbCallbacks); |
40
|
1 |
|
$result=[]; |
41
|
1 |
|
foreach ($objects as $k=>$v){ |
42
|
1 |
|
$result[$k]=self::groupsBy($v,$gbCallbacks); |
43
|
|
|
} |
44
|
1 |
|
return $result; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Groups an array using a user defined comparison function. |
49
|
|
|
* @param array $objects |
50
|
|
|
* @param callable $gbCallback |
51
|
|
|
* @param callable|null $lineCallback |
52
|
|
|
* @param bool|null $sort |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
2 |
|
public static function groupBy(array $objects, callable $gbCallback, ?callable $lineCallback=null, ?bool $sort=null):array { |
56
|
2 |
|
if($sort){ |
57
|
2 |
|
$objects=self::sort($objects, function ($item1, $item2) use ($gbCallback){ |
58
|
2 |
|
return $gbCallback($item1)<=>$gbCallback($item2); |
59
|
2 |
|
}); |
60
|
|
|
}elseif($sort===false){ |
61
|
|
|
$objects=self::sort($objects, function ($item1, $item2) use ($gbCallback){ |
62
|
|
|
return $gbCallback($item2)<=>$gbCallback($item1); |
63
|
|
|
}); |
64
|
|
|
} |
65
|
2 |
|
$result=[]; |
66
|
2 |
|
$groupBy=null; |
67
|
2 |
|
foreach ($objects as $line){ |
68
|
2 |
|
if($groupBy!==($nGB=$gbCallback($line))){ |
69
|
2 |
|
$groupBy=$nGB; |
70
|
|
|
} |
71
|
2 |
|
$result[$groupBy][]=(isset($lineCallback))?$lineCallback($line):$line; |
72
|
|
|
} |
73
|
2 |
|
return $result; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns an associative array of key/values from an array of objects. |
78
|
|
|
* @param array $objects |
79
|
|
|
* @param ?string|callable $keyFunction |
80
|
|
|
* @param ?string|callable $valueFunction |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
1 |
|
public static function asKeyValues(array $objects, $keyFunction = NULL, $valueFunction = NULL) { |
84
|
1 |
|
$result = []; |
85
|
1 |
|
if (isset($valueFunction) === false) { |
86
|
1 |
|
$valueFunction = '__toString'; |
87
|
|
|
} |
88
|
1 |
|
if (isset($keyFunction) === false) { |
89
|
|
|
foreach ($objects as $object) { |
90
|
|
|
$result[] = self::callFunction($object, $valueFunction); |
91
|
|
|
} |
92
|
|
|
} else { |
93
|
1 |
|
foreach ($objects as $object) { |
94
|
1 |
|
$result[self::callFunction($object, $keyFunction)] = self::callFunction($object, $valueFunction); |
95
|
|
|
} |
96
|
|
|
} |
97
|
1 |
|
return $result; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Finds and returns the first occurrence of the array satisfying the callback. |
102
|
|
|
* @param array|null $objects |
103
|
|
|
* @param callable $callback |
104
|
|
|
* @return mixed|null |
105
|
|
|
*/ |
106
|
2 |
|
public static function find(?array $objects,callable $callback){ |
107
|
2 |
|
if(\is_array($objects)) { |
108
|
2 |
|
foreach ($objects as $o){ |
109
|
2 |
|
if($callback($o)){ |
110
|
2 |
|
return $o; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Finds and returns the first occurrence of the array using one of the objects properties. |
119
|
|
|
* @param array $objects |
120
|
|
|
* @param mixed $value |
121
|
|
|
* @param string $property default the id property |
122
|
|
|
* @return mixed|null |
123
|
|
|
*/ |
124
|
4 |
|
public static function findBy(?array $objects,$value,string $property='id'){ |
125
|
4 |
|
if(!\is_array($objects)){ |
126
|
|
|
return null; |
127
|
|
|
} |
128
|
4 |
|
$get='get'.\ucfirst($property); |
129
|
4 |
|
foreach ($objects as $index=>$o) { |
130
|
4 |
|
if($value==$o->$get()){ |
131
|
4 |
|
return $o; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
return null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Finds and returns the first occurrence of the array using the objects id. |
139
|
|
|
* |
140
|
|
|
* @param array $objects |
141
|
|
|
* @param mixed $idValue |
142
|
|
|
* @return NULL|mixed|NULL |
143
|
|
|
*/ |
144
|
1 |
|
public static function findById(?array $objects,$idValue){ |
145
|
1 |
|
if(!is_array($objects) && \count($objects)>0){ |
146
|
|
|
return null; |
147
|
|
|
} |
148
|
1 |
|
$property=OrmUtils::getFirstKey(\get_class(\current($objects))); |
149
|
1 |
|
return self::findBy($objects, $idValue,$property); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Checks if an object exist in an array of objects satisfying the callback. |
154
|
|
|
* |
155
|
|
|
* @param array $objects |
156
|
|
|
* @param callable $callback |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
1 |
|
public static function contains(?array $objects,callable $callback):bool{ |
160
|
1 |
|
return self::find($objects, $callback)!==null; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Checks if an object exist in an array of objects using one of the object property. |
165
|
|
|
* |
166
|
|
|
* @param array $objects |
167
|
|
|
* @param object $object |
168
|
|
|
* @param string $property |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
2 |
|
public static function containsBy(?array $objects,object $object,string $property='id'):bool{ |
172
|
2 |
|
if($object===null){ |
173
|
|
|
return false; |
174
|
|
|
} |
175
|
2 |
|
$get='get'.\ucfirst($property); |
176
|
2 |
|
$objectValue=$object->$get(); |
177
|
2 |
|
return self::findBy($objects, $objectValue,$property)!==null; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Checks if an object exist in an array of objects using the object id. |
182
|
|
|
* |
183
|
|
|
* @param array $objects |
184
|
|
|
* @param object $object |
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
1 |
|
public static function containsById(?array $objects,object $object):bool { |
188
|
1 |
|
if($object===null){ |
189
|
|
|
return false; |
190
|
|
|
} |
191
|
1 |
|
$property=OrmUtils::getFirstKey(\get_class($object)); |
192
|
1 |
|
return self::containsBy($objects, $object,$property); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Removes the first occurrence of the array satisfying the callback. |
198
|
|
|
* @param array|null $objects |
199
|
|
|
* @param callable $callback |
200
|
|
|
* @return array |
201
|
|
|
*/ |
202
|
1 |
|
public static function remove(?array $objects,callable $callback):array{ |
203
|
1 |
|
foreach ($objects as $index=>$o) { |
204
|
1 |
|
if($callback($o)){ |
205
|
1 |
|
unset($objects[$index]); |
206
|
1 |
|
break; |
207
|
|
|
} |
208
|
|
|
} |
209
|
1 |
|
return $objects; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Removes an object from an array of objects using one of its properties. |
214
|
|
|
* @param array $objects |
215
|
|
|
* @param object $object |
216
|
|
|
* @param string $property default the id property |
217
|
|
|
* @return ?array |
218
|
|
|
*/ |
219
|
1 |
|
public static function removeBy(?array $objects,object $object,string $property='id'):?array{ |
220
|
1 |
|
if(!is_array($objects) || $object==null){ |
221
|
|
|
return null; |
222
|
|
|
} |
223
|
1 |
|
$get='get'.\ucfirst($property); |
224
|
1 |
|
$objectValue=$object->$get(); |
225
|
1 |
|
foreach ($objects as $index=>$o) { |
226
|
1 |
|
if($objectValue===$o->$get()){ |
227
|
1 |
|
unset($objects[$index]); |
228
|
1 |
|
return $objects; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
return $objects; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Removes objects from an array of objects using one of their properties. |
236
|
|
|
* @param array $objects |
237
|
|
|
* @param object $object |
238
|
|
|
* @param string $property default the id property |
239
|
|
|
* @return ?array |
240
|
|
|
*/ |
241
|
1 |
|
public static function removeAllBy(?array $objects,object $object,string $property='id'):?array{ |
242
|
1 |
|
if(!is_array($objects) || $object==null){ |
243
|
|
|
return null; |
244
|
|
|
} |
245
|
1 |
|
$get='get'.\ucfirst($property); |
246
|
1 |
|
$objectValue=$object->$get(); |
247
|
1 |
|
$toRemove=[]; |
248
|
1 |
|
foreach ($objects as $index=>$o) { |
249
|
1 |
|
if($objectValue===$o->$get()){ |
250
|
1 |
|
$toRemove[]=$index; |
251
|
|
|
} |
252
|
|
|
} |
253
|
1 |
|
foreach ($toRemove as $index){ |
254
|
1 |
|
unset($objects[$index]); |
255
|
|
|
} |
256
|
1 |
|
return $objects; |
257
|
|
|
} |
258
|
|
|
|
259
|
1 |
|
public static function compute(?array $objects,callable $callable,callable $computeCall){ |
260
|
1 |
|
$res=null; |
261
|
1 |
|
if($objects!=null) { |
262
|
1 |
|
foreach ($objects as $object) { |
263
|
1 |
|
$computeCall($res, $callable($object)); |
264
|
|
|
} |
265
|
|
|
} |
266
|
1 |
|
return $res; |
267
|
|
|
} |
268
|
|
|
|
269
|
1 |
|
public static function computeSumProperty(?array $objects,string $propertyName){ |
270
|
1 |
|
$getter='get'.\ucfirst($propertyName); |
271
|
1 |
|
return self::compute($objects,fn($o)=>$o->$getter(),fn(&$r,$o)=>$r+=$o); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
public static function computeSum(?array $objects,callable $callable){ |
275
|
|
|
return self::compute($objects,$callable,fn(&$r,$o)=>$r+=$o); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Removes all the occurrences of the array satisfying the callback. |
280
|
|
|
* @param array|null $objects |
281
|
|
|
* @param callable $callback |
282
|
|
|
* @return array |
283
|
|
|
*/ |
284
|
1 |
|
public static function removeAll(?array $objects,callable $callback):array{ |
285
|
1 |
|
$toRemove=[]; |
286
|
1 |
|
foreach ($objects as $index=>$o) { |
287
|
1 |
|
if($callback($o)){ |
288
|
1 |
|
$toRemove[]=$index; |
289
|
|
|
} |
290
|
|
|
} |
291
|
1 |
|
foreach ($toRemove as $index){ |
292
|
1 |
|
unset($objects[$index]); |
293
|
|
|
} |
294
|
1 |
|
return $objects; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @param array $objects |
299
|
|
|
* @return array |
300
|
|
|
*/ |
301
|
1 |
|
public static function asArray(array $objects):array{ |
302
|
1 |
|
$result=[]; |
303
|
1 |
|
foreach ($objects as $index=>$o) { |
304
|
1 |
|
$result[$index]=$o->_rest??[]; |
305
|
|
|
} |
306
|
1 |
|
return $result; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @param array $objects |
311
|
|
|
* @param int $options |
312
|
|
|
* @return string |
313
|
|
|
*/ |
314
|
1 |
|
public static function asJson(array $objects,int $options=0):string{ |
315
|
1 |
|
$result=[]; |
316
|
1 |
|
foreach ($objects as $index=>$o) { |
317
|
1 |
|
$result[$index]=$o->_rest??[]; |
318
|
|
|
} |
319
|
1 |
|
return \json_encode($result,$options); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param array $objects |
324
|
|
|
* @param array $properties |
325
|
|
|
* @return array |
326
|
|
|
*/ |
327
|
2 |
|
public static function asArrayProperties(array $objects,array $properties):array{ |
328
|
2 |
|
$res=[]; |
329
|
2 |
|
$accessors=self::getAccessors($properties); |
330
|
2 |
|
foreach ($objects as $object){ |
331
|
2 |
|
$or=[]; |
332
|
2 |
|
foreach ($accessors as $prop=>$get){ |
333
|
2 |
|
$or[$prop]=$object->$get(); |
334
|
|
|
} |
335
|
2 |
|
$res[]=$or; |
336
|
|
|
} |
337
|
2 |
|
return $res; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @param array $objects |
342
|
|
|
* @param array $properties |
343
|
|
|
* @param int $options |
344
|
|
|
* @return string |
345
|
|
|
*/ |
346
|
1 |
|
public static function asJsonProperties(array $objects,array $properties,int $options=0):string{ |
347
|
1 |
|
return \json_encode(self::asArrayProperties($objects, $properties),$options); |
348
|
|
|
} |
349
|
|
|
|
350
|
2 |
|
private static function getAccessors($properties,$prefix='get'){ |
351
|
2 |
|
$res=[]; |
352
|
2 |
|
foreach ($properties as $property){ |
353
|
2 |
|
$res[$property]=$prefix.\ucfirst($property); |
354
|
|
|
|
355
|
|
|
} |
356
|
2 |
|
return $res; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @param $object |
361
|
|
|
* @param $callback |
362
|
|
|
* @return false|mixed |
363
|
|
|
*/ |
364
|
1 |
|
private static function callFunction($object, $callback) { |
365
|
1 |
|
if (\is_string($callback)){ |
366
|
1 |
|
return \call_user_func(array( |
367
|
1 |
|
$object, |
368
|
1 |
|
$callback |
369
|
1 |
|
), []); |
370
|
|
|
} |
371
|
|
|
if (\is_callable($callback)) { |
372
|
|
|
return $callback($object); |
373
|
|
|
} |
374
|
|
|
return $object; |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
|