Completed
Push — master ( aff06e...dd04e7 )
by Jean-Christophe
02:50 queued 01:02
created

UArray::removeOne()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
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
		if (self::isAssociative($array)) {
74
			foreach ( $array as $k => $v ) {
75
				$exts[]="\"" . UString::doubleBackSlashes($k) . "\"=>" . self::parseValue($v, 'array',$depth+1,$format);
76
			}
77
		} else {
78
			foreach ( $array as $v ) {
79
				$exts[]=self::parseValue($v, 'array',$depth+1,$format);
80
			}
81
		}
82
		if (\sizeof($exts) > 0 || $prefix !== "") {
83
			$extsStr="(" . \implode(",".$nl.$tab, $exts).")";
84
			if(\sizeof($exts)>0){
85
				$extsStr="(" .$nl.$tab. \implode(",".$nl.$tab, $exts).$nl.$tab.")";
86
			}
87
		}
88
		return $prefix . $extsStr;
89
	}
90
	
91
	public static function asJSON($array){
92
		return json_encode($array, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
93
	}
94
95 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...
96
		if (\is_array($search)) {
97
			foreach ( $search as $val ) {
98
				$array=self::removeOne($array, $val);
99
			}
100
		} else {
101
			$array=self::removeOne($array, $search);
102
		}
103
		return array_values($array);
104
	}
105
	
106
	/**
107
	 * Removes from array by key
108
	 * @param array $array
109
	 * @param int|string $key
110
	 * @return array
111
	 */
112
	public static function removeByKey($array,$key){
113
		if(isset($array[$key])){
114
			unset($array[$key]);
115
		}
116
		return $array;
117
	}
118
	
119
	public static function removeRecursive(&$array,$key){
120
		foreach ($array as &$item){
121
			if(array_key_exists($key, $item)){
122
				unset($item[$key]);
123
			}elseif(is_array($item)){
124
				self::removeRecursive($item, $key);
125
			}
126
		}
127
	}
128
	
129
	public static function removeByKeys($array,$keys){
130
		$assocKeys = [];
131
		foreach($keys as $key) {
132
			$assocKeys[$key] = true;
133
		}
134
		return array_diff_key($array, $assocKeys);
135
	}
136
137
	public static function removeOne($array, $search) {
138
		if (($key=array_search($search, $array)) !== false) {
139
			unset($array[$key]);
140
		}
141
		return $array;
142
	}
143
144
	public static function update(&$array, $search, $newValue) {
145
		if (($key=array_search($search, $array)) !== false) {
146
			$array[$key]=$newValue;
147
		}
148
		return $array;
149
	}
150
	
151
	/**
152
	 * @param array $array
153
	 * @return boolean
154
	 */
155
	public static function doubleBackSlashes(&$array){
156
		return array_walk($array, function(&$value){$value=UString::doubleBackSlashes($value);});
157
	}
158
159
	private static function parseValue($v, $prefix="",$depth=1,$format=false) {
160
		if (UString::isBooleanStr($v)) {
161
			$result=UString::getBooleanStr($v);
162
		} elseif (\is_numeric($v)) {
163
			$result=$v;
164
		} elseif (\is_array($v)) {
165
			$result=self::asPhpArray($v, $prefix,$depth+1,$format);
166
		}elseif(is_string($v) && (UString::startswith(trim($v), '$config') || UString::startswith(trim($v), "function") || UString::startswith(trim($v), "array("))){
167
			$result=$v;
168
		}elseif($v instanceof \Closure){
169
			$result=UIntrospection::closure_dump($v);
170
		}else {
171
			$result="\"" . \str_replace('$', '\$', $v) . "\"";
172
			$result=UString::doubleBackSlashes($result);
173
		}
174
		return $result;
175
	}
176
	
177
	public static function iSearch($needle,$haystack,$strict=null){
178
		return array_search(strtolower($needle), array_map('strtolower', $haystack),$strict);
179
	}
180
	
181 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...
182
		if (\is_array($search)) {
183
			foreach ( $search as $val ) {
184
				$array=self::iRemoveOne($array, $val);
185
			}
186
		} else {
187
			$array=self::iRemoveOne($array, $search);
188
		}
189
		return array_values($array);
190
	}
191
	
192
	public static function iRemoveOne($array, $search) {
193
		if (($key=self::iSearch($search, $array)) !== false) {
194
			unset($array[$key]);
195
		}
196
		return $array;
197
	}
198
	
199
	
200
}
201