Passed
Pull Request — master (#201)
by Jean-Christophe
26:40
created

UArrayAsTrait::isExpression()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
cc 4
nc 4
nop 1
crap 4
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.7
13
 *
14
 */
15
trait UArrayAsTrait {
16 60
	private static function isExpression(string $v): bool {
17 60
		$v=\trim($v);
18 60
		return UString::startswith ( $v , '$' ) || UString::startswith ( $v , 'function' ) || UString::startswith ( $v , 'array(' ) || UString::startswith ( $v , 'getenv(' );
19
	}
20 61
	private static function parseValue($v, $depth = 1, $format = false) {
21 61
		if (\is_numeric ( $v ) && gettype($v)!=='string') {
22 15
			$result = $v;
23 60
		} elseif ($v !== '' && UString::isBooleanStr ( $v )) {
24 55
			$result = UString::getBooleanStr ( $v );
25 60
		} elseif (\is_array ( $v )) {
26 56
			$result = self::asPhpArray_ ( $v, $depth + 1, $format );
27 60
		} elseif (\is_string ( $v ) && self::isExpression($v)) {
28 9
			$result = trim($v);
29 60
		} elseif ($v instanceof \Closure) {
30 11
			$result = UIntrospection::closure_dump ( $v );
31 60
		} elseif ($v instanceof \DateTime) {
32
			$result = "\DateTime::createFromFormat('Y-m-d H:i:s','" . $v->format ( 'Y-m-d H:i:s' ) . "')";
33
		} else {
34 60
			$result = UString::doubleBackSlashes ( $v );
35 60
			$result = "\"" . \str_replace ( [ '$','"' ], [ '\$','\"' ], $result ) . "\"";
36
		}
37 61
		return $result;
38
	}
39 62
	private static function as_($array,$formatParams=['prefix' => '','before'=>'(','after'=>')'], $valueCallback=null,$depth = 1,$format = false) {
40 62
		$exts = [];
41 62
		$extsStr = '';
42 62
		$tab = '';
43 62
		$nl = '';
44 62
		if ($format) {
45 39
			$tab = \str_repeat ( "\t", $depth );
46 39
			$nl = PHP_EOL;
47
		}
48 62
		foreach ( $array as $k => $v ) {
49 61
			$v=self::parseValue ( $v, $depth + 1, $format );
50 61
			if (\is_string ( $k )) {
51 61
				if(isset($valueCallback)){
52
					$exts []=$valueCallback($k,$v);
53
				}else{
54 61
					$exts [] = "\"" . UString::doubleBackSlashes ( $k ) . "\"=>" . $v;
55
				}
56
			} else {
57 18
				$exts [] = $v;
58
			}
59
		}
60
61 62
		if (\count ( $exts ) > 0) {
62 61
			$extsStr = $formatParams['before']."{$nl}{$tab}" . \implode ( ",{$nl}{$tab}", $exts ) . "{$nl}{$tab}".$formatParams['after'];
63
		}else{
64 50
			$extsStr = $formatParams['before'].$formatParams['after'];
65
		}
66 62
		return $formatParams['prefix'] . $extsStr;
67
	}
68 62
	public static function asPhpArray($array, $prefix = '', $depth = 1, $format = false) {
69 62
		return self::as_($array,['prefix'=>$prefix,'before'=>'(','after'=>')'],null,$depth,$format);
70
	}
71
72 56
	public static function asPhpArray_($array, $depth = 1, $format = false) {
73 56
		return self::as_($array,['prefix'=>'','before'=>'[','after'=>']'],null,$depth,$format);
74
	}
75
76
	public static function asPhpAttribute($array, $prefix = '', $depth = 1, $format = false) {
77
		return self::as_($array,['prefix'=>$prefix,'before'=>'(','after'=>')'],function($k,$v){return $k.': '.$v;},$depth,$format);
78
	}
79
80 28
	public static function asPhpClass($array, $name, $namespace = '', $format = false) {
81 28
		$tab = '';
82 28
		$nl = '';
83 28
		if ($format) {
84 28
			$tab = "\t";
85 28
			$nl = PHP_EOL;
86
		}
87 28
		$content = 'public static $value=' . self::asPhpArray ( $array, 'array', 1, true ) . ';';
88 28
		if ($namespace != null) {
89 28
			$namespace = "namespace {$namespace};{$nl}";
90
		}
91 28
		return "{$namespace}class {$name} {" . $nl . $tab . $content . $nl . $tab . "}";
92
	}
93
94 1
	public static function asJSON($array) {
95 1
		return \json_encode ( $array, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE );
96
	}
97
}
98
99