Repository navigation
Expand file tree
/
Copy pathsrc.php
More file actions
58 lines (51 loc) · 1.45 KB
/
Copy pathsrc.php
File metadata and controls
58 lines (51 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/**
*
* @param array $array
* @param int $depth
* @param bool $is_last
* @return string
*/
function getCode(array $array, int $depth = 0, bool $is_last = false): string{
$depth++;
$is_list = array_is_list($array); //since PHP 8.1.0
$result = sprintf("[%s", $is_list ? "" : "\n");
foreach($array as $key => $val){
$key_last = ($key === array_key_last($array));
if(is_array($val)){
if($is_list){
$result .= sprintf("%s", getCode($val, $depth, $key_last));
}else{
$result .= str_repeat("\t", $depth);
$result .= sprintf("%s => %s\n", getPropertyCode($key), getCode($val, $depth, $key_last));
}
}else{
if($is_list){
$result .= sprintf("%s%s", getPropertyCode($val), $key_last ? "" : ",");
}else{
$result .= str_repeat("\t", $depth);
$result .= sprintf("%s => %s%s\n", getPropertyCode($key), getPropertyCode($val), $key_last ? "" : ",");
}
}
}
return sprintf("%s%s]%s", $result, ($is_list ? "" : str_repeat("\t", $depth - 1)), (($depth == 1) || $is_last) ? "" : ",");
}
/**
*
* @param type $property
* @return string
* @throws Exception
*/
function getPropertyCode($property){
if(is_string($property)){
return sprintf('"%s"', $property);
}elseif(is_bool($property)){
return sprintf('%s', $property ? 'TRUE' : 'FALSE');
}elseif(is_numeric($property)){
return $property;
}elseif(is_null($property)){
return 'NULL';
}else{
throw new Exception('getPropertyCode: Undefined property type.');
}
}