Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
UArrayAsTrait.php
Go to the documentation of this file.
1<?php
2
4
7
16 private static function isExpression(string $v): bool {
17 $v=\trim($v);
18 return UString::startswith ( $v , '$' ) || UString::startswith ( $v , 'function' ) || UString::startswith ( $v , 'array(' ) || UString::startswith ( $v , 'getenv(' );
19 }
20 private static function parseValue($v, $depth = 1, $format = false) {
21 if (\is_numeric ( $v ) && gettype($v)!=='string') {
22 $result = $v;
23 } elseif ($v !== '' && UString::isBooleanStr ( $v )) {
24 $result = UString::getBooleanStr ( $v );
25 } elseif (\is_array ( $v )) {
26 $result = self::asPhpArray_ ( $v, $depth + 1, $format );
27 } elseif (\is_string ( $v ) && self::isExpression($v)) {
28 $result = trim($v);
29 } elseif ($v instanceof \Closure) {
30 $result = UIntrospection::closure_dump ( $v );
31 } 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 $result = UString::doubleBackSlashes ( $v );
35 $result = "\"" . \str_replace ( [ '$','"' ], [ '\$','\"' ], $result ) . "\"";
36 }
37 return $result;
38 }
39 private static function as_($array,$formatParams=['prefix' => '','before'=>'(','after'=>')'], $valueCallback=null,$depth = 1,$format = false) {
40 $exts = [];
41 $extsStr = '';
42 $tab = '';
43 $nl = '';
44 if ($format) {
45 $tab = \str_repeat ( "\t", $depth );
46 $nl = PHP_EOL;
47 }
48 foreach ( $array as $k => $v ) {
49 $v=self::parseValue ( $v, $depth + 1, $format );
50 if (\is_string ( $k )) {
51 if(isset($valueCallback)){
52 $exts []=$valueCallback($k,$v);
53 }else{
54 $exts [] = "\"" . UString::doubleBackSlashes ( $k ) . "\"=>" . $v;
55 }
56 } else {
57 $exts [] = $v;
58 }
59 }
60
61 if (\count ( $exts ) > 0) {
62 $extsStr = $formatParams['before']."{$nl}{$tab}" . \implode ( ",{$nl}{$tab}", $exts ) . "{$nl}{$tab}".$formatParams['after'];
63 }else{
64 $extsStr = $formatParams['before'].$formatParams['after'];
65 }
66 return $formatParams['prefix'] . $extsStr;
67 }
68 public static function asPhpArray($array, $prefix = '', $depth = 1, $format = false) {
69 return self::as_($array,['prefix'=>$prefix,'before'=>'(','after'=>')'],null,$depth,$format);
70 }
71
72 public static function asPhpArray_($array, $depth = 1, $format = false) {
73 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 public static function asPhpClass($array, $name, $namespace = '', $format = false) {
81 $tab = '';
82 $nl = '';
83 if ($format) {
84 $tab = "\t";
85 $nl = PHP_EOL;
86 }
87 $content = 'public static $value=' . self::asPhpArray ( $array, 'array', 1, true ) . ';';
88 if ($namespace != null) {
89 $namespace = "namespace {$namespace};{$nl}";
90 }
91 return "{$namespace}class {$name} {" . $nl . $tab . $content . $nl . $tab . "}";
92 }
93
94 public static function asJSON($array) {
95 return \json_encode ( $array, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE );
96 }
97}
98
Ubiquity\utils\base$UIntrospection This class is part of Ubiquity.
String utilities.
Definition UString.php:15
static isBooleanStr($value)
Definition UString.php:62
static startswith($hay, $needle)
Definition UString.php:17
static doubleBackSlashes($value)
Definition UString.php:107
static getBooleanStr($value)
Definition UString.php:38
Ubiquity\utils\base\traits$UArrayAsTrait This class is part of Ubiquity.
static parseValue($v, $depth=1, $format=false)
static asPhpAttribute($array, $prefix='', $depth=1, $format=false)
static as_($array, $formatParams=['prefix'=> '', 'before'=>'(', 'after'=>')'], $valueCallback=null, $depth=1, $format=false)
static asPhpArray_($array, $depth=1, $format=false)
static asPhpClass($array, $name, $namespace='', $format=false)
static asPhpArray($array, $prefix='', $depth=1, $format=false)