@@ -33,6 +33,18 @@ class Inline
33
33
private static $ objectForMap = false ;
34
34
private static $ constantSupport = false ;
35
35
36
+ public static function initialize (int $ flags , int $ parsedLineNumber = null )
37
+ {
38
+ self ::$ exceptionOnInvalidType = (bool ) (Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE & $ flags );
39
+ self ::$ objectSupport = (bool ) (Yaml::PARSE_OBJECT & $ flags );
40
+ self ::$ objectForMap = (bool ) (Yaml::PARSE_OBJECT_FOR_MAP & $ flags );
41
+ self ::$ constantSupport = (bool ) (Yaml::PARSE_CONSTANT & $ flags );
42
+
43
+ if (null !== $ parsedLineNumber ) {
44
+ self ::$ parsedLineNumber = $ parsedLineNumber ;
45
+ }
46
+ }
47
+
36
48
/**
37
49
* Converts a YAML string to a PHP value.
38
50
*
@@ -44,12 +56,9 @@ class Inline
44
56
*
45
57
* @throws ParseException
46
58
*/
47
- public static function parse ($ value, $ flags = 0 , $ references = array ())
59
+ public static function parse (string $ value = null , int $ flags = 0 , array $ references = array ())
48
60
{
49
- self ::$ exceptionOnInvalidType = (bool ) (Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE & $ flags );
50
- self ::$ objectSupport = (bool ) (Yaml::PARSE_OBJECT & $ flags );
51
- self ::$ objectForMap = (bool ) (Yaml::PARSE_OBJECT_FOR_MAP & $ flags );
52
- self ::$ constantSupport = (bool ) (Yaml::PARSE_CONSTANT & $ flags );
61
+ self ::initialize ($ flags );
53
62
54
63
$ value = trim ($ value );
55
64
@@ -103,7 +112,7 @@ public static function parse($value, $flags = 0, $references = array())
103
112
*
104
113
* @throws DumpException When trying to dump PHP resource
105
114
*/
106
- public static function dump ($ value , $ flags = 0 )
115
+ public static function dump ($ value , int $ flags = 0 ): string
107
116
{
108
117
switch (true ) {
109
118
case is_resource ($ value ):
@@ -124,7 +133,13 @@ public static function dump($value, $flags = 0)
124
133
}
125
134
126
135
if (Yaml::DUMP_OBJECT_AS_MAP & $ flags && ($ value instanceof \stdClass || $ value instanceof \ArrayObject)) {
127
- return self ::dumpArray ($ value , $ flags & ~Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE );
136
+ $ output = array ();
137
+
138
+ foreach ($ value as $ key => $ val ) {
139
+ $ output [] = sprintf ('%s: %s ' , self ::dump ($ key , $ flags ), self ::dump ($ val , $ flags ));
140
+ }
141
+
142
+ return sprintf ('{ %s } ' , implode (', ' , $ output ));
128
143
}
129
144
130
145
if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $ flags ) {
@@ -182,13 +197,11 @@ public static function dump($value, $flags = 0)
182
197
/**
183
198
* Check if given array is hash or just normal indexed array.
184
199
*
185
- * @internal
186
- *
187
200
* @param array|\ArrayObject|\stdClass $value The PHP array or array-like object to check
188
201
*
189
202
* @return bool true if value is hash array, false otherwise
190
203
*/
191
- public static function isHash ($ value )
204
+ public static function isHash ($ value ): bool
192
205
{
193
206
if ($ value instanceof \stdClass || $ value instanceof \ArrayObject) {
194
207
return true ;
@@ -213,7 +226,7 @@ public static function isHash($value)
213
226
*
214
227
* @return string The YAML string representing the PHP array
215
228
*/
216
- private static function dumpArray ($ value , $ flags )
229
+ private static function dumpArray (array $ value , int $ flags ): string
217
230
{
218
231
// array
219
232
if (($ value || Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE & $ flags ) && !self ::isHash ($ value )) {
@@ -244,13 +257,11 @@ private static function dumpArray($value, $flags)
244
257
* @param bool $evaluate
245
258
* @param array $references
246
259
*
247
- * @return string
260
+ * @return mixed
248
261
*
249
262
* @throws ParseException When malformed inline YAML string is parsed
250
- *
251
- * @internal
252
263
*/
253
- public static function parseScalar ($ scalar , $ flags = 0 , $ delimiters = null , &$ i = 0 , $ evaluate = true , $ references = array ())
264
+ public static function parseScalar (string $ scalar , int $ flags = 0 , array $ delimiters = null , int &$ i = 0 , bool $ evaluate = true , array $ references = array ())
254
265
{
255
266
if (in_array ($ scalar [$ i ], array ('" ' , "' " ))) {
256
267
// quoted scalar
@@ -302,7 +313,7 @@ public static function parseScalar($scalar, $flags = 0, $delimiters = null, &$i
302
313
*
303
314
* @throws ParseException When malformed inline YAML string is parsed
304
315
*/
305
- private static function parseQuotedScalar ($ scalar , &$ i )
316
+ private static function parseQuotedScalar (string $ scalar , int &$ i ): string
306
317
{
307
318
if (!Parser::preg_match ('/ ' .self ::REGEX_QUOTED_STRING .'/Au ' , substr ($ scalar , $ i ), $ match )) {
308
319
throw new ParseException (sprintf ('Malformed inline YAML string: %s. ' , substr ($ scalar , $ i )));
@@ -334,7 +345,7 @@ private static function parseQuotedScalar($scalar, &$i)
334
345
*
335
346
* @throws ParseException When malformed inline YAML string is parsed
336
347
*/
337
- private static function parseSequence ($ sequence , $ flags , &$ i = 0 , $ references = array ())
348
+ private static function parseSequence (string $ sequence , int $ flags , int &$ i = 0 , array $ references = array ()): array
338
349
{
339
350
$ output = array ();
340
351
$ len = strlen ($ sequence );
@@ -403,7 +414,7 @@ private static function parseSequence($sequence, $flags, &$i = 0, $references =
403
414
*
404
415
* @throws ParseException When malformed inline YAML string is parsed
405
416
*/
406
- private static function parseMapping ($ mapping , $ flags , &$ i = 0 , $ references = array ())
417
+ private static function parseMapping (string $ mapping , int $ flags , int &$ i = 0 , array $ references = array ())
407
418
{
408
419
$ output = array ();
409
420
$ len = strlen ($ mapping );
@@ -515,7 +526,7 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
515
526
*
516
527
* @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
517
528
*/
518
- private static function evaluateScalar ($ scalar , $ flags , $ references = array ())
529
+ private static function evaluateScalar (string $ scalar , int $ flags , array $ references = array ())
519
530
{
520
531
$ scalar = trim ($ scalar );
521
532
$ scalarLower = strtolower ($ scalar );
@@ -638,10 +649,10 @@ private static function evaluateScalar($scalar, $flags, $references = array())
638
649
*
639
650
* @return null|string
640
651
*/
641
- private static function parseTag ($ value , &$ i , $ flags )
652
+ private static function parseTag (string $ value , int &$ i , int $ flags ): ? string
642
653
{
643
654
if ('! ' !== $ value [$ i ]) {
644
- return ;
655
+ return null ;
645
656
}
646
657
647
658
$ tagLength = strcspn ($ value , " \t\n[]{}, " , $ i + 1 );
@@ -653,7 +664,7 @@ private static function parseTag($value, &$i, $flags)
653
664
// Is followed by a scalar and is a built-in tag
654
665
if ($ tag && (!isset ($ value [$ nextOffset ]) || !in_array ($ value [$ nextOffset ], array ('[ ' , '{ ' ), true )) && ('! ' === $ tag [0 ] || 'str ' === $ tag || 'php/const ' === $ tag || 'php/object ' === $ tag )) {
655
666
// Manage in {@link self::evaluateScalar()}
656
- return ;
667
+ return null ;
657
668
}
658
669
659
670
$ i = $ nextOffset ;
@@ -674,10 +685,8 @@ private static function parseTag($value, &$i, $flags)
674
685
* @param string $scalar
675
686
*
676
687
* @return string
677
- *
678
- * @internal
679
688
*/
680
- public static function evaluateBinaryScalar ($ scalar )
689
+ public static function evaluateBinaryScalar (string $ scalar ): string
681
690
{
682
691
$ parsedBinaryData = self ::parseScalar (preg_replace ('/\s/ ' , '' , $ scalar ));
683
692
@@ -692,7 +701,7 @@ public static function evaluateBinaryScalar($scalar)
692
701
return base64_decode ($ parsedBinaryData , true );
693
702
}
694
703
695
- private static function isBinaryString ($ value )
704
+ private static function isBinaryString (string $ value )
696
705
{
697
706
return !preg_match ('//u ' , $ value ) || preg_match ('/[^\x00\x07-\x0d\x1B\x20-\xff]/ ' , $ value );
698
707
}
@@ -704,7 +713,7 @@ private static function isBinaryString($value)
704
713
*
705
714
* @see http://www.yaml.org/spec/1.2/spec.html#id2761573
706
715
*/
707
- private static function getTimestampRegex ()
716
+ private static function getTimestampRegex (): string
708
717
{
709
718
return <<<EOF
710
719
~^
@@ -727,7 +736,7 @@ private static function getTimestampRegex()
727
736
*
728
737
* @return string
729
738
*/
730
- private static function getHexRegex ()
739
+ private static function getHexRegex (): string
731
740
{
732
741
return '~^0x[0-9a-f_]++$~i ' ;
733
742
}
0 commit comments