Browse Source

[Yaml] added support for compact notation (patch from redotheoffice - closes #8082)

Fabien Potencier 15 years ago
parent
commit
a80a61b5c2

+ 19 - 2
src/Symfony/Components/Yaml/Parser.php

@@ -65,7 +65,7 @@ class Parser
       }
 
       $isRef = $isInPlace = $isProcessed = false;
-      if (preg_match('#^\-(\s+(?P<value>.+?))?\s*$#', $this->currentLine, $values))
+      if (preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+?))?\s*$#', $this->currentLine, $values))
       {
         if (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#', $values['value'], $matches))
         {
@@ -87,13 +87,30 @@ class Parser
           {
             $data[] = array($matches[1] => Inline::load($matches[2]));
           }
+          elseif (isset($values['leadspaces'])
+            && ' ' == $values['leadspaces']
+            && preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"].*?) *\:(\s+(?P<value>.+?))?\s*$#', $values['value'], $matches))
+          {
+            // this is a compact notation element, add to next block and parse
+            $c = $this->getRealCurrentLineNb();
+            $parser = new Parser($c);
+            $parser->refs =& $this->refs;
+
+            $block = $values['value'];
+            if (!$this->isNextLineIndented())
+            {
+              $block .= "\n".$this->getNextEmbedBlock();
+            }
+
+            $data[] = $parser->parse($block);
+          }
           else
           {
             $data[] = $this->parseValue($values['value']);
           }
         }
       }
-      else if (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ ].*?) *\:(\s+(?P<value>.+?))?\s*$#', $this->currentLine, $values))
+      else if (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"].*?) *\:(\s+(?P<value>.+?))?\s*$#', $this->currentLine, $values))
       {
         $key = Inline::parseScalar($values['key']);
 

+ 16 - 2
tests/fixtures/Symfony/Components/Yaml/YtsSpecificationExamples.yml

@@ -149,7 +149,7 @@ yaml: |
       hr: 63,
       avg: 0.288
     }
-ruby: | 
+php: | 
   array(  
     'Mark McGwire' => 
       array( 'hr' => 65, 'avg' => 0.278 ), 
@@ -286,7 +286,6 @@ syck: |
  
 --- 
 test: Sequence key shortcut 
-todo: true
 spec: 2.12
 yaml: | 
   ---
@@ -297,6 +296,21 @@ yaml: |
     quantity: 4
   - item    : Big Shoes
     quantity: 1
+php: | 
+  array (
+    array (
+      'item' => 'Super Hoop',
+      'quantity' => 1,
+    ),
+    array (
+      'item' => 'Basketball',
+      'quantity' => 4,
+    ),
+    array (
+      'item' => 'Big Shoes',
+      'quantity' => 1,
+    )
+  )
 perl: | 
   [  
      { item => 'Super Hoop', quantity => 1 },