Completed
Push — master ( 77fbe2...eac339 )
by Jean-Christophe
02:27
created

UArray::parseValue()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 14
rs 8.8571
c 1
b 0
f 0
cc 5
eloc 12
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[]="\"" . $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
	private static function parseValue($v, $prefix="",$depth=1,$format=false) {
117
		if (UString::isBooleanStr($v)) {
118
			$result=UString::getBooleanStr($v);
119
		} elseif (\is_numeric($v)) {
120
			$result=$v;
121
		} elseif (\is_array($v)) {
122
			$result=self::asPhpArray($v, $prefix,$depth+1,$format);
123
		}elseif(UString::startswith(trim($v), "function")){
124
			$result=$v;
125
		} else {
126
			$result="\"" . \str_replace('$', '\$', $v) . "\"";
127
		}
128
		return $result;
129
	}
130
}
131