8000 [Yaml] Cleanups for object maps · symfony/symfony@0cee604 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0cee604

Browse files
[Yaml] Cleanups for object maps
1 parent e2d5468 commit 0cee604

File tree

3 files changed

+190
-226
lines changed

3 files changed

+190
-226
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Inline
2525

2626
private static $exceptionOnInvalidType = false;
2727
private static $objectSupport = false;
28+
private static $objectForMap = false;
2829

2930
/**
3031
* Converts a YAML string to a PHP array.
@@ -36,12 +37,13 @@ class Inline
3637
*
3738
* @return array A PHP array representing the YAML string
3839
*
39-
* @throws Exception\ParseException
40+
* @throws ParseException
4041
*/
4142
public static function parse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false)
4243
{
4344
self::$exceptionOnInvalidType = $exceptionOnInvalidType;
4445
self::$objectSupport = $objectSupport;
46+
self::$objectForMap = $objectForMap;
4547

4648
$value = trim($value);
4749

@@ -57,11 +59,11 @@ public static function parse($value, $exceptionOnInvalidType = false, $objectSup
5759
$i = 0;
5860
switch ($value[0]) {
5961
case '[':
60-
$result = self::parseSequence($value, $i, $objectForMap);
62+
$result = self::parseSequence($value, $i);
6163
++$i;
6264
break;
6365
case '{':
64-
$result = self::parseMapping($value, $i, $objectForMap);
66+
$result = self::parseMapping($value, $i);
6567
++$i;
6668
break;
6769
default:
@@ -264,13 +266,12 @@ private static function parseQuotedScalar($scalar, &$i)
264266
*
265267
* @param string $sequence
266268
* @param int &$i
267-
* @param bool $objectForMap true if maps should return a stdClass instead of array()
268269
*
269270
* @return string A YAML string
270271
*
271-
* @throws Exception\ParseException
272+
* @throws ParseException When malformed inline YAML string is parsed
272273
*/
273-
private static function parseSequence($sequence, &$i = 0, $objectForMap = false)
274+
private static function parseSequence($sequence, &$i = 0)
274275
{
275276
$output = array();
276277
$len = strlen($sequence);
@@ -281,11 +282,11 @@ private static function parseSequence($sequence, &$i = 0, $objectForMap = false)
281282
switch ($sequence[$i]) {
282283
case '[':
283284
// nested sequence
284-
$output[] = self::parseSequence($sequence, $i, $objectForMap);
285+
$output[] = self::parseSequence($sequence, $i);
285286
break;
286287
case '{':
287288
// nested mapping
288-
$output[] = self::parseMapping($sequence, $i, $objectForMap);
289+
$output[] = self::parseMapping($sequence, $i);
289290
break;
290291
case ']':
291292
return $output;
@@ -299,8 +300,7 @@ private static function parseSequence($sequence, &$i = 0, $objectForMap = false)
299300
if (!$isQuoted && false !== strpos($value, ': ')) {
300301
// embedded mapping?
301302
try {
302-
$j = 0;
303-
$value = self::parseMapping('{'.$value.'}', $j, $objectForMap);
303+
$value = self::parseMapping('{'.$value.'}');
304304
} catch (\InvalidArgumentException $e) {
305305
// no, it's not
306306
}
@@ -322,13 +322,12 @@ private static function parseSequence($sequence, &$i = 0, $objectForMap = false)
322322
*
323323
* @param string $mapping
324324
* @param int &$i
325-
* @param bool $objectForMap true if maps should return a stdClass instead of array()
326325
*
327326
* @return string A YAML string
328327
*
329-
* @throws Exception\ParseException
328+
* @throws ParseException When malformed inline YAML string is parsed
330329
*/
331-
private static function parseMapping($mapping, &$i = 0, $objectForMap = false)
330+
private static function parseMapping($mapping, &$i = 0)
332331
{
333332
$output = array();
334333
$len = strlen($mapping);
@@ -342,7 +341,7 @@ private static function parseMapping($mapping, &$i = 0, $objectForMap = false)
342341
++$i;
343342
continue 2;
344343
case '}':
345-
if (true === $objectForMap) {
344+
if (self::$objectForMap) {
346345
return (object) $output;
347346
}
348347

@@ -355,12 +354,11 @@ private static function parseMapping($mapping, &$i = 0, $objectForMap = false)
355354
// value
356355
$done = false;
357356

358-
359357
while ($i < $len) {
360358
switch ($mapping[$i]) {
361359
case '[':
362360
// nested sequence
363-
$value = self::parseSequence($mapping, $i, $objectForMap);
361+
$value = self::parseSequence($mapping, $i);
364362
// Spec: Keys MUST be unique; first one wins.
365363
// Parser cannot abort this mapping earlier, since lines
366364
// are processed sequentially.
@@ -371,7 +369,7 @@ private static function parseMapping($mapping, &$i = 0, $objectForMap = false)
371369
break;
372370
case '{':
373371
// nested mapping
374-
$value = self::parseMapping($mapping, $i, $objectForMap);
372+
$value = self::parseMapping($mapping, $i);
375373
// Spec: Keys MUST be unique; first one wins.
376374
// Parser cannot abort this mapping earlier, since lines
377375
// are processed sequentially.

src/Symfony/Component/Yaml/Parser.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ public function __construct($offset = 0)
4444
* @param string $value A YAML string
4545
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
4646
* @param bool $objectSupport true if object support is enabled, false otherwise
47+
* @param bool $objectForMap true if maps should return a stdClass instead of array()
4748
*
4849
* @return mixed A PHP value
4950
*
5051
* @throws ParseException If the YAML is not valid
5152
*/
52-
public function parse($value, $exceptionOnInvalidType = false, $objectSupport = false)
53+
public function parse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false)
5354
{
5455
$this->currentLineNb = -1;
5556
$this->currentLine = '';
@@ -93,7 +94,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
9394
$c = $this->getRealCurrentLineNb() + 1;
9495
$parser = new Parser($c);
9596
$parser->refs =& $this->refs;
96-
$data[] = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport);
97+
$data[] = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport, $objectForMap);
9798
} else {
9899
if (isset($values['leadspaces'])
99100
&& ' ' == $values['leadspaces']
@@ -109,9 +110,9 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
109110
$block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + 2);
110111
}
111112

112-
$data[] = $parser->parse($block, $exceptionOnInvalidType, $objectSupport);
113+
$data[] = $parser->parse($block, $exceptionOnInvalidType, $objectSupport, $objectForMap);
113114
} else {
114-
$data[] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport);
115+
$data[] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap);
115116
}
116117
}
117118
} elseif (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->currentLine, $values) && false === strpos($values['key'],' #')) {
@@ -121,7 +122,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
121122
$context = 'mapping';
122123

123124
// force correct settings
124-
Inline::parse(null, $exceptionOnInvalidType, $objectSupport);
125+
Inline::parse(null, $exceptionOnInvalidType, $objectSupport, $objectForMap);
125126
try {
126127
$key = Inline::parseScalar($values['key']);
127128
} catch (ParseException $e) {
@@ -146,7 +147,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
146147
$c = $this->getRealCurrentLineNb() + 1;
147148
$parser = new Parser($c);
148149
$parser->refs =& $this->refs;
149-
$parsed = $parser->parse($value, $exceptionOnInvalidType, $objectSupport);
150+
$parsed = $parser->parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap);
150151

151152
$merged = array();
152153
if (!is_array($parsed)) {
@@ -188,7 +189,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
188189
$c = $this->getRealCurrentLineNb() + 1;
189190
$parser = new Parser($c);
190191
$parser->refs =& $this->refs;
191-
$value = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport);
192+
$value = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport, $objectForMap);
192193
// Spec: Keys MUST be unique; first one wins.
193194
// Parser cannot abort this mapping earlier, since lines
194195
// are processed sequentially.
@@ -200,7 +201,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
200201
if ($isInPlace) {
201202
$data = $this->refs[$isInPlace];
202203
} else {
203-
$value = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport);;
204+
$value = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap);
204205
// Spec: Keys MUST be unique; first one wins.
205206
// Parser cannot abort this mapping earlier, since lines
206207
// are processed sequentially.
@@ -214,7 +215,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
214215
$lineCount = count($this->lines);
215216
if (1 === $lineCount || (2 === $lineCount && empty($this->lines[1]))) {
216217
try {
217-
$value = Inline::parse($this->lines[0], $exceptionOnInvalidType, $objectSupport);
218+
$value = Inline::parse($this->lines[0], $exceptionOnInvalidType, $objectSupport, $objectForMap);
218219
} catch (ParseException $e) {
219220
$e->setParsedLine($this->getRealCurrentLineNb() + 1);
220221
$e->setSnippet($this->currentLine);
@@ -390,15 +391,16 @@ private function moveToPreviousLine()
390391
/**
391392
* Parses a YAML value.
392393
*
393-
* @param string $value A YAML value
394-
* @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise
395-
* @param bool $objectSupport True if object support is enabled, false otherwise
394+
* @param string $value A YAML value
395+
* @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise
396+
* @param bool $objectSupport True if object support is enabled, false otherwise
397+
* @param bool $objectForMap true if maps should return a stdClass instead of array()
396398
*
397-
* @return mixed A PHP value
399+
* @return mixed A PHP value
398400
*
399401
* @throws ParseException When reference does not exist
400402
*/
401-
private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
403+
private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $objectForMap)
402404
{
403405
if (0 === strpos($value, '*')) {
404406
if (false !== $pos = strpos($value, '#')) {
@@ -421,7 +423,7 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
421423
}
422424

423425
try {
424-
return Inline::parse($value, $exceptionOnInvalidType, $objectSupport);
426+
return Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap);
425427
} catch (ParseException $e) {
426428
$e->setParsedLine($this->getRealCurrentLineNb() + 1);
427429
$e->setSnippet($this->currentLine);

0 commit comments

Comments
 (0)
0