Test Failed
Push — master ( 93f81f...e78505 )
by Jean-Christophe
78:02
created

UArrayAsTrait::as_()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 30
ccs 22
cts 22
cp 1
rs 8.6186
cc 7
nc 32
nop 5
crap 7
1
<?php
2
3
namespace Ubiquity\utils\base\traits;
4
5
use Ubiquity\utils\base\UIntrospection;
6
use Ubiquity\utils\base\UString;
7
8
/**
9
 * Ubiquity\utils\base\traits$UArrayAsTrait
10
 * This class is part of Ubiquity
11
 * @author jc
12
 * @version 1.0.6
13
 *
14
 */
15
trait UArrayAsTrait {
16 59
	private static function parseValue($v, $formatParams,$valueCallback=null, $depth = 1, $format = false) {
0 ignored issues
show
Unused Code introduced by
The parameter $formatParams is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

16
	private static function parseValue($v, /** @scrutinizer ignore-unused */ $formatParams,$valueCallback=null, $depth = 1, $format = false) {

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

Loading history...
Unused Code introduced by
The parameter $valueCallback is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

16
	private static function parseValue($v, $formatParams,/** @scrutinizer ignore-unused */ $valueCallback=null, $depth = 1, $format = false) {

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

Loading history...
17 59
		if (\is_numeric ( $v )) {
18 54
			$result = $v;
19 58
		} elseif ($v !== '' && UString::isBooleanStr ( $v )) {
20 47
			$result = UString::getBooleanStr ( $v );
21 58
		} elseif (\is_array ( $v )) {
22 55
			$result = self::asPhpArray_ ( $v, $depth + 1, $format );
23 58
		} elseif (\is_string ( $v ) && (UString::startswith ( \trim ( $v ), '$config' ) || UString::startswith ( \trim ( $v ), 'function' ) || UString::startswith ( \trim ( $v ), 'array(' ))) {
24 9
			$result = $v;
25 58
		} elseif ($v instanceof \Closure) {
26 11
			$result = UIntrospection::closure_dump ( $v );
27 58
		} elseif ($v instanceof \DateTime) {
28
			$result = "\DateTime::createFromFormat('Y-m-d H:i:s','" . $v->format ( 'Y-m-d H:i:s' ) . "')";
29
		} else {
30 58
			$result = UString::doubleBackSlashes ( $v );
31 58
			$result = "\"" . \str_replace ( [ '$','"' ], [ '\$','\"' ], $result ) . "\"";
32
		}
33 59
		return $result;
34
	}
35 60
	private static function as_($array,$formatParams=['prefix' => '','before'=>'(','after'=>')'], $valueCallback=null,$depth = 1,$format = false) {
36 60
		$exts = array ();
37 60
		$extsStr = '';
38 60
		$tab = '';
39 60
		$nl = '';
40 60
		if ($format) {
41 33
			$tab = \str_repeat ( "\t", $depth );
42 33
			$nl = PHP_EOL;
43
		}
44 60
		foreach ( $array as $k => $v ) {
45 59
			$v=self::parseValue ( $v, $formatParams,$valueCallback, $depth + 1, $format );
46 59
			if (\is_string ( $k )) {
47
				if(isset($valueCallback)){
48 52
					$exts []=$valueCallback($k,$v);
49
				}else{
50
					$exts [] = "\"" . UString::doubleBackSlashes ( $k ) . "\"=>" . $v;
51 60
				}
52 60
			} else {
53
				$exts [] = $v;
54 60
			}
55 59
		}
56
		if ($formatParams['prefix'] !== '') {
57 60
			$extsStr = $formatParams['before'].$formatParams['after'];
0 ignored issues
show
Unused Code introduced by
The assignment to $extsStr is dead and can be removed.
Loading history...
58
		}
59
		if (\sizeof ( $exts ) > 0) {
60 28
			$extsStr = $formatParams['before']."{$nl}{$tab}" . \implode ( ",{$nl}{$tab}", $exts ) . "{$nl}{$tab}".$formatParams['after'];
61 28
		}else{
62 28
			$extsStr = $formatParams['before'].$formatParams['after'];
63 28
		}
64 28
		return $formatParams['prefix'] . $extsStr;
65 28
	}
66
	public static function asPhpArray($array, $prefix = '', $depth = 1, $format = false) {
67 28
		return self::as_($array,['prefix'=>$prefix,'before'=>'(','after'=>')'],null,$depth,$format);
68 28
	}
69 28
70
	public static function asPhpArray_($array, $depth = 1, $format = false) {
71 28
		return self::as_($array,['prefix'=>'','before'=>'[','after'=>']'],null,$depth,$format);
72
	}
73
74 1
	public static function asPhpAttribute($array, $prefix = '', $depth = 1, $format = false) {
75 1
		return self::as_($array,['prefix'=>$prefix,'before'=>'(','after'=>')'],function($k,$v){return $k.': '.$v;},$depth,$format);
76
	}
77
78
	public static function asPhpClass($array, $name, $namespace = '', $format = false) {
79
		$tab = '';
80
		$nl = '';
81
		if ($format) {
82
			$tab = "\t";
83
			$nl = PHP_EOL;
84
		}
85
		$content = 'public static $value=' . self::asPhpArray ( $array, 'array', 1, true ) . ';';
86
		if ($namespace != null) {
87
			$namespace = "namespace {$namespace};{$nl}";
88
		}
89
		return "{$namespace}class {$name} {" . $nl . $tab . $content . $nl . $tab . "}";
90
	}
91
92
	public static function asJSON($array) {
93
		return \json_encode ( $array, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE );
94
	}
95
}
96
97