Completed
Push — master ( 782061...10806f )
by Jean-Christophe
01:37
created

UArray::getRecursive()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 4
nc 4
nop 3
1
<?php
2
3
namespace Ubiquity\utils\base;
4
5
/**
6
 * Array utilities
7
 * @author jc
8
 *
9
 */
10
class UArray {
11
12
	/**
13
	 * Tests if array is associative
14
	 * @param array $array
15
	 * @return boolean
16
	 */
17
	public static function isAssociative($array) {
18
		return (array_keys($array) !== range(0, count($array) - 1));
19
	}
20
	
21
	/**
22
	 * Returns a new array with the keys $keys
23
	 * @param array $array an associative array
24
	 * @param array $keys some keys
25
	 * @return array
26
	 */
27
	public static function extractKeys($array,$keys){
28
		$result=[];
29
		foreach ($keys as $key){
30
			if(isset($array[$key])){
31
				$result[$key]=$array[$key];
32
			}
33
		}
34
		return $result;
35
	}
36
37
	public static function getValue($array, $key, $pos) {
38
		if (array_key_exists($key, $array)) {
39
			return $array[$key];
40
		}
41
		$values=array_values($array);
42
		if ($pos < sizeof($values))
43
			return $values[$pos];
44
	}
45
	
46
	public static function getRecursive($array,$key,$default=null){
47
		if(array_key_exists($key,$array)){
48
			return $array[$key];
49
		}else{
50
			foreach ($array as $item){
51
				if(is_array($item)){
52
					return self::getRecursive($item, $key,$default);
53
				}
54
			}
55
		}
56
		return $default;
57
	}
58
59
	public static function getDefaultValue($array, $key, $default) {
60
		if (array_key_exists($key, $array)) {
61
			return $array[$key];
62
		} else
63
			return $default;
64
	}
65
66
	public static function asPhpArray($array, $prefix="",$depth=1,$format=false) {
67
		$exts=array ();
68
		$extsStr="";$tab="";$nl="";
69
		if($format){
70
			$tab=str_repeat("\t",$depth);
71
			$nl=PHP_EOL;
72
		}
73
		foreach ( $array as $k => $v ) {
74
			if(is_string($k)){
75
				$exts[]="\"" . UString::doubleBackSlashes($k) . "\"=>" . self::parseValue($v, 'array',$depth+1,$format);
76
			}else{
77
				$exts[]=self::parseValue($v, $prefix,$depth+1,$format);
78
			}
79
		}
80
		if (\sizeof($exts) > 0 || $prefix !== "") {
81
			$extsStr="(" . \implode(",".$nl.$tab, $exts).")";
82
			if(\sizeof($exts)>0){
83
				$extsStr="(" .$nl.$tab. \implode(",".$nl.$tab, $exts).$nl.$tab.")";
84
			}
85
		}
86
		return $prefix . $extsStr;
87
	}
88
	
89
	public static function asJSON($array){
90
		return json_encode($array, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
91
	}
92
93 View Code Duplication
	public static function remove($array, $search) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
		if (\is_array($search)) {
95
			foreach ( $search as $val ) {
96
				$array=self::removeOne($array, $val);
97
			}
98
		} else {
99
			$array=self::removeOne($array, $search);
100
		}
101
		return array_values($array);
102
	}
103
	
104
	/**
105
	 * Removes from array by key
106
	 * @param array $array
107
	 * @param int|string $key
108
	 * @return array
109
	 */
110
	public static function removeByKey($array,$key){
111
		if(isset($array[$key])){
112
			unset($array[$key]);
113
		}
114
		return $array;
115
	}
116
	
117
	public static function removeRecursive(&$array,$key){
118
		foreach ($array as &$item){
119
			if(array_key_exists($key, $item)){
120
				unset($item[$key]);
121
			}elseif(is_array($item)){
122
				self::removeRecursive($item, $key);
123
			}
124
		}
125
	}
126
	
127
	public static function removeByKeys($array,$keys){
128
		$assocKeys = [];
129
		foreach($keys as $key) {
130
			$assocKeys[$key] = true;
131
		}
132
		return array_diff_key($array, $assocKeys);
133
	}
134
135
	public static function removeOne($array, $search) {
136
		if (($key=array_search($search, $array)) !== false) {
137
			unset($array[$key]);
138
		}
139
		return $array;
140
	}
141
142
	public static function update(&$array, $search, $newValue) {
143
		if (($key=array_search($search, $array)) !== false) {
144
			$array[$key]=$newValue;
145
		}
146
		return $array;
147
	}
148
	
149
	/**
150
	 * @param array $array
151
	 * @return boolean
152
	 */
153
	public static function doubleBackSlashes(&$array){
154
		return array_walk($array, function(&$value){$value=UString::doubleBackSlashes($value);});
155
	}
156
157
	private static function parseValue($v, $prefix="",$depth=1,$format=false) {
158
		if (UString::isBooleanStr($v)) {
159
			$result=UString::getBooleanStr($v);
160
		} elseif (\is_numeric($v)) {
161
			$result=$v;
162
		} elseif (\is_array($v)) {
163
			$result=self::asPhpArray($v, $prefix,$depth+1,$format);
164
		}elseif(is_string($v) && (UString::startswith(trim($v), '$config') || UString::startswith(trim($v), "function") || UString::startswith(trim($v), "array("))){
165
			$result=$v;
166
		}elseif($v instanceof \Closure){
167
			$result=UIntrospection::closure_dump($v);
168
		}else {
169
			$result="\"" . \str_replace('$', '\$', $v) . "\"";
170
			$result=UString::doubleBackSlashes($result);
171
		}
172
		return $result;
173
	}
174
	
175
	public static function iSearch($needle,$haystack,$strict=null){
176
		return array_search(strtolower($needle), array_map('strtolower', $haystack),$strict);
177
	}
178
	
179 View Code Duplication
	public static function iRemove($array, $search) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
180
		if (\is_array($search)) {
181
			foreach ( $search as $val ) {
182
				$array=self::iRemoveOne($array, $val);
183
			}
184
		} else {
185
			$array=self::iRemoveOne($array, $search);
186
		}
187
		return array_values($array);
188
	}
189
	
190
	public static function iRemoveOne($array, $search) {
191
		if (($key=self::iSearch($search, $array)) !== false) {
192
			unset($array[$key]);
193
		}
194
		return $array;
195
	}
196
	
197
	
198
}
199