Passed
Push — master ( b68a78...d29a76 )
by Jean-Christophe
16:10 queued 03:52
created

UArrayAsTrait::as_()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6.0052

Importance

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