Completed
Push — master ( 61f3b2...32110c )
by Jean-Christophe
02:56
created

UArray::parseValue()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 15
rs 8.8571
c 1
b 0
f 0
cc 6
eloc 13
nc 5
nop 4
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 getDefaultValue($array, $key, $default) {
47
		if (array_key_exists($key, $array)) {
48
			return $array[$key];
49
		} else
50
			return $default;
51
	}
52
53
	public static function asPhpArray($array, $prefix="",$depth=1,$format=false) {
54
		$exts=array ();
55
		$extsStr="";$tab="";$nl="";
56
		if($format){
57
			$tab=str_repeat("\t",$depth);
58
			$nl=PHP_EOL;
59
		}
60
		if (self::isAssociative($array)) {
61
			foreach ( $array as $k => $v ) {
62
				$exts[]="\"" . UString::doubleBackSlashes($k) . "\"=>" . self::parseValue($v, 'array',$depth+1,$format);
63
			}
64
		} else {
65
			foreach ( $array as $v ) {
66
				$exts[]=self::parseValue($v, 'array',$depth+1,$format);
67
			}
68
		}
69
		if (\sizeof($exts) > 0 || $prefix !== "") {
70
			$extsStr="(" . \implode(",".$nl.$tab, $exts).")";
71
			if(\sizeof($exts)>0){
72
				$extsStr="(" .$nl.$tab. \implode(",".$nl.$tab, $exts).$nl.$tab.")";
73
			}
74
		}
75
		return $prefix . $extsStr;
76
	}
77
	
78
	public static function asJSON($array){
79
		return json_encode($array, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
80
	}
81
82 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...
83
		if (\is_array($search)) {
84
			foreach ( $search as $val ) {
85
				$array=self::removeOne($array, $val);
86
			}
87
		} else {
88
			$array=self::removeOne($array, $search);
89
		}
90
		return array_values($array);
91
	}
92
	
93
	/**
94
	 * Removes from array by key
95
	 * @param array $array
96
	 * @param int|string $key
97
	 * @return array
98
	 */
99
	public static function removeByKey($array,$key){
100
		if(isset($array[$key])){
101
			unset($array[$key]);
102
		}
103
		return $array;
104
	}
105
	
106
	public static function removeByKeys($array,$keys){
107
		$assocKeys = [];
108
		foreach($keys as $key) {
109
			$assocKeys[$key] = true;
110
		}
111
		return array_diff_key($array, $assocKeys);
112
	}
113
114
	public static function removeOne($array, $search) {
115
		if (($key=array_search($search, $array)) !== false) {
116
			unset($array[$key]);
117
		}
118
		return $array;
119
	}
120
121
	public static function update(&$array, $search, $newValue) {
122
		if (($key=array_search($search, $array)) !== false) {
123
			$array[$key]=$newValue;
124
		}
125
		return $array;
126
	}
127
	
128
	public static function doubleBackSlashes(&$array){
129
		return array_walk($array, function($value){$value=UString::doubleBackSlashes($value);});
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
130
	}
131
132
	private static function parseValue($v, $prefix="",$depth=1,$format=false) {
133
		if (UString::isBooleanStr($v)) {
134
			$result=UString::getBooleanStr($v);
135
		} elseif (\is_numeric($v)) {
136
			$result=$v;
137
		} elseif (\is_array($v)) {
138
			$result=self::asPhpArray($v, $prefix,$depth+1,$format);
139
		}elseif(UString::startswith(trim($v), "function") || UString::startswith(trim($v), "array(")){
140
			$result=$v;
141
		} else {
142
			$result="\"" . \str_replace('$', '\$', $v) . "\"";
143
			$result=UString::doubleBackSlashes($result);
144
		}
145
		return $result;
146
	}
147
	
148
	public static function iSearch($needle,$haystack,$strict=null){
149
		return array_search(strtolower($needle), array_map('strtolower', $haystack),$strict);
150
	}
151
	
152 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...
153
		if (\is_array($search)) {
154
			foreach ( $search as $val ) {
155
				$array=self::iRemoveOne($array, $val);
156
			}
157
		} else {
158
			$array=self::iRemoveOne($array, $search);
159
		}
160
		return array_values($array);
161
	}
162
	
163
	public static function iRemoveOne($array, $search) {
164
		if (($key=self::iSearch($search, $array)) !== false) {
165
			unset($array[$key]);
166
		}
167
		return $array;
168
	}
169
}
170