Exemplo n.º 1
0
 private static function _parseString($lines)
 {
     self::$path = self::$result = self::$delayedPath = self::$savedGroups = array();
     self::$groupAlias = self::$groupAnchor = FALSE;
     $count = count($lines);
     for ($i = 0; $i < $count; ++$i) {
         $line = $lines[$i];
         $indent = $idt = strlen($line) - strlen(ltrim($line));
         // getParentPathByIndent extracts inline
         if ($indent === 0) {
             $tmpPath = array();
         } else {
             $tmpPath = self::$path;
             do {
                 end($tmpPath);
                 if ($indent <= ($lastIndentPath = key($tmpPath))) {
                     unset($tmpPath[$lastIndentPath]);
                 }
             } while ($indent <= $lastIndentPath);
         }
         if ($indent === -1) {
             $idt = strlen($line) - strlen(ltrim($line));
         }
         $line = substr($line, $idt);
         $tmpLine = trim($line);
         // line string is commented or empty section?
         if ($tmpLine === '' || $line[0] === '#' || trim($line, " \r\n\t") === '---') {
             continue;
         }
         self::$path = $tmpPath;
         $lastChar = substr($tmpLine, -1);
         $literalBlockStyle = $lastChar !== '>' && $lastChar !== '|' || preg_match('#<.*?>$#', $line) ? FALSE : $lastChar;
         if ($literalBlockStyle) {
             $literalBlock = '';
             $line = rtrim($line, $literalBlockStyle . " \n") . '__SPICYYAML__';
             $literalBlockIndent = strlen($lines[++$i]) - strlen(ltrim($lines[$i--]));
             while (++$i < $count && (!trim($lines[$i]) || strlen($lines[$i]) - strlen(ltrim($lines[$i])) > $indent)) {
                 $tmpLine = $lines[$i];
                 //$_indent = ( $literalBlockIndent === -1 ) ? (strlen($tmpLine) - strlen(ltrim($tmpLine))) : $literalBlockIndent;
                 $tmpLineTrimedDistance = strlen($tmpLine) - strlen(ltrim($tmpLine));
                 $tmpLine = substr($tmpLine, $literalBlockIndent === -1 ? $tmpLineTrimedDistance : $literalBlockIndent);
                 //$tmpLine = substr($tmpLine, $_indent);
                 if ($literalBlockStyle !== '|') {
                     //$_indent = strlen($tmpLine) - strlen(ltrim($tmpLine));
                     //$tmpLine = substr($tmpLine, $_indent);
                     $tmpLine = substr($tmpLine, $tmpLineTrimedDistance);
                 }
                 $tmpLine = rtrim($tmpLine, "\r\n\t ") . "\n";
                 if ($literalBlockStyle === '|') {
                     $literalBlock .= $tmpLine;
                 } else {
                     if ($tmpLine == "\n" && $literalBlockStyle === '>') {
                         $literalBlock = rtrim($literalBlock, " \t") . "\n";
                     } else {
                         if (strlen($tmpLine) === 0) {
                             $literalBlock = rtrim($literalBlock, ' ') . "\n";
                         } else {
                             if ($tmpLine !== "\n") {
                                 $tmpLine = trim($tmpLine, "\r\n ") . " ";
                             }
                             $literalBlock .= $tmpLine;
                             //$literalBlock .= ( $tmpLine !== "\n" ) ? trim($tmpLine, "\r\n ") . " " : $tmpLine;
                         }
                     }
                 }
             }
             --$i;
         }
         while (++$i < $count) {
             $tmpLine = trim($line);
             if (!strlen($tmpLine) || substr($tmpLine, -1, 1) === ']') {
                 break;
             }
             if ($tmpLine[0] === '[' || preg_match('#^[^:]+?:\\s*\\[#', $tmpLine)) {
                 $line = rtrim($line, " \n\t\r") . ' ' . ltrim($lines[$i], " \t");
                 continue;
             }
             break;
             //$line = rtrim($line, " \n\t\r") . ' ' . ltrim($lines[$i], " \t");
         }
         --$i;
         if (strpos($line, '#') && strpos($line, '"') === FALSE && strpos($line, '\'') === FALSE) {
             //if ( strpos($line, '"') === FALSE && strpos($line, '\'') === FALSE )
             //{
             $line = preg_replace('/\\s+#(.+)$/', '', $line);
             //}
         }
         $lineArray = !$line || !($line = trim($line)) ? array() : self::_parseLine($line, $indent);
         if ($literalBlockStyle) {
             $lineArray = self::_revertLiteralPlaceHolder($lineArray, $literalBlock);
         }
         if (count($lineArray) > 1) {
             // addArrayInline inline
             $groupPath = self::$path;
             foreach ($lineArray as $k => $v) {
                 self::_addArray(array($k => $v), $indent);
                 self::$path = $groupPath;
             }
         } else {
             self::_addArray($lineArray, $indent);
         }
         foreach (self::$delayedPath as $idt => $delayedPath) {
             self::$path[$idt] = $delayedPath;
         }
         self::$delayedPath = array();
     }
     return self::$result;
 }