Completed
Push — master ( 492ae6...2f0c4d )
by Jean-Christophe
01:53
created

UArray::doubleBackSlashes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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 remove($array, $search) {
79
		if (\is_array($search)) {
80
			foreach ( $search as $val ) {
81
				$array=self::removeOne($array, $val);
82
			}
83
		} else {
84
			$array=self::removeOne($array, $search);
85
		}
86
		return array_values($array);
87
	}
88
	
89
	/**
90
	 * Removes from array by key
91
	 * @param array $array
92
	 * @param int|string $key
93
	 * @return array
94
	 */
95
	public static function removeByKey($array,$key){
96
		if(isset($array[$key])){
97
			unset($array[$key]);
98
		}
99
		return $array;
100
	}
101
102
	public static function removeOne($array, $search) {
103
		if (($key=array_search($search, $array)) !== false) {
104
			unset($array[$key]);
105
		}
106
		return $array;
107
	}
108
109
	public static function update(&$array, $search, $newValue) {
110
		if (($key=array_search($search, $array)) !== false) {
111
			$array[$key]=$newValue;
112
		}
113
		return $array;
114
	}
115
	
116
	public static function doubleBackSlashes(&$array){
117
		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...
118
	}
119
120
	private static function parseValue($v, $prefix="",$depth=1,$format=false) {
121
		if (UString::isBooleanStr($v)) {
122
			$result=UString::getBooleanStr($v);
123
		} elseif (\is_numeric($v)) {
124
			$result=$v;
125
		} elseif (\is_array($v)) {
126
			$result=self::asPhpArray($v, $prefix,$depth+1,$format);
127
		}elseif(UString::startswith(trim($v), "function") || UString::startswith(trim($v), "array(")){
128
			$result=$v;
129
		} else {
130
			$result="\"" . \str_replace('$', '\$', $v) . "\"";
131
			$result=UString::doubleBackSlashes($result);
132
		}
133
		return $result;
134
	}
135
}
136