-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsrc
26 lines (25 loc) · 852 Bytes
/
src
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
function getCode(array $arr, $depth = 0) {
$depth++;
$result = '[';
foreach($arr as $key => $val) {
if (is_array($val)) {
$result .= sprintf('%s => %s', getPropertyCode($key), getCode($val, $depth));
} else {
$result .= sprintf('%s => %s,', getPropertyCode($key), getPropertyCode($val));
}
}
return sprintf('%s]%s', $result, $depth !== 1 ? ',' : '');
}
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 Exception(sprintf('Undefined property type: %s', $property));
}
}