Passed
Push — master ( b91873...b1e0e9 )
by Jean-Christophe
11:12
created

UArrayModels::removeAll()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 11
ccs 0
cts 8
cp 0
rs 10
cc 4
nc 6
nop 2
crap 20
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.1
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
	public static function sort(array $array , callable $callback):array{
22
		\usort($array,$callback);
23
		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
	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
	
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
	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
	
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
	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
	
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
	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
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
	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
	
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
	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
	
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
	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
	
297
	/**
298
	 * @param array $objects
299
	 * @return array
300
	 */
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
	
309
	/**
310
	 * @param array $objects
311
	 * @param int $options
312
	 * @return string
313
	 */
314
	public static function asJson(array $objects,int $options=null):string{
315
		$result=[];
316
		foreach ($objects as $index=>$o) {
317
			$result[$index]=$o->_rest??[];
318
		}
319
		return \json_encode($result,$options);
320
	}
321
	
322
	/**
323
	 * @param array $objects
324
	 * @param array $properties
325
	 * @return array
326
	 */
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
	
340
	/**
341
	 * @param array $objects
342
	 * @param array $properties
343
	 * @param int $options
344
	 * @return string
345
	 */
346
	public static function asJsonProperties(array $objects,array $properties,int $options=null):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
	
359
	/**
360
	 * @param $object
361
	 * @param $callback
362
	 * @return false|mixed
363
	 */
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
378