@@ -41,6 +41,15 @@ public function __construct(private int $indentation = 4)
41
41
* @param int-mask-of<Yaml::DUMP_*> $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
42
42
*/
43
43
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
44
53
{
45
54
$ output = '' ;
46
55
$ prefix = $ indent ? str_repeat (' ' , $ indent ) : '' ;
@@ -51,9 +60,9 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
51
60
}
52
61
53
62
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 );
55
64
} elseif ($ input instanceof TaggedValue) {
56
- $ output .= $ this ->dumpTaggedValue ($ input , $ inline , $ indent , $ flags , $ prefix );
65
+ $ output .= $ this ->dumpTaggedValue ($ input , $ inline , $ indent , $ flags , $ prefix, $ nestingLevel );
57
66
} else {
58
67
$ dumpAsMap = Inline::isHash ($ input );
59
68
@@ -105,10 +114,10 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
105
114
}
106
115
107
116
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" ;
109
118
} else {
110
119
$ 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 );
112
121
}
113
122
114
123
continue ;
@@ -126,15 +135,15 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
126
135
$ prefix ,
127
136
$ dumpAsMap ? Inline::dump ($ key , $ flags ).': ' : '- ' ,
128
137
$ 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 )
130
139
).($ willBeInlined ? "\n" : '' );
131
140
}
132
141
}
133
142
134
143
return $ output ;
135
144
}
136
145
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
138
147
{
139
148
$ output = \sprintf ('%s!%s ' , $ prefix ? $ prefix .' ' : '' , $ value ->getTag ());
140
149
@@ -150,10 +159,10 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i
150
159
}
151
160
152
161
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" ;
154
163
}
155
164
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 );
157
166
}
158
167
159
168
private function getBlockIndentationIndicator (string $ value ): string
0 commit comments