Completed
Push — master ( b4ccca...781bd5 )
by Jean-Christophe
03:50
created

PropertyWrapper::containsElement()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.2
cc 4
eloc 5
nc 3
nop 1
1
<?php
2
3
namespace Ajax\common\html;
4
5
use Ajax\service\JArray;
6
7
class PropertyWrapper {
8
9
	public static function wrap($input, $js=NULL, $separator=' ', $valueQuote='"') {
10
		$output="";
11
		if (is_string($input)) {
12
			$output=$input;
13
		}
14
		if (is_array($input)) {
15
			if (sizeof($input) > 0) {
16
				if (self::containsElement($input) === false) {
17
					$output=self::wrapStrings($input, $js, $separator=' ', $valueQuote='"');
18
				} else {
19
					$output=self::wrapObjects($input, $js, $separator, $valueQuote);
20
				}
21
			}
22
		}
23
		return $output;
24
	}
25
26
	private static function containsElement($input) {
27
		foreach ( $input as $v ) {
28
			if (\is_object($v) === true || \is_array($v))
29
				return true;
30
		}
31
		return false;
32
	}
33
34
	public static function wrapStrings($input, $js, $separator=' ', $valueQuote='"') {
0 ignored issues
show
Unused Code introduced by
The parameter $js is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
		if (JArray::isAssociative($input) === true) {
36
			$result=implode($separator, array_map(function ($v, $k) use($valueQuote) {
37
				return $k . '=' . $valueQuote . $v . $valueQuote;
38
			}, $input, array_keys($input)));
39
		} else {
40
			$result=implode($separator, array_values($input));
41
		}
42
		return $result;
43
	}
44
45
	public static function wrapObjects($input, $js=NULL, $separator=' ', $valueQuote='"') {
46
		return implode($separator, array_map(function ($v) use($js, $separator, $valueQuote) {
47
			if (is_object($v))
48
				return $v->compile($js);
49
			elseif (\is_array($v)) {
50
				return self::wrap($v, $js, $separator, $valueQuote);
51
			} else
52
				return $v;
53
		}, $input));
54
	}
55
}