|
1
|
|
|
<?php |
|
2
|
|
|
namespace Dallgoot\Yaml; |
|
3
|
|
|
|
|
4
|
|
|
use \SplDoublyLinkedList as DLL; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Convert PHP datatypes to a YAML string syntax |
|
8
|
|
|
* |
|
9
|
|
|
* @author Stéphane Rebai <[email protected]> |
|
10
|
|
|
* @license Apache 2.0 |
|
11
|
|
|
* @link https://github.com/dallgoot/yaml |
|
12
|
|
|
*/ |
|
13
|
|
|
class Dumper |
|
14
|
|
|
{ |
|
15
|
|
|
private const INDENT = 2; |
|
16
|
|
|
// private const WIDTH = 120; /// forget this feature for the moment |
|
17
|
|
|
private const OPTIONS = 00000; |
|
18
|
|
|
private const DATE_FORMAT = 'Y-m-d'; |
|
19
|
|
|
|
|
20
|
|
|
/** @var null|\SplDoublyLinkedList */ |
|
21
|
|
|
private static $result; |
|
22
|
|
|
private static $options; |
|
23
|
|
|
//options |
|
24
|
|
|
public const EXPAND_SHORT = 00001; |
|
25
|
|
|
public const SERIALIZE_CUSTOM_OBJECTS = 00010; |
|
26
|
|
|
/** @var int */ |
|
27
|
|
|
public static $floatPrecision = 4; |
|
28
|
|
|
|
|
29
|
|
|
// public function __construct(int $options = null) |
|
30
|
|
|
// { |
|
31
|
|
|
// if (is_int($options)) self::$options = $options; |
|
32
|
|
|
// } |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Returns (as a string) the YAML representation of the $dataType provided |
|
36
|
|
|
* |
|
37
|
|
|
* @param mixed $dataType The data type |
|
38
|
|
|
* @param int|null $options The options |
|
39
|
|
|
* |
|
40
|
|
|
* @throws \Exception datatype cannot be null |
|
41
|
|
|
* |
|
42
|
|
|
* @return string The Yaml string content |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function toString($dataType, int $options = null):string |
|
45
|
|
|
{ |
|
46
|
|
|
if (empty($dataType)) throw new \Exception(self::class.": No content to convert to Yaml"); |
|
47
|
|
|
self::$options = is_int($options) ? $options : self::OPTIONS; |
|
48
|
|
|
if (is_scalar($dataType)) { |
|
49
|
|
|
// TODO: what to woth comments ??? |
|
50
|
|
|
return "--- ".DumperHandlers::dumpScalar($dataType, 0). self::LINEFEED ; |
|
51
|
|
|
} |
|
52
|
|
|
return DumperHandlers::dump($dataType, $options); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Calls and saves the result of Dumper::toString to the file $filePath provided |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $filePath The file path |
|
59
|
|
|
* @param mixed $dataType The data type |
|
60
|
|
|
* @param int|null $options The options |
|
61
|
|
|
* |
|
62
|
|
|
* @throws \Exception datatype cannot be null |
|
63
|
|
|
* |
|
64
|
|
|
* @return bool true = if the file has been correctly saved ( return value from 'file_put_contents') |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function toFile(string $filePath, $dataType, int $options = null):bool |
|
67
|
|
|
{ |
|
68
|
|
|
return !is_bool(file_put_contents($filePath, self::toString($dataType, $options))); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Dump (determine) the string value according to type |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $dataType The data type |
|
75
|
|
|
* @param integer $indent The indent |
|
76
|
|
|
* |
|
77
|
|
|
* @return string The YAML representation of $dataType |
|
78
|
|
|
*/ |
|
79
|
|
|
private static function dump($dataType, int $indent) |
|
80
|
|
|
{ |
|
81
|
|
|
if (is_scalar($dataType)) { |
|
82
|
|
|
if ($dataType === \INF) return '.inf'; |
|
83
|
|
|
if ($dataType === -\INF) return '-.inf'; |
|
84
|
|
|
switch (gettype($dataType)) { |
|
85
|
|
|
case 'boolean': return $dataType ? 'true' : 'false'; |
|
86
|
|
|
case 'float': //fall through |
|
87
|
|
|
case 'double': return is_nan((double) $dataType) ? '.nan' : sprintf('%.'.self::$floatPrecision.'F', $dataType); |
|
88
|
|
|
default: |
|
89
|
|
|
return $dataType; |
|
90
|
|
|
} |
|
91
|
|
|
} elseif (is_object($dataType)) { |
|
92
|
|
|
return self::dumpObject($dataType, $indent); |
|
93
|
|
|
} elseif (is_array($dataType)) { |
|
94
|
|
|
return self::dumpArray($dataType, $indent); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Dumps an YamlObject (YAML document) as a YAML string |
|
100
|
|
|
* |
|
101
|
|
|
* @param YamlObject $obj The object |
|
102
|
|
|
*/ |
|
103
|
|
|
private static function dumpYamlObject(YamlObject $obj) |
|
104
|
|
|
{ |
|
105
|
|
|
if ($obj->hasDocStart() && self::$result instanceof DLL) self::$result->push("---"); |
|
106
|
|
|
// self::dump($obj, 0); |
|
107
|
|
|
if (count($obj) > 0) { |
|
108
|
|
|
self::dumpArray($obj->getArrayCopy(), 0); |
|
109
|
|
|
} else { |
|
110
|
|
|
self::dumpObject($obj, 0); |
|
111
|
|
|
} |
|
112
|
|
|
// self::insertComments($obj->getComment()); |
|
113
|
|
|
//TODO: $references = $obj->getAllReferences(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Add $value to the current YAML representation (self::$result) and cut lines to self::WIDTH if needed |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $value The value |
|
120
|
|
|
* @param int $indent The indent |
|
121
|
|
|
*/ |
|
122
|
|
|
private static function add(string $value, int $indent) |
|
123
|
|
|
{ |
|
124
|
|
|
$newVal = str_repeat(" ", $indent).$value; |
|
125
|
|
|
foreach (str_split($newVal, self::WIDTH) as $chunks) { |
|
126
|
|
|
self::$result->push($chunks); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Insert comments from YamlObject at specific lines OR add to the value currently at the line |
|
132
|
|
|
* |
|
133
|
|
|
* @param array $commentsArray The comments array |
|
134
|
|
|
*/ |
|
135
|
|
|
private static function insertComments(array $commentsArray) |
|
136
|
|
|
{ |
|
137
|
|
|
foreach ($commentsArray as $lineNb => $comment) { |
|
138
|
|
|
self::$result->add($lineNb, $comment); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
|
|
|
|
|
141
|
|
|
|