@@ -41,6 +41,15 @@ public function __construct(private int $indentation = 4)
4141 * @param int-mask-of<Yaml::DUMP_*> $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
4242 */
4343 public function dump (mixed $ input , int $ inline = 0 , int $ indent = 0 , int $ flags = 0 ): string
44+ {
45+ if ($ flags & Yaml::DUMP_NULL_AS_EMPTY && $ flags & Yaml::DUMP_NULL_AS_TILDE ) {
46+ throw new \InvalidArgumentException ('The Yaml::DUMP_NULL_AS_EMPTY and Yaml::DUMP_NULL_AS_TILDE flags cannot be used together. ' );
47+ }
48+
49+ return $ this ->doDump ($ input , $ inline , $ indent , $ flags );
50+ }
51+
52+ private function doDump (mixed $ input , int $ inline = 0 , int $ indent = 0 , int $ flags = 0 , int $ nestingLevel = 0 ): string
4453 {
4554 $ output = '' ;
4655 $ prefix = $ indent ? str_repeat (' ' , $ indent ) : '' ;
@@ -51,9 +60,9 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
5160 }
5261
5362 if ($ inline <= 0 || (!\is_array ($ input ) && !$ input instanceof TaggedValue && $ dumpObjectAsInlineMap ) || !$ input ) {
54- $ output .= $ prefix .Inline::dump ($ input , $ flags );
63+ $ output .= $ prefix .Inline::dump ($ input , $ flags, 0 === $ nestingLevel );
5564 } elseif ($ input instanceof TaggedValue) {
56- $ output .= $ this ->dumpTaggedValue ($ input , $ inline , $ indent , $ flags , $ prefix );
65+ $ output .= $ this ->dumpTaggedValue ($ input , $ inline , $ indent , $ flags , $ prefix, $ nestingLevel );
5766 } else {
5867 $ dumpAsMap = Inline::isHash ($ input );
5968
@@ -105,10 +114,10 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
105114 }
106115
107116 if ($ inline - 1 <= 0 || null === $ value ->getValue () || \is_scalar ($ value ->getValue ())) {
108- $ output .= ' ' .$ this ->dump ($ value ->getValue (), $ inline - 1 , 0 , $ flags )."\n" ;
117+ $ output .= ' ' .$ this ->doDump ($ value ->getValue (), $ inline - 1 , 0 , $ flags, $ nestingLevel + 1 )."\n" ;
109118 } else {
110119 $ output .= "\n" ;
111- $ output .= $ this ->dump ($ value ->getValue (), $ inline - 1 , $ dumpAsMap ? $ indent + $ this ->indentation : $ indent + 2 , $ flags );
120+ $ output .= $ this ->doDump ($ value ->getValue (), $ inline - 1 , $ dumpAsMap ? $ indent + $ this ->indentation : $ indent + 2 , $ flags, $ nestingLevel + 1 );
112121 }
113122
114123 continue ;
@@ -126,15 +135,15 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
126135 $ prefix ,
127136 $ dumpAsMap ? Inline::dump ($ key , $ flags ).': ' : '- ' ,
128137 $ willBeInlined ? ' ' : "\n" ,
129- $ this ->dump ($ value , $ inline - 1 , $ willBeInlined ? 0 : $ indent + $ this ->indentation , $ flags )
138+ $ this ->doDump ($ value , $ inline - 1 , $ willBeInlined ? 0 : $ indent + $ this ->indentation , $ flags, $ nestingLevel + 1 )
130139 ).($ willBeInlined ? "\n" : '' );
131140 }
132141 }
133142
134143 return $ output ;
135144 }
136145
137- 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
138147 {
139148 $ output = \sprintf ('%s!%s ' , $ prefix ? $ prefix .' ' : '' , $ value ->getTag ());
140149
@@ -150,10 +159,10 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i
150159 }
151160
152161 if ($ inline - 1 <= 0 || null === $ value ->getValue () || \is_scalar ($ value ->getValue ())) {
153- return $ output .' ' .$ this ->dump ($ value ->getValue (), $ inline - 1 , 0 , $ flags )."\n" ;
162+ return $ output .' ' .$ this ->doDump ($ value ->getValue (), $ inline - 1 , 0 , $ flags, $ nestingLevel + 1 )."\n" ;
154163 }
155164
156- return $ output ."\n" .$ this ->dump ($ value ->getValue (), $ inline - 1 , $ indent , $ flags );
165+ return $ output ."\n" .$ this ->doDump ($ value ->getValue (), $ inline - 1 , $ indent , $ flags, $ nestingLevel + 1 );
157166 }
158167
159168 private function getBlockIndentationIndicator (string $ value ): string
0 commit comments