Completed
Push — master ( d4e951...14a860 )
by Jean-Christophe
04:01
created

UArray::getDefaultValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ubiquity\utils\base;
4
5
/**
6
 * Array utilities
7
 *
8
 * @author jc
9
 *
10
 */
11
class UArray {
12
13
	/**
14
	 * Tests if array is associative
15
	 *
16
	 * @param array $array
17
	 * @return boolean
18
	 */
19 9
	public static function isAssociative($array) {
20 9
		return (array_keys ( $array ) !== range ( 0, count ( $array ) - 1 ));
21
	}
22
23
	/**
24
	 * Returns a new array with the keys $keys
25
	 *
26
	 * @param array $array
27
	 *        	an associative array
28
	 * @param array $keys
29
	 *        	some keys
30
	 * @return array
31
	 */
32 1
	public static function extractKeys($array, $keys) {
33 1
		$result = [ ];
34 1
		foreach ( $keys as $key ) {
35 1
			if (isset ( $array [$key] )) {
36 1
				$result [$key] = $array [$key];
37
			}
38
		}
39 1
		return $result;
40
	}
41
42
	/**
43
	 *
44
	 * @param array $array
45
	 * @param string $key
46
	 * @param int $pos
47
	 * @return mixed|null
48
	 */
49 1
	public static function getValue($array, $key, $pos) {
50 1
		if (array_key_exists ( $key, $array )) {
51 1
			return $array [$key];
52
		}
53 1
		$values = array_values ( $array );
54 1
		if ($pos < sizeof ( $values ))
55 1
			return $values [$pos];
56 1
	}
57
58
	/**
59
	 *
60
	 * @param array $array
61
	 * @param string|int $key
62
	 * @param mixed $default
63
	 * @return mixed
64
	 */
65 3
	public static function getRecursive($array, $key, $default = null) {
66 3
		if (array_key_exists ( $key, $array )) {
67 3
			return $array [$key];
68
		} else {
69 2
			foreach ( $array as $item ) {
70 2
				if (is_array ( $item )) {
71 2
					return self::getRecursive ( $item, $key, $default );
72
				}
73
			}
74
		}
75 1
		return $default;
76
	}
77
78 1
	public static function getDefaultValue($array, $key, $default) {
79 1
		return $array [$key] ?? $default;
80
	}
81
82 6
	public static function asPhpArray($array, $prefix = "", $depth = 1, $format = false) {
83 6
		$exts = array ();
84 6
		$extsStr = "";
85 6
		$tab = "";
86 6
		$nl = "";
87 6
		if ($format) {
88 1
			$tab = str_repeat ( "\t", $depth );
89 1
			$nl = PHP_EOL;
90
		}
91 6
		foreach ( $array as $k => $v ) {
92 4
			if (is_string ( $k )) {
93 4
				$exts [] = "\"" . UString::doubleBackSlashes ( $k ) . "\"=>" . self::parseValue ( $v, 'array', $depth + 1, $format );
94
			} else {
95 3
				$exts [] = self::parseValue ( $v, $prefix, $depth + 1, $format );
96
			}
97
		}
98 6
		if (\sizeof ( $exts ) > 0 || $prefix !== "") {
99 6
			$extsStr = "(" . \implode ( "," . $nl . $tab, $exts ) . ")";
100 6
			if (\sizeof ( $exts ) > 0) {
101 4
				$extsStr = "(" . $nl . $tab . \implode ( "," . $nl . $tab, $exts ) . $nl . $tab . ")";
102
			}
103
		}
104 6
		return $prefix . $extsStr;
105
	}
106
107
	public static function asJSON($array) {
108
		return json_encode ( $array, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE );
109
	}
110
111 1
	public static function remove($array, $search) {
112 1
		if (\is_array ( $search )) {
113
			foreach ( $search as $val ) {
114
				$array = self::removeOne ( $array, $val );
115
			}
116
		} else {
117 1
			$array = self::removeOne ( $array, $search );
118
		}
119 1
		return array_values ( $array );
120
	}
121
122
	/**
123
	 * Removes from array by key
124
	 *
125
	 * @param array $array
126
	 * @param int|string $key
127
	 * @return array
128
	 */
129
	public static function removeByKey($array, $key) {
130
		if (isset ( $array [$key] )) {
131
			unset ( $array [$key] );
132
		}
133
		return $array;
134
	}
135
136 2
	public static function removeRecursive(&$array, $key) {
137 2
		foreach ( $array as &$item ) {
138 2
			if (array_key_exists ( $key, $item )) {
139 2
				unset ( $item [$key] );
140 1
			} elseif (is_array ( $item )) {
141 1
				self::removeRecursive ( $item, $key );
142
			}
143
		}
144 2
	}
145
146 2
	public static function removeByKeys($array, $keys) {
147 2
		$assocKeys = [ ];
148 2
		foreach ( $keys as $key ) {
149 2
			$assocKeys [$key] = true;
150
		}
151 2
		return array_diff_key ( $array, $assocKeys );
152
	}
153
154 1
	public static function removeOne($array, $search) {
155 1
		if (($key = array_search ( $search, $array )) !== false) {
156 1
			unset ( $array [$key] );
157
		}
158 1
		return $array;
159
	}
160
161
	public static function update(&$array, $search, $newValue) {
162
		if (($key = array_search ( $search, $array )) !== false) {
163
			$array [$key] = $newValue;
164
		}
165
		return $array;
166
	}
167
168
	/**
169
	 *
170
	 * @param array $array
171
	 * @return boolean
172
	 */
173
	public static function doubleBackSlashes(&$array) {
174
		return array_walk ( $array, function (&$value) {
175
			$value = UString::doubleBackSlashes ( $value );
176
		} );
177
	}
178
179 4
	private static function parseValue($v, $prefix = "", $depth = 1, $format = false) {
180 4
		if (UString::isBooleanStr ( $v )) {
181 4
			$result = UString::getBooleanStr ( $v );
182 4
		} elseif (\is_numeric ( $v )) {
183 3
			$result = $v;
184 3
		} elseif (\is_array ( $v )) {
185 3
			$result = self::asPhpArray ( $v, $prefix, $depth + 1, $format );
186 3
		} elseif (is_string ( $v ) && (UString::startswith ( trim ( $v ), '$config' ) || UString::startswith ( trim ( $v ), "function" ) || UString::startswith ( trim ( $v ), "array(" ))) {
187
			$result = $v;
188 3
		} elseif ($v instanceof \Closure) {
189 1
			$result = UIntrospection::closure_dump ( $v );
190
		} else {
191 3
			$result = "\"" . \str_replace ( '$', '\$', $v ) . "\"";
192 3
			$result = UString::doubleBackSlashes ( $result );
193
		}
194 4
		return $result;
195
	}
196
197 2
	public static function iSearch($needle, $haystack, $strict = null) {
198 2
		return array_search ( strtolower ( $needle ), array_map ( 'strtolower', $haystack ), $strict );
199
	}
200
201
	public static function iRemove($array, $search) {
202
		if (\is_array ( $search )) {
203
			foreach ( $search as $val ) {
204
				$array = self::iRemoveOne ( $array, $val );
205
			}
206
		} else {
207
			$array = self::iRemoveOne ( $array, $search );
208
		}
209
		return array_values ( $array );
210
	}
211
212 2
	public static function iRemoveOne($array, $search) {
213 2
		if (($key = self::iSearch ( $search, $array )) !== false) {
214 2
			unset ( $array [$key] );
215
		}
216 2
		return $array;
217
	}
218
}
219