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