Completed
Push — master ( abc246...138cdf )
by Jean-Christophe
03:36
created

JArray::moveElementTo()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 8
nc 3
nop 3
1
<?php
2
3
namespace Ajax\service;
4
5
class JArray {
6
7
	public static function isAssociative($array) {
8
		return (array_values($array) !== $array);
9
		// return (array_keys($array)!==range(0, count($array)-1));
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
10
	}
11
12
	public static function getValue($array, $key, $pos) {
13
		if (array_key_exists($key, $array)) {
14
			return $array[$key];
15
		}
16
		$values=array_values($array);
17
		if ($pos < sizeof($values))
18
			return $values[$pos];
19
	}
20
21
	public static function getConditionalValue($array, $key, $condition) {
22
		$result=NULL;
23
		if (array_key_exists($key, $array)) {
24
			$result=$array[$key];
25
			if ($condition($result) === true)
26
				return $result;
27
		}
28
		$values=array_values($array);
29
		foreach ( $values as $val ) {
30
			if ($condition($val) === true)
31
				return $val;
32
		}
33
		return $result;
34
	}
35
36
	public static function getDefaultValue($array, $key, $default=NULL) {
37
		if (array_key_exists($key, $array)) {
38
			return $array[$key];
39
		} else
40
			return $default;
41
	}
42
43
	public static function implode($glue, $pieces) {
44
		$result="";
45
		if (\is_array($glue)) {
46
			$size=\sizeof($pieces);
47
			if ($size > 0) {
48
				for($i=0; $i < $size - 1; $i++) {
49
					$result.=$pieces[$i] . @$glue[$i];
50
				}
51
				$result.=$pieces[$size - 1];
52
			}
53
		} else {
54
			$result=\implode($glue, $pieces);
55
		}
56
		return $result;
57
	}
58
59
	public static function dimension($array) {
60
		if (\is_array(reset($array))) {
61
			$return=self::dimension(reset($array)) + 1;
62
		} else {
63
			$return=1;
64
		}
65
		return $return;
66
	}
67
68
	public static function sortAssociative($array, $sortedKeys=array()) {
69
		$newArray=array ();
70
		foreach ( $sortedKeys as $key ) {
71
			if (\array_key_exists($key, $array)) {
72
				$newArray[$key]=$array[$key];
73
			}
74
		}
75
		return $newArray;
76
	}
77
78
	public static function moveElementTo(&$array, $from, $to) {
79
		$result=false;
80
		if(isset($array)){
81
			if(isset($array[$from])){
82
				$out = array_splice($array, $from, 1);
83
				array_splice($array, $to, 0, $out);
84
				$result=true;
85
			}
86
		}
87
		return $result;
88
	}
89
90
	public static function swapElements(&$array,$index1,$index2){
91
		$result=false;
92
		if(isset($array)){
93
			if(isset($array[$index1])&& isset($array[$index2])){
94
				$tmp=$array[$index1];
95
				$array[$index1]=$array[$index2];
96
				$array[$index2]=$tmp;
97
				$result=true;
98
			}
99
		}
100
		return $result;
101
	}
102
103
104
	public static function modelArray($objects,$identifierFunction=NULL,$modelFunction=NULL){
105
		$result=[];
106
		if(isset($modelFunction)===false){
107
			$modelFunction="__toString";
108
		}
109
		if(isset($identifierFunction)===false){
110
			foreach ($objects as $object){
111
				$result[]=self::callFunction($object, $modelFunction);
112
			}
113
		}else{
114
			foreach ($objects as $object){
115
				$result[self::callFunction($object, $identifierFunction)]=self::callFunction($object, $modelFunction);
116
			}
117
		}
118
		return $result;
119
	}
120
121
	private static function callFunction($object,$callback){
122
		if(\is_string($callback))
123
			return \call_user_func(array($object, $callback),[]);
124
		else if (\is_callable($callback)){
125
			return $callback($object);
126
		}
127
	}
128
}