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