@@ -39,13 +39,18 @@ public function __construct(int $indentation = 4)
39
39
/**
40
40
* Dumps a PHP value to YAML.
41
41
*
42
- * @param mixed $input The PHP value
43
- * @param int $inline The level where you switch to inline YAML
44
- * @param int $indent The level of indentation (used internally)
45
- * @param int-mask-of<Yaml::DUMP_*> $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
42
+ * @param mixed $input The PHP value
43
+ * @param int $inline The level where you switch to inline YAML
44
+ * @param int $indent The level of indentation (used internally)
45
+ * @param int-mask-of<Yaml::DUMP_*> $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
46
+ * @param int $nestingLevel The level of nesting (used internally)
46
47
*/
47
- public function dump (mixed $ input , int $ inline = 0 , int $ indent = 0 , int $ flags = 0 ): string
48
+ public function dump (mixed $ input , int $ inline = 0 , int $ indent = 0 , int $ flags = 0 , int $ nestingLevel = 0 ): string
48
49
{
50
+ if ($ flags & Yaml::DUMP_NULL_AS_EMPTY && $ flags & Yaml::DUMP_NULL_AS_TILDE ) {
51
+ throw new \InvalidArgumentException ('The Yaml::DUMP_NULL_AS_EMPTY and Yaml::DUMP_NULL_AS_TILDE flags cannot be used together. ' );
52
+ }
53
+
49
54
$ output = '' ;
50
55
$ prefix = $ indent ? str_repeat (' ' , $ indent ) : '' ;
51
56
$ dumpObjectAsInlineMap = true ;
@@ -55,9 +60,9 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
55
60
}
56
61
57
62
if ($ inline <= 0 || (!\is_array ($ input ) && !$ input instanceof TaggedValue && $ dumpObjectAsInlineMap ) || !$ input ) {
58
- $ output .= $ prefix .Inline::dump ($ input , $ flags );
63
+ $ output .= $ prefix .Inline::dump ($ input , $ flags, 0 === $ nestingLevel );
59
64
} elseif ($ input instanceof TaggedValue) {
60
- $ output .= $ this ->dumpTaggedValue ($ input , $ inline , $ indent , $ flags , $ prefix );
65
+ $ output .= $ this ->dumpTaggedValue ($ input , $ inline , $ indent , $ flags , $ prefix, $ nestingLevel );
61
66
} else {
62
67
$ dumpAsMap = Inline::isHash ($ input );
63
68
@@ -109,10 +114,10 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
109
114
}
110
115
111
116
if ($ inline - 1 <= 0 || null === $ value ->getValue () || \is_scalar ($ value ->getValue ())) {
112
- $ output .= ' ' .$ this ->dump ($ value ->getValue (), $ inline - 1 , 0 , $ flags )."\n" ;
117
+ $ output .= ' ' .$ this ->dump ($ value ->getValue (), $ inline - 1 , 0 , $ flags, $ nestingLevel + 1 )."\n" ;
113
118
} else {
114
119
$ output .= "\n" ;
115
- $ output .= $ this ->dump ($ value ->getValue (), $ inline - 1 , $ dumpAsMap ? $ indent + $ this ->indentation : $ indent + 2 , $ flags );
120
+ $ output .= $ this ->dump ($ value ->getValue (), $ inline - 1 , $ dumpAsMap ? $ indent + $ this ->indentation : $ indent + 2 , $ flags, $ nestingLevel + 1 );
116
121
}
117
122
118
123
continue ;
@@ -130,15 +135,15 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
130
135
$ prefix ,
131
136
$ dumpAsMap ? Inline::dump ($ key , $ flags ).': ' : '- ' ,
132
137
$ willBeInlined ? ' ' : "\n" ,
133
- $ this ->dump ($ value , $ inline - 1 , $ willBeInlined ? 0 : $ indent + $ this ->indentation , $ flags )
138
+ $ this ->dump ($ value , $ inline - 1 , $ willBeInlined ? 0 : $ indent + $ this ->indentation , $ flags, $ nestingLevel + 1 )
134
139
).($ willBeInlined ? "\n" : '' );
135
140
}
136
141
}
137
142
138
143
return $ output ;
139
144
}
140
145
141
- private function dumpTaggedValue (TaggedValue $ value , int $ inline , int $ indent , int $ flags , string $ prefix ): string
146
+ private function dumpTaggedValue (TaggedValue $ value , int $ inline , int $ indent , int $ flags , string $ prefix, int $ nestingLevel ): string
142
147
{
143
148
$ output = \sprintf ('%s!%s ' , $ prefix ? $ prefix .' ' : '' , $ value ->getTag ());
144
149
@@ -154,10 +159,10 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i
154
159
}
155
160
156
161
if ($ inline - 1 <= 0 || null === $ value ->getValue () || \is_scalar ($ value ->getValue ())) {
157
- return $ output .' ' .$ this ->dump ($ value ->getValue (), $ inline - 1 , 0 , $ flags )."\n" ;
162
+ return $ output .' ' .$ this ->dump ($ value ->getValue (), $ inline - 1 , 0 , $ flags, $ nestingLevel + 1 )."\n" ;
158
163
}
159
164
160
- return $ output ."\n" .$ this ->dump ($ value ->getValue (), $ inline - 1 , $ indent , $ flags );
165
+ return $ output ."\n" .$ this ->dump ($ value ->getValue (), $ inline - 1 , $ indent , $ flags, $ nestingLevel + 1 );
161
166
}
162
167
163
168
private function getBlockIndentationIndicator (string $ value ): string
0 commit comments