1
|
|
|
<?php |
2
|
|
|
namespace Dallgoot\Yaml; |
|
|
|
|
3
|
|
|
|
4
|
|
|
use Dallgoot\Yaml\{Types as T, YamlObject, Tag, Compact}; |
|
|
|
|
5
|
|
|
use \SplDoublyLinkedList as DLL; |
6
|
|
|
|
7
|
|
|
/** |
|
|
|
|
8
|
|
|
* |
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
class Dumper //extends AnotherClass |
11
|
|
|
{ |
12
|
|
|
private const indent = 2; |
|
|
|
|
13
|
|
|
private const width = 120; |
|
|
|
|
14
|
|
|
private const options = 00000; |
|
|
|
|
15
|
|
|
|
16
|
|
|
private static $result; |
|
|
|
|
17
|
|
|
private static $options; |
|
|
|
|
18
|
|
|
//options |
19
|
|
|
public const EXPAND_SHORT = 00001; |
20
|
|
|
public const SERIALIZE_CUSTOM_OBJECTS = 00010; |
21
|
|
|
|
22
|
|
|
public function __construct(int $options = null) |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
if (is_int($options)) self::$options = $options; |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function toString($dataType, int $options):string |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml", 1); |
|
|
|
|
30
|
|
|
self::$options = is_int($options) ? $options : self::$options; |
|
|
|
|
31
|
|
|
self::$result = new DLL; |
32
|
|
|
self::$result->setIteratorMode(DLL::IT_MODE_FIFO | DLL::IT_MODE_DELETE); |
33
|
|
|
if ($dataType instanceof YamlObject) { |
34
|
|
|
self::dumpYamlObject($dataType); |
35
|
|
|
} elseif (is_array($dataType) && $dataType[0] instanceof YamlObject) { |
36
|
|
|
array_map([self::class, 'dumpYamlObject'], $dataType); |
37
|
|
|
} else { |
38
|
|
|
self::dump($dataType, 0); |
39
|
|
|
} |
40
|
|
|
$out = ''; |
41
|
|
|
foreach (self::$result as $value) { |
42
|
|
|
$out .= $value."\n"; |
43
|
|
|
} |
44
|
|
|
self::$result = null; |
45
|
|
|
return $out; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public static function toFile(string $filePath, $dataType, int $options):bool |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
return !is_bool(file_put_contents($filePath, self::toString($dataType, $options))); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private static function dump($dataType, int $indent) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
if (is_scalar($dataType)) { |
56
|
|
|
switch (gettype($dataType)) { |
57
|
|
|
case 'boolean': return $dataType ? 'true' : 'false';break; |
|
|
|
|
58
|
|
|
case 'float': if (is_infinite($dataType)) return $dataType > 0 ? '.inf' : '-.inf'; |
|
|
|
|
59
|
|
|
case 'double': if (is_nan($dataType)) return '.nan'; |
|
|
|
|
60
|
|
|
default: |
|
|
|
|
61
|
|
|
return $dataType; |
62
|
|
|
} |
63
|
|
|
} elseif (is_object($dataType)) { |
64
|
|
|
self::dumpObject($dataType, $indent); |
|
|
|
|
65
|
|
|
} elseif (is_array($dataType)) { |
66
|
|
|
self::dumpSequence($dataType, $indent); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private static function dumpYamlObject(YamlObject $dataType) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
self::$result->push("---"); |
73
|
|
|
return self::dump($dataType, 0); |
|
|
|
|
74
|
|
|
// self::insertComments($dataType->getComment()); |
75
|
|
|
//TODO: $references = $dataType->getAllReferences(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private static function add($value, $indent) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$newVal = str_repeat(" ", $indent).$value; |
81
|
|
|
foreach (str_split($newVal, self::width) as $chunks) { |
82
|
|
|
self::$result->push($chunks); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private static function dumpSequence(array $array, int $indent):void |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$refKeys = range(0, count($array)); |
89
|
|
|
foreach ($array as $key => $item) { |
90
|
|
|
$lineStart = current($refKeys) === $key ? "- " : "- $key: "; |
91
|
|
|
if (is_scalar($item)) { |
92
|
|
|
self::add($lineStart.$item, $indent ); |
|
|
|
|
93
|
|
|
} else { |
94
|
|
|
self::add($lineStart, $indent ); |
|
|
|
|
95
|
|
|
self::dump($item, $indent + self::indent); |
96
|
|
|
} |
97
|
|
|
next($refKeys); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private static function insertComments(array $commentsArray):void |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
foreach ($commentsArray as $lineNb => $comment) { |
104
|
|
|
self::$result->add($lineNb, $comment); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function dumpObject(object $object, int $indent) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
if ($dataType instanceof Tag) { |
|
|
|
|
111
|
|
|
if (is_scalar($dataType->value)) { |
112
|
|
|
return "!".$dataType->tagName.' '.$dataType->value; |
113
|
|
|
} else{ |
|
|
|
|
114
|
|
|
yield "!".$dataType->tagName; |
115
|
|
|
self::dump($dataType->value, $indent + self::indent); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
if ($dataType instanceof Compact) {//TODO |
119
|
|
|
self::dumpCompact($dataType, $indent); |
120
|
|
|
} |
121
|
|
|
if ($dataType instanceof \DateTime) { |
122
|
|
|
# code... |
|
|
|
|
123
|
|
|
} |
124
|
|
|
$propList = get_object_vars($dataType); |
125
|
|
|
foreach ($propList as $property => $value) { |
126
|
|
|
if (is_scalar($value)) { |
127
|
|
|
self::add("$property: ".$value, $indent); |
128
|
|
|
} else { |
129
|
|
|
self::add("$property: ", $indent); |
130
|
|
|
self::dump($value, $indent + self::indent); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public static function dumpCompact(Compact $object, int $indent) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
// if (empty($object)) return "{}"; |
138
|
|
|
// if (empty($object)) return "[]"; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|