Completed
Push — master ( af6f2f...54d848 )
by Jean-Christophe
01:36
created

JArray::removeOne()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace micro\utils;
4
5
class JArray {
6
7
	public static function isAssociative($array) {
8
		return (array_keys($array) !== range(0, count($array) - 1));
9
	}
10
11
	public static function getValue($array, $key, $pos) {
12
		if (array_key_exists($key, $array)) {
13
			return $array[$key];
14
		}
15
		$values=array_values($array);
16
		if ($pos < sizeof($values))
17
			return $values[$pos];
18
	}
19
20
	public static function getDefaultValue($array, $key, $default) {
21
		if (array_key_exists($key, $array)) {
22
			return $array[$key];
23
		} else
24
			return $default;
25
	}
26
27
	public static function asPhpArray($array, $prefix="") {
28
		$exts=array ();
29
		$extsStr="";
30
		if (self::isAssociative($array)) {
31
			foreach ( $array as $k => $v ) {
32
				$exts[]="\"" . $k . "\"=>" . self::parseValue($v, $prefix);
33
			}
34
		} else {
35
			foreach ( $array as $v ) {
36
				$exts[]=self::parseValue($v, $prefix);
37
			}
38
		}
39
		if (\sizeof($exts) > 0 || $prefix !== "") {
40
			$extsStr="(" . \implode(",", $exts) . ")";
41
		}
42
		return $prefix . $extsStr;
43
	}
44
45
	public static function remove($array,$search){
46
		if(\is_array($search)){
47
			foreach ($search as $val){
48
				$array=self::removeOne($array, $val);
49
			}
50
		}else{
51
			$array=self::removeOne($array, $search);
52
		}
53
		return array_values($array);
54
	}
55
56
	public static function removeOne($array,$search){
57
		if (($key = array_search($search, $array)) !== false) {
58
			unset($array[$key]);
59
		}
60
		return $array;
61
	}
62
63
	private static function parseValue($v, $prefix="") {
64
		if (\is_bool($v) === true) {
65
			$result=StrUtils::getBooleanStr($v);
66
		} elseif (\is_numeric($v)) {
67
			$result=$v;
68
		} elseif (\is_array($v)) {
69
			$result=self::asPhpArray($v, $prefix);
70
		} else {
71
			$result="\"" . \str_replace('$', '\$', $v) . "\"";
72
		}
73
		return $result;
74
	}
75
}
76