Completed
Push — master ( 1f90e4...0f8170 )
by Jean-Christophe
02:14
created

JArray::getValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 3
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
	private static function parseValue($v,$prefix=""){
46
		if(\is_bool($v)===true){
47
			$result=StrUtils::getBooleanStr($v);
48
		}elseif(\is_array($v)){
49
			$result=self::asPhpArray($v,$prefix);
50
		}
51
		else{
52
			$result="\"".$v."\"";
53
		}
54
		return $result;
55
	}
56
}
57