1
|
|
|
<?php |
2
|
|
|
namespace Dallgoot\Yaml; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Convert PHP datatypes to a YAML string syntax |
7
|
|
|
* |
8
|
|
|
* @author Stéphane Rebai <[email protected]> |
9
|
|
|
* @license Apache 2.0 |
10
|
|
|
* @link https://github.com/dallgoot/yaml |
11
|
|
|
*/ |
12
|
|
|
class Dumper |
13
|
|
|
{ |
14
|
|
|
private const LINEFEED = "\n"; |
15
|
|
|
public const INDENT = 2; |
16
|
|
|
// private const WIDTH = 120; //TODO forget this feature for the moment |
17
|
|
|
private const OPTIONS = 0b00000; |
18
|
|
|
public const DATE_FORMAT = 'Y-m-d'; |
19
|
|
|
|
20
|
|
|
public $options; |
21
|
|
|
//options |
22
|
|
|
public const EXPAND_SHORT = 0b00001; |
23
|
|
|
public const SERIALIZE_CUSTOM_OBJECTS = 0b00010; |
24
|
|
|
/** @var int */ |
25
|
|
|
public $floatPrecision = 4; |
26
|
|
|
/** @var bool */ |
27
|
|
|
private $multipleDocs = false; |
28
|
|
|
/** @var bool */ |
29
|
|
|
public $_compactMode = false; |
30
|
|
|
/** @var null|DumperHandlers */ |
31
|
|
|
private $handler; |
32
|
|
|
|
33
|
|
|
|
34
|
6 |
|
public function __construct($options=null) |
35
|
|
|
{ |
36
|
6 |
|
$this->options = is_int($options) ? $options : self::OPTIONS; |
37
|
6 |
|
$this->handler = new DumperHandlers($this); |
38
|
6 |
|
} |
39
|
|
|
/** |
40
|
|
|
* Returns (as a string) the YAML representation of the $dataType provided |
41
|
|
|
* |
42
|
|
|
* @param mixed $dataType The data type |
43
|
|
|
* @param int|null $options The options |
44
|
|
|
* |
45
|
|
|
* @throws \Exception datatype cannot be null |
46
|
|
|
* |
47
|
|
|
* @return string The Yaml string content |
48
|
|
|
*/ |
49
|
2 |
|
public function toString($dataType, int $options = null):string |
|
|
|
|
50
|
|
|
{ |
51
|
2 |
|
if (empty($dataType)) throw new \Exception(self::class.": No content to convert to Yaml"); |
52
|
2 |
|
if (is_scalar($dataType)) { |
53
|
1 |
|
return "--- ".$this->handler->dumpScalar($dataType). self::LINEFEED ; |
|
|
|
|
54
|
|
|
} |
55
|
2 |
|
return $this->dump($dataType, 0); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Calls and saves the result of Dumper::toString to the file $filePath provided |
60
|
|
|
* |
61
|
|
|
* @param string $filePath The file path |
62
|
|
|
* @param mixed $dataType The data type |
63
|
|
|
* @param int|null $options The options |
64
|
|
|
* |
65
|
|
|
* @throws \Exception datatype cannot be null |
66
|
|
|
* |
67
|
|
|
* @return bool true = if the file has been correctly saved ( return value from 'file_put_contents') |
68
|
|
|
*/ |
69
|
1 |
|
public function toFile(string $filePath, $dataType, int $options = null):bool |
70
|
|
|
{ |
71
|
1 |
|
return !is_bool(file_put_contents($filePath, $this->toString($dataType, $options))); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
|
76
|
5 |
|
public function dump($dataType, int $indent):string |
77
|
|
|
{ |
78
|
5 |
|
if (is_null($dataType)) { |
79
|
1 |
|
return ''; |
80
|
5 |
|
} elseif (is_resource($dataType)) { |
81
|
1 |
|
return get_resource_type($dataType); |
82
|
5 |
|
} elseif (is_scalar($dataType)) { |
83
|
5 |
|
return $this->handler->dumpScalar($dataType); |
84
|
|
|
} else { |
85
|
3 |
|
return $this->handler->dumpCompound($dataType, $indent); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
public function dumpMultiDoc($arrayOfYamlObject) |
91
|
|
|
{ |
92
|
|
|
foreach ($arrayOfYamlObject as $key => $yamlObject) { |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Dumps an yaml object to a YAML string |
99
|
|
|
* |
100
|
|
|
* @param YamlObject $obj The object |
101
|
|
|
* |
102
|
|
|
* @return string YAML formatted string |
103
|
|
|
* @todo export comment from YamlObject |
104
|
|
|
*/ |
105
|
1 |
|
public function dumpYamlObject(YamlObject $obj):string |
106
|
|
|
{ |
107
|
1 |
|
if ($this->multipleDocs || $obj->hasDocStart() || $obj->isTagged()) { |
108
|
|
|
$this->multipleDocs = true; |
109
|
|
|
// && $this->$result instanceof DLL) $this->$result->push("---"); |
110
|
|
|
} |
111
|
|
|
// $this->insertComments($obj->getComment()); |
112
|
1 |
|
if (count($obj) > 0) { |
113
|
1 |
|
return $this->iteratorToString($obj, '-', "\n", 0); |
114
|
|
|
} else { |
115
|
1 |
|
return $this->iteratorToString(new \ArrayIterator(get_object_vars($obj)), '%s:', "\n", 0); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
5 |
|
public function iteratorToString(\Iterator $iterable, |
121
|
|
|
string $keyMask, string $itemSeparator, int $indent):string |
122
|
|
|
{ |
123
|
5 |
|
$pairs = []; |
124
|
5 |
|
$valueIndent = $indent + self::INDENT; |
125
|
5 |
|
foreach ($iterable as $key => $value) { |
126
|
5 |
|
$separator = "\n"; |
127
|
5 |
|
if (is_scalar($value) || $value instanceof Compact || $value instanceof \DateTime) { |
128
|
5 |
|
$separator = ' '; |
129
|
5 |
|
$valueIndent = 0; |
130
|
|
|
} |
131
|
5 |
|
if ($this->_compactMode) { |
132
|
|
|
$pairs[] = sprintf($keyMask, $key).$this->dump($value, $valueIndent); |
133
|
|
|
} else { |
134
|
5 |
|
$pairs[] = str_repeat(' ', $indent).sprintf($keyMask, $key).$separator.$this->dump($value, $valueIndent); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
// $processItem = function () |
139
|
5 |
|
return implode($itemSeparator, $pairs); |
140
|
|
|
// return implode($itemSeparator, array_map(callback, arr1)); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.