Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
UArrayModels.php
Go to the documentation of this file.
1<?php
2
4
6
21 public static function sort(array $array , callable $callback):array{
22 \usort($array,$callback);
23 return $array;
24 }
25
34 public static function groupsBy(array $objects, array $gbCallbacks, ?callable $lineCallback=null, ?bool $sort=null):array {
35 if(\count($gbCallbacks)==0) {
36 return $objects;
37 }
38 $objects=self::groupBy($objects, current($gbCallbacks), $lineCallback, $sort);
39 \array_shift($gbCallbacks);
40 $result=[];
41 foreach ($objects as $k=>$v){
42 $result[$k]=self::groupsBy($v,$gbCallbacks);
43 }
44 return $result;
45 }
46
55 public static function groupBy(array $objects, callable $gbCallback, ?callable $lineCallback=null, ?bool $sort=null):array {
56 if($sort){
57 $objects=self::sort($objects, function ($item1, $item2) use ($gbCallback){
58 return $gbCallback($item1)<=>$gbCallback($item2);
59 });
60 }elseif($sort===false){
61 $objects=self::sort($objects, function ($item1, $item2) use ($gbCallback){
62 return $gbCallback($item2)<=>$gbCallback($item1);
63 });
64 }
65 $result=[];
66 $groupBy=null;
67 foreach ($objects as $line){
68 if($groupBy!==($nGB=$gbCallback($line))){
69 $groupBy=$nGB;
70 }
71 $result[$groupBy][]=(isset($lineCallback))?$lineCallback($line):$line;
72 }
73 return $result;
74 }
75
83 public static function asKeyValues(array $objects, $keyFunction = NULL, $valueFunction = NULL) {
84 $result = [];
85 if (isset($valueFunction) === false) {
86 $valueFunction = '__toString';
87 }
88 if (isset($keyFunction) === false) {
89 foreach ($objects as $object) {
90 $result[] = self::callFunction($object, $valueFunction);
91 }
92 } else {
93 foreach ($objects as $object) {
94 $result[self::callFunction($object, $keyFunction)] = self::callFunction($object, $valueFunction);
95 }
96 }
97 return $result;
98 }
99
106 public static function find(?array $objects,callable $callback){
107 if(\is_array($objects)) {
108 foreach ($objects as $o){
109 if($callback($o)){
110 return $o;
111 }
112 }
113 }
114 return null;
115 }
116
124 public static function findBy(?array $objects,$value,string $property='id'){
125 if(!\is_array($objects)){
126 return null;
127 }
128 $get='get'.\ucfirst($property);
129 foreach ($objects as $index=>$o) {
130 if($value==$o->$get()){
131 return $o;
132 }
133 }
134 return null;
135 }
136
144 public static function findById(?array $objects,$idValue){
145 if(!is_array($objects) && \count($objects)>0){
146 return null;
147 }
148 $property=OrmUtils::getFirstKey(\get_class(\current($objects)));
149 return self::findBy($objects, $idValue,$property);
150 }
151
159 public static function contains(?array $objects,callable $callback):bool{
160 return self::find($objects, $callback)!==null;
161 }
162
171 public static function containsBy(?array $objects,object $object,string $property='id'):bool{
172 if($object===null){
173 return false;
174 }
175 $get='get'.\ucfirst($property);
176 $objectValue=$object->$get();
177 return self::findBy($objects, $objectValue,$property)!==null;
178 }
179
187 public static function containsById(?array $objects,object $object):bool {
188 if($object===null){
189 return false;
190 }
191 $property=OrmUtils::getFirstKey(\get_class($object));
192 return self::containsBy($objects, $object,$property);
193 }
194
195
202 public static function remove(?array $objects,callable $callback):array{
203 foreach ($objects as $index=>$o) {
204 if($callback($o)){
205 unset($objects[$index]);
206 break;
207 }
208 }
209 return $objects;
210 }
211
219 public static function removeBy(?array $objects,object $object,string $property='id'):?array{
220 if(!is_array($objects) || $object==null){
221 return null;
222 }
223 $get='get'.\ucfirst($property);
224 $objectValue=$object->$get();
225 foreach ($objects as $index=>$o) {
226 if($objectValue===$o->$get()){
227 unset($objects[$index]);
228 return $objects;
229 }
230 }
231 return $objects;
232 }
233
241 public static function removeAllBy(?array $objects,object $object,string $property='id'):?array{
242 if(!is_array($objects) || $object==null){
243 return null;
244 }
245 $get='get'.\ucfirst($property);
246 $objectValue=$object->$get();
247 $toRemove=[];
248 foreach ($objects as $index=>$o) {
249 if($objectValue===$o->$get()){
250 $toRemove[]=$index;
251 }
252 }
253 foreach ($toRemove as $index){
254 unset($objects[$index]);
255 }
256 return $objects;
257 }
258
259 public static function compute(?array $objects,callable $callable,callable $computeCall){
260 $res=null;
261 if($objects!=null) {
262 foreach ($objects as $object) {
263 $computeCall($res, $callable($object));
264 }
265 }
266 return $res;
267 }
268
269 public static function computeSumProperty(?array $objects,string $propertyName){
270 $getter='get'.\ucfirst($propertyName);
271 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
284 public static function removeAll(?array $objects,callable $callback):array{
285 $toRemove=[];
286 foreach ($objects as $index=>$o) {
287 if($callback($o)){
288 $toRemove[]=$index;
289 }
290 }
291 foreach ($toRemove as $index){
292 unset($objects[$index]);
293 }
294 return $objects;
295 }
296
301 public static function asArray(array $objects):array{
302 $result=[];
303 foreach ($objects as $index=>$o) {
304 $result[$index]=$o->_rest??[];
305 }
306 return $result;
307 }
308
314 public static function asJson(array $objects,int $options=0):string{
315 $result=[];
316 foreach ($objects as $index=>$o) {
317 $result[$index]=$o->_rest??[];
318 }
319 return \json_encode($result,$options);
320 }
321
327 public static function asArrayProperties(array $objects,array $properties):array{
328 $res=[];
329 $accessors=self::getAccessors($properties);
330 foreach ($objects as $object){
331 $or=[];
332 foreach ($accessors as $prop=>$get){
333 $or[$prop]=$object->$get();
334 }
335 $res[]=$or;
336 }
337 return $res;
338 }
339
346 public static function asJsonProperties(array $objects,array $properties,int $options=0):string{
347 return \json_encode(self::asArrayProperties($objects, $properties),$options);
348 }
349
350 private static function getAccessors($properties,$prefix='get'){
351 $res=[];
352 foreach ($properties as $property){
353 $res[$property]=$prefix.\ucfirst($property);
354
355 }
356 return $res;
357 }
358
364 private static function callFunction($object, $callback) {
365 if (\is_string($callback)){
366 return \call_user_func(array(
367 $object,
368 $callback
369 ), []);
370 }
371 if (\is_callable($callback)) {
372 return $callback($object);
373 }
374 return $object;
375 }
376}
377
Object/relational mapping utilities.
Definition OrmUtils.php:17
Ubiquity\utils\models$UArrayModels This class is part of Ubiquity.
static groupBy(array $objects, callable $gbCallback, ?callable $lineCallback=null, ?bool $sort=null)
Groups an array using a user defined comparison function.
static computeSumProperty(?array $objects, string $propertyName)
static sort(array $array, callable $callback)
Returns a sorted array using a user defined comparison function.
static getAccessors($properties, $prefix='get')
static removeAll(?array $objects, callable $callback)
Removes all the occurrences of the array satisfying the callback.
static removeBy(?array $objects, object $object, string $property='id')
Removes an object from an array of objects using one of its properties.
static asKeyValues(array $objects, $keyFunction=NULL, $valueFunction=NULL)
Returns an associative array of key/values from an array of objects.
static find(?array $objects, callable $callback)
Finds and returns the first occurrence of the array satisfying the callback.
static callFunction($object, $callback)
static containsBy(?array $objects, object $object, string $property='id')
Checks if an object exist in an array of objects using one of the object property.
static groupsBy(array $objects, array $gbCallbacks, ?callable $lineCallback=null, ?bool $sort=null)
Groups an array using an array of user defined comparison functions.
static removeAllBy(?array $objects, object $object, string $property='id')
Removes objects from an array of objects using one of their properties.
static contains(?array $objects, callable $callback)
Checks if an object exist in an array of objects satisfying the callback.
static asArrayProperties(array $objects, array $properties)
static findById(?array $objects, $idValue)
Finds and returns the first occurrence of the array using the objects id.
static asJsonProperties(array $objects, array $properties, int $options=0)
static findBy(?array $objects, $value, string $property='id')
Finds and returns the first occurrence of the array using one of the objects properties.
static compute(?array $objects, callable $callable, callable $computeCall)
static containsById(?array $objects, object $object)
Checks if an object exist in an array of objects using the object id.
static asJson(array $objects, int $options=0)
static computeSum(?array $objects, callable $callable)