1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Dallgoot\Yaml; |
4
|
|
|
|
5
|
|
|
use Dallgoot\Yaml\{Yaml as Y, Regex as R}; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* TODO |
9
|
|
|
* |
10
|
|
|
* @author Stéphane Rebai <[email protected]> |
11
|
|
|
* @license Apache 2.0 |
12
|
|
|
* @link TODO : url to specific online doc |
13
|
|
|
*/ |
|
|
|
|
14
|
|
|
final class Node2PHP |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
|
|
|
|
18
|
|
|
* Returns the correct PHP datatype for the value of the current Node |
19
|
|
|
* |
20
|
|
|
* @return mixed The value as PHP type : scalar, array or Compact, DateTime |
21
|
|
|
*/ |
22
|
|
|
public static function get(Node $n) |
23
|
|
|
{ |
24
|
|
|
if (is_null($n->value)) return null; |
25
|
|
|
if ($n->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar($n->value); |
|
|
|
|
26
|
|
|
if ($n->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) |
27
|
|
|
return self::getCompact(substr($n->value, 1, -1), $n->type); |
|
|
|
|
28
|
|
|
$expected = [Y::JSON => json_decode($n->value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR), |
|
|
|
|
29
|
|
|
Y::QUOTED => trim($n->value, "\"'"), |
|
|
|
|
30
|
|
|
Y::RAW => strval($n->value)]; |
31
|
|
|
return $expected[$n->type] ?? null; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns the correct PHP type according to the string value |
36
|
|
|
* |
37
|
|
|
* @param string $v a string value |
38
|
|
|
* |
39
|
|
|
* @return mixed The value with appropriate PHP type |
40
|
|
|
*/ |
41
|
|
|
private static function getScalar(string $v) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
if (R::isDate($v)) return date_create($v); |
44
|
|
|
if (R::isNumber($v)) return self::getNumber($v); |
45
|
|
|
$types = ['yes' => true, |
46
|
|
|
'no' => false, |
47
|
|
|
'true' => true, |
48
|
|
|
'false' => false, |
49
|
|
|
'null' => null, |
50
|
|
|
'.inf' => INF, |
51
|
|
|
'-.inf' => -INF, |
52
|
|
|
'.nan' => NAN |
53
|
|
|
]; |
54
|
|
|
return array_key_exists(strtolower($v), $types) ? $types[strtolower($v)] : strval($v); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the correct PHP type according to the string value |
59
|
|
|
* |
60
|
|
|
* @param string $v a string value |
61
|
|
|
* |
62
|
|
|
* @return int|float The scalar value with appropriate PHP type |
63
|
|
|
*/ |
64
|
|
|
private static function getNumber(string $v) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
if (preg_match("/^(0o\d+)$/i", $v)) return intval(base_convert($v, 8, 10)); |
67
|
|
|
if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10)); |
68
|
|
|
return is_bool(strpos($v, '.')) ? intval($v) : floatval($v); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns a Compact object representing the inline object/array provided as string |
73
|
|
|
* |
74
|
|
|
* @param string $mappingOrSeqString The mapping or sequence string |
75
|
|
|
* @param integer $type The type |
76
|
|
|
* |
77
|
|
|
* @return Compact The compact object equivalent to $mappingOrString |
78
|
|
|
*/ |
79
|
|
|
private static function getCompact(string $mappingOrSeqString, int $type):object |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
//TODO : this should handle references present inside the string |
82
|
|
|
$out = new Compact(); |
83
|
|
|
if ($type === Y::COMPACT_SEQUENCE) { |
84
|
|
|
$f = function ($e) { return self::getScalar(trim($e));}; |
|
|
|
|
85
|
|
|
//TODO : that's not robust enough, improve it |
86
|
|
|
foreach (array_map($f, explode(",", $mappingOrSeqString)) as $key => $value) { |
87
|
|
|
$out[$key] = $value; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
if ($type === Y::COMPACT_MAPPING) { |
91
|
|
|
//TODO : that's not robust enough, improve it |
92
|
|
|
foreach (explode(',', $mappingOrSeqString) as $value) { |
93
|
|
|
list($keyName, $keyValue) = explode(':', $value); |
94
|
|
|
$out->{trim($keyName, '\'" ')} = self::getScalar(trim($keyValue)); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
return $out; |
98
|
|
|
} |
99
|
|
|
} |