Passed
Push — master ( 77da54...071749 )
by Jean-Christophe
02:39
created

JArray::callFunction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Ajax\service;
4
5
class JArray {
6
7
	public static function isAssociative($array) {
8
		return (array_values($array) !== $array);
9
	}
10
11
	public static function getValue($array, $key, $pos) {
12
		if (array_key_exists($key, $array)) {
13
			return $array[$key];
14
		}
15
		$values=array_values($array);
16
		if ($pos < sizeof($values))
17
			return $values[$pos];
18
	}
19
20
	public static function getConditionalValue($array, $key, $condition) {
21
		$result=NULL;
22
		if (array_key_exists($key, $array)) {
23
			$result=$array[$key];
24
			if ($condition($result) === true)
25
				return $result;
26
		}
27
		$values=array_values($array);
28
		foreach ( $values as $val ) {
29
			if ($condition($val) === true)
30
				return $val;
31
		}
32
		return $result;
33
	}
34
35
	public static function getDefaultValue($array, $key, $default=NULL) {
36
		if (array_key_exists($key, $array)) {
37
			return $array[$key];
38
		} else
39
			return $default;
40
	}
41
42
	public static function implode($glue, $pieces) {
43
		$result="";
44
		if (\is_array($glue)) {
45
			$size=\sizeof($pieces);
46
			if ($size > 0) {
47
				for($i=0; $i < $size - 1; $i++) {
48
					$result.=$pieces[$i] . @$glue[$i];
49
				}
50
				$result.=$pieces[$size - 1];
51
			}
52
		} else {
53
			$result=\implode($glue, $pieces);
54
		}
55
		return $result;
56
	}
57
58
	public static function dimension($array) {
59
		if (\is_array(reset($array))) {
60
			$return=self::dimension(reset($array)) + 1;
61
		} else {
62
			$return=1;
63
		}
64
		return $return;
65
	}
66
67
	public static function sortAssociative($array, $sortedKeys=array()) {
68
		$newArray=array ();
69
		foreach ( $sortedKeys as $key ) {
70
			if (\array_key_exists($key, $array)) {
71
				$newArray[$key]=$array[$key];
72
			}
73
		}
74
		return $newArray;
75
	}
76
77
	public static function moveElementTo(&$array, $from, $to) {
78
		$result=false;
79
		if(isset($array)){
80
			if(isset($array[$from])){
81
				$out = array_splice($array, $from, 1);
82
				array_splice($array, $to, 0, $out);
83
				$result=true;
84
			}
85
		}
86
		return $result;
87
	}
88
89
	public static function swapElements(&$array,$index1,$index2){
90
		$result=false;
91
		if(isset($array)){
92
			if(isset($array[$index1])&& isset($array[$index2])){
93
				$tmp=$array[$index1];
94
				$array[$index1]=$array[$index2];
95
				$array[$index2]=$tmp;
96
				$result=true;
97
			}
98
		}
99
		return $result;
100
	}
101
102
103
	public static function modelArray($objects,$identifierFunction=NULL,$modelFunction=NULL){
104
		$result=[];
105
		if(isset($modelFunction)===false){
106
			$modelFunction="__toString";
107
		}
108
		if(isset($identifierFunction)===false){
109
			foreach ($objects as $object){
110
				$result[]=self::callFunction($object, $modelFunction);
111
			}
112
		}else{
113
			foreach ($objects as $object){
114
				$result[self::callFunction($object, $identifierFunction)]=self::callFunction($object, $modelFunction);
115
			}
116
		}
117
		return $result;
118
	}
119
120
	private static function callFunction($object,$callback){
121
		if(\is_string($callback))
122
			return \call_user_func(array($object, $callback),[]);
123
		else if (\is_callable($callback)){
124
			return $callback($object);
125
		}
126
	}
127
128
	public static function count($array){
129
		if(\is_array($array)){
130
			return \sizeof($array);
131
		}
132
		return 0;
133
	}
134
}
135