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
|
|
|
// return (array_keys($array)!==range(0, count($array)-1)); |
|
|
|
|
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
public static function getValue($array, $key, $pos) { |
13
|
|
|
if (array_key_exists($key, $array)) { |
14
|
|
|
return $array[$key]; |
15
|
|
|
} |
16
|
|
|
$values=array_values($array); |
17
|
|
|
if ($pos < sizeof($values)) |
18
|
|
|
return $values[$pos]; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public static function getConditionalValue($array, $key, $condition) { |
22
|
|
|
$result=NULL; |
23
|
|
|
if (array_key_exists($key, $array)) { |
24
|
|
|
$result=$array[$key]; |
25
|
|
|
if ($condition($result) === true) |
26
|
|
|
return $result; |
27
|
|
|
} |
28
|
|
|
$values=array_values($array); |
29
|
|
|
foreach ( $values as $val ) { |
30
|
|
|
if ($condition($val) === true) |
31
|
|
|
return $val; |
32
|
|
|
} |
33
|
|
|
return $result; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function getDefaultValue($array, $key, $default=NULL) { |
37
|
|
|
if (array_key_exists($key, $array)) { |
38
|
|
|
return $array[$key]; |
39
|
|
|
} else |
40
|
|
|
return $default; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function implode($glue, $pieces) { |
44
|
|
|
$result=""; |
45
|
|
|
if (\is_array($glue)) { |
46
|
|
|
$size=\sizeof($pieces); |
47
|
|
|
if ($size > 0) { |
48
|
|
|
for($i=0; $i < $size - 1; $i++) { |
49
|
|
|
$result.=$pieces[$i] . @$glue[$i]; |
50
|
|
|
} |
51
|
|
|
$result.=$pieces[$size - 1]; |
52
|
|
|
} |
53
|
|
|
} else { |
54
|
|
|
$result=\implode($glue, $pieces); |
55
|
|
|
} |
56
|
|
|
return $result; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public static function dimension($array) { |
60
|
|
|
if (is_array(reset($array))) { |
61
|
|
|
$return=self::dimension(reset($array)) + 1; |
62
|
|
|
} else { |
63
|
|
|
$return=1; |
64
|
|
|
} |
65
|
|
|
return $return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function sortAssociative($array, $sortedKeys=array()) { |
69
|
|
|
$newArray=array (); |
70
|
|
|
foreach ( $sortedKeys as $key ) { |
71
|
|
|
if (\array_key_exists($key, $array)) { |
72
|
|
|
$newArray[$key]=$array[$key]; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
return $newArray; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public static function modelArray($objects,$identifierFunction=NULL,$modelFunction=NULL){ |
79
|
|
|
$result=[]; |
80
|
|
|
if(isset($modelFunction)===false){ |
81
|
|
|
$modelFunction="__toString"; |
82
|
|
|
} |
83
|
|
|
if(isset($identifierFunction)===false){ |
84
|
|
|
foreach ($objects as $object){ |
85
|
|
|
$result[]=self::callFunction($object, $modelFunction); |
86
|
|
|
} |
87
|
|
|
}else{ |
88
|
|
|
foreach ($objects as $object){ |
89
|
|
|
$result[self::callFunction($object, $identifierFunction)]=self::callFunction($object, $modelFunction); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
return $result; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private static function callFunction($object,$callback){ |
96
|
|
|
if(\is_string($callback)) |
97
|
|
|
return \call_user_func(array($object, $callback),[]); |
98
|
|
|
else if (\is_callable($callback)){ |
99
|
|
|
return $callback($object); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.