Correct handling of hashes according to YAML 1.1 specs. Thanks to Pascal A. Diethelm...
[spyc.git] / spyc.php
blobde56a037fcf8f3f9e881f7e3c31926b4accee398
1 <?php
2 /**
3 * Spyc -- A Simple PHP YAML Class
4 * @version 0.3
5 * @author Chris Wanstrath <chris@ozmm.org>
6 * @author Vlad Andersen <vlad@oneiros.ru>
7 * @link http://spyc.sourceforge.net/
8 * @copyright Copyright 2005-2006 Chris Wanstrath
9 * @license http://www.opensource.org/licenses/mit-license.php MIT License
10 * @package Spyc
12 /**
13 * The Simple PHP YAML Class.
15 * This class can be used to read a YAML file and convert its contents
16 * into a PHP array. It currently supports a very limited subsection of
17 * the YAML spec.
19 * Usage:
20 * <code>
21 * $parser = new Spyc;
22 * $array = $parser->load($file);
23 * </code>
24 * @package Spyc
26 class Spyc {
28 /**#@+
29 * @access private
30 * @var mixed
32 private $_haveRefs;
33 private $_allNodes;
34 private $_allParent;
35 private $_lastIndent;
36 private $_lastNode;
37 private $_inBlock;
38 private $_isInline;
39 private $_dumpIndent;
40 private $_dumpWordWrap;
41 private $_containsGroupAnchor = false;
42 private $_containsGroupAlias = false;
43 private $path;
44 private $result;
45 private $LiteralBlockMarkers = array ('>', '|');
46 private $LiteralPlaceHolder = '___YAML_Literal_Block___';
47 private $SavedGroups = array();
49 /**#@+
50 * @access public
51 * @var mixed
53 public $_nodeId;
55 /**
56 * Load YAML into a PHP array statically
58 * The load method, when supplied with a YAML stream (string or file),
59 * will do its best to convert YAML in a file into a PHP array. Pretty
60 * simple.
61 * Usage:
62 * <code>
63 * $array = Spyc::YAMLLoad('lucky.yaml');
64 * print_r($array);
65 * </code>
66 * @access public
67 * @return array
68 * @param string $input Path of YAML file or string containing YAML
70 public static function YAMLLoad($input) {
71 $Spyc = new Spyc;
72 return $Spyc->load($input);
75 /**
76 * Load a string of YAML into a PHP array statically
78 * The load method, when supplied with a YAML string, will do its best
79 * to convert YAML in a string into a PHP array. Pretty simple.
81 * Note: use this function if you don't want files from the file system
82 * loaded and processed as YAML. This is of interest to people concerned
83 * about security whose input is from a string.
85 * Usage:
86 * <code>
87 * $array = Spyc::YAMLLoadString("---\n0: hello world\n");
88 * print_r($array);
89 * </code>
90 * @access public
91 * @return array
92 * @param string $input String containing YAML
94 public static function YAMLLoadString($input) {
95 $Spyc = new Spyc;
96 return $Spyc->loadString($input);
99 /**
100 * Dump YAML from PHP array statically
102 * The dump method, when supplied with an array, will do its best
103 * to convert the array into friendly YAML. Pretty simple. Feel free to
104 * save the returned string as nothing.yaml and pass it around.
106 * Oh, and you can decide how big the indent is and what the wordwrap
107 * for folding is. Pretty cool -- just pass in 'false' for either if
108 * you want to use the default.
110 * Indent's default is 2 spaces, wordwrap's default is 40 characters. And
111 * you can turn off wordwrap by passing in 0.
113 * @access public
114 * @return string
115 * @param array $array PHP array
116 * @param int $indent Pass in false to use the default, which is 2
117 * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40)
119 public static function YAMLDump($array,$indent = false,$wordwrap = false) {
120 $spyc = new Spyc;
121 return $spyc->dump($array,$indent,$wordwrap);
126 * Dump PHP array to YAML
128 * The dump method, when supplied with an array, will do its best
129 * to convert the array into friendly YAML. Pretty simple. Feel free to
130 * save the returned string as tasteful.yaml and pass it around.
132 * Oh, and you can decide how big the indent is and what the wordwrap
133 * for folding is. Pretty cool -- just pass in 'false' for either if
134 * you want to use the default.
136 * Indent's default is 2 spaces, wordwrap's default is 40 characters. And
137 * you can turn off wordwrap by passing in 0.
139 * @access public
140 * @return string
141 * @param array $array PHP array
142 * @param int $indent Pass in false to use the default, which is 2
143 * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40)
145 public function dump($array,$indent = false,$wordwrap = false) {
146 // Dumps to some very clean YAML. We'll have to add some more features
147 // and options soon. And better support for folding.
149 // New features and options.
150 if ($indent === false or !is_numeric($indent)) {
151 $this->_dumpIndent = 2;
152 } else {
153 $this->_dumpIndent = $indent;
156 if ($wordwrap === false or !is_numeric($wordwrap)) {
157 $this->_dumpWordWrap = 40;
158 } else {
159 $this->_dumpWordWrap = $wordwrap;
162 // New YAML document
163 $string = "---\n";
165 // Start at the base of the array and move through it.
166 foreach ($array as $key => $value) {
167 $string .= $this->_yamlize($key,$value,0);
169 return $string;
173 * Attempts to convert a key / value array item to YAML
174 * @access private
175 * @return string
176 * @param $key The name of the key
177 * @param $value The value of the item
178 * @param $indent The indent of the current node
180 private function _yamlize($key,$value,$indent) {
181 if (is_array($value)) {
182 // It has children. What to do?
183 // Make it the right kind of item
184 $string = $this->_dumpNode($key,NULL,$indent);
185 // Add the indent
186 $indent += $this->_dumpIndent;
187 // Yamlize the array
188 $string .= $this->_yamlizeArray($value,$indent);
189 } elseif (!is_array($value)) {
190 // It doesn't have children. Yip.
191 $string = $this->_dumpNode($key,$value,$indent);
193 return $string;
197 * Attempts to convert an array to YAML
198 * @access private
199 * @return string
200 * @param $array The array you want to convert
201 * @param $indent The indent of the current level
203 private function _yamlizeArray($array,$indent) {
204 if (is_array($array)) {
205 $string = '';
206 foreach ($array as $key => $value) {
207 $string .= $this->_yamlize($key,$value,$indent);
209 return $string;
210 } else {
211 return false;
216 * Returns YAML from a key and a value
217 * @access private
218 * @return string
219 * @param $key The name of the key
220 * @param $value The value of the item
221 * @param $indent The indent of the current node
223 private function _dumpNode($key,$value,$indent) {
224 // do some folding here, for blocks
225 if (strpos($value,"\n") !== false || strpos($value,": ") !== false || strpos($value,"- ") !== false) {
226 $value = $this->_doLiteralBlock($value,$indent);
227 } else {
228 $value = $this->_doFolding($value,$indent);
231 if (is_bool($value)) {
232 $value = ($value) ? "true" : "false";
235 $spaces = str_repeat(' ',$indent);
237 if (is_int($key)) {
238 // It's a sequence
239 $string = $spaces.'- '.$value."\n";
240 } else {
241 // It's mapped
242 if (strpos($key, ":") !== false) { $key = '"' . $key . '"'; }
243 $string = $spaces.$key.': '.$value."\n";
245 return $string;
249 * Creates a literal block for dumping
250 * @access private
251 * @return string
252 * @param $value
253 * @param $indent int The value of the indent
255 private function _doLiteralBlock($value,$indent) {
256 $exploded = explode("\n",$value);
257 $newValue = '|';
258 $indent += $this->_dumpIndent;
259 $spaces = str_repeat(' ',$indent);
260 foreach ($exploded as $line) {
261 $newValue .= "\n" . $spaces . trim($line);
263 return $newValue;
267 * Folds a string of text, if necessary
268 * @access private
269 * @return string
270 * @param $value The string you wish to fold
272 private function _doFolding($value,$indent) {
273 // Don't do anything if wordwrap is set to 0
274 if ($this->_dumpWordWrap === 0) {
275 return $value;
278 if (strlen($value) > $this->_dumpWordWrap) {
279 $indent += $this->_dumpIndent;
280 $indent = str_repeat(' ',$indent);
281 $wrapped = wordwrap($value,$this->_dumpWordWrap,"\n$indent");
282 $value = ">\n".$indent.$wrapped;
284 return $value;
287 /* LOADING FUNCTIONS */
289 private function load($input) {
290 $Source = $this->loadFromSource($input);
291 return $this->loadWithSource($Source);
294 private function loadString($input) {
295 $Source = $this->loadFromString($input);
296 return $this->loadWithSource($Source);
299 private function loadWithSource($Source) {
300 if (empty ($Source)) return array();
301 $this->path = array();
302 $this->result = array();
305 for ($i = 0; $i < count($Source); $i++) {
306 $line = $Source[$i];
308 $lineIndent = $this->_getIndent($line);
309 $this->path = $this->getParentPathByIndent($lineIndent);
310 $line = $this->stripIndent($line, $lineIndent);
311 if ($this->isComment($line)) continue;
313 if ($literalBlockStyle = $this->startsLiteralBlock($line)) {
314 $line = rtrim ($line, $literalBlockStyle . "\n");
315 $literalBlock = '';
316 $line .= $this->LiteralPlaceHolder;
318 while (++$i < count($Source) && $this->literalBlockContinues($Source[$i], $lineIndent)) {
319 $literalBlock = $this->addLiteralLine($literalBlock, $Source[$i], $literalBlockStyle);
321 $i--;
323 $lineArray = $this->_parseLine($line);
324 if ($literalBlockStyle)
325 $lineArray = $this->revertLiteralPlaceHolder ($lineArray, $literalBlock);
327 $this->addArray($lineArray, $lineIndent);
329 return $this->result;
332 private function loadFromSource ($input) {
333 if (!empty($input) && strpos($input, "\n") === false && file_exists($input))
334 return file($input);
336 return $this->loadFromString($input);
339 function loadFromString ($input) {
340 $lines = explode("\n",$input);
341 foreach ($lines as $k => $_) {
342 $lines[$k] = trim ($_, "\r");
344 return $lines;
348 * Finds and returns the indentation of a YAML line
349 * @access private
350 * @return int
351 * @param string $line A line from the YAML file
353 private function _getIndent($line) {
354 if (!preg_match('/^ +/',$line,$match)) return 0;
355 if (!empty($match[0])) return strlen ($match[0]);
356 return 0;
360 * Parses YAML code and returns an array for a node
361 * @access private
362 * @return array
363 * @param string $line A line from the YAML file
365 private function _parseLine($line) {
366 if (!$line) return array();
367 $line = trim($line);
368 if (!$line) return array();
369 $array = array();
371 if ($group = $this->nodeContainsGroup($line)) {
372 $this->addGroup($line, $group);
373 $line = $this->stripGroup ($line, $group);
376 if ($this->startsMappedSequence($line))
377 return $this->returnMappedSequence($line);
379 if ($this->startsMappedValue($line))
380 return $this->returnMappedValue($line);
382 if ($this->isArrayElement($line))
383 return $this->returnArrayElement($line);
385 if ($this->isPlainArray($line))
386 return $this->returnPlainArray($line);
389 return $this->returnKeyValuePair($line);
394 * Finds the type of the passed value, returns the value as the new type.
395 * @access private
396 * @param string $value
397 * @return mixed
399 private function _toType($value) {
400 $is_quoted = false;
401 do {
402 if (!$value) break;
403 if (substr($value, 0, 1) != '"' && substr($value, 0, 1) != "'") break;
404 if (substr($value, -1, 1) != '"' && substr($value, -1, 1) != "'") break;
405 $is_quoted = true;
406 } while (0);
408 if (!$is_quoted && strpos($value, ' #') !== false)
409 $value = preg_replace('/\s+#(.+)$/','',$value);
411 if (preg_match('/^("(.*)"|\'(.*)\')/',$value,$matches)) {
412 $value = (string)preg_replace('/(\'\'|\\\\\')/',"'",end($matches));
413 $value = preg_replace('/\\\\"/','"',$value);
414 } elseif (preg_match('/^\\[(.+)\\]$/',$value,$matches)) {
415 // Inline Sequence
417 // Take out strings sequences and mappings
418 $explode = $this->_inlineEscape($matches[1]);
420 // Propagate value array
421 $value = array();
422 foreach ($explode as $v) {
423 $value[] = $this->_toType($v);
425 } elseif (strpos($value,': ')!==false && !preg_match('/^{(.+)/',$value)) {
426 // It's a map
427 $array = explode(': ',$value);
428 $key = trim($array[0]);
429 array_shift($array);
430 $value = trim(implode(': ',$array));
431 $value = $this->_toType($value);
432 $value = array($key => $value);
433 } elseif (preg_match("/{(.+)}$/",$value,$matches)) {
434 // Inline Mapping
436 // Take out strings sequences and mappings
437 $explode = $this->_inlineEscape($matches[1]);
439 // Propogate value array
440 $array = array();
441 foreach ($explode as $v) {
442 $SubArr = $this->_toType($v);
443 if (empty($SubArr)) continue;
444 if (is_array ($SubArr)) {
445 $array[key($SubArr)] = $SubArr[key($SubArr)]; continue;
447 $array[] = $SubArr;
449 $value = $array;
450 } elseif (strtolower($value) == 'null' or $value == '' or $value == '~') {
451 $value = null;
452 } elseif (preg_match ('/^[1-9]+[0-9]*$/', $value)) {
453 $intvalue = (int)$value;
454 if ($intvalue != PHP_INT_MAX)
455 $value = $intvalue;
456 } elseif (in_array(strtolower($value),
457 array('true', 'on', '+', 'yes', 'y'))) {
458 $value = true;
459 } elseif (in_array(strtolower($value),
460 array('false', 'off', '-', 'no', 'n'))) {
461 $value = false;
462 } elseif (is_numeric($value)) {
463 if ($value === '0') return 0;
464 if (trim ($value, 0) === $value)
465 $value = (float)$value;
466 } else {
467 // Just a normal string, right?
472 // print_r ($value);
473 return $value;
477 * Used in inlines to check for more inlines or quoted strings
478 * @access private
479 * @return array
481 private function _inlineEscape($inline) {
482 // There's gotta be a cleaner way to do this...
483 // While pure sequences seem to be nesting just fine,
484 // pure mappings and mappings with sequences inside can't go very
485 // deep. This needs to be fixed.
487 $saved_strings = array();
489 // Check for strings
490 $regex = '/(?:(")|(?:\'))((?(1)[^"]+|[^\']+))(?(1)"|\')/';
491 if (preg_match_all($regex,$inline,$strings)) {
492 $saved_strings = $strings[0];
493 $inline = preg_replace($regex,'YAMLString',$inline);
495 unset($regex);
497 // Check for sequences
498 if (preg_match_all('/\[(.+)\]/U',$inline,$seqs)) {
499 $inline = preg_replace('/\[(.+)\]/U','YAMLSeq',$inline);
500 $seqs = $seqs[0];
503 // Check for mappings
504 if (preg_match_all('/{(.+)}/U',$inline,$maps)) {
505 $inline = preg_replace('/{(.+)}/U','YAMLMap',$inline);
506 $maps = $maps[0];
509 $explode = explode(', ',$inline);
512 // Re-add the sequences
513 if (!empty($seqs)) {
514 $i = 0;
515 foreach ($explode as $key => $value) {
516 if (strpos($value,'YAMLSeq') !== false) {
517 $explode[$key] = str_replace('YAMLSeq',$seqs[$i],$value);
518 ++$i;
523 // Re-add the mappings
524 if (!empty($maps)) {
525 $i = 0;
526 foreach ($explode as $key => $value) {
527 if (strpos($value,'YAMLMap') !== false) {
528 $explode[$key] = str_replace('YAMLMap',$maps[$i],$value);
529 ++$i;
535 // Re-add the strings
536 if (!empty($saved_strings)) {
537 $i = 0;
538 foreach ($explode as $key => $value) {
539 while (strpos($value,'YAMLString') !== false) {
540 $explode[$key] = preg_replace('/YAMLString/',$saved_strings[$i],$value, 1);
541 ++$i;
542 $value = $explode[$key];
547 return $explode;
550 private function literalBlockContinues ($line, $lineIndent) {
551 if (!trim($line)) return true;
552 if ($this->_getIndent($line) > $lineIndent) return true;
553 return false;
556 private function addArrayInline ($array, $indent) {
557 $CommonGroupPath = $this->path;
558 if (empty ($array)) return false;
560 foreach ($array as $k => $_) {
561 $this->addArray(array($k => $_), $indent);
562 $this->path = $CommonGroupPath;
564 return true;
567 private function addArray ($array, $indent) {
569 if (count ($array) > 1)
570 return $this->addArrayInline ($array, $indent);
572 $key = key ($array);
574 if (!isset ($array[$key])) return false;
575 if ($array[$key] === array()) { $array[$key] = ''; };
576 $value = $array[$key];
578 // Unfolding inner array tree as defined in $this->_arrpath.
579 //$_arr = $this->result; $_tree[0] = $_arr; $i = 1;
581 $tempPath = Spyc::flatten ($this->path);
582 eval ('$_arr = $this->result' . $tempPath . ';');
585 if ($this->_containsGroupAlias) {
586 do {
587 if (!isset($this->SavedGroups[$this->_containsGroupAlias])) { echo "Bad group name: $this->_containsGroupAlias."; break; }
588 $groupPath = $this->SavedGroups[$this->_containsGroupAlias];
589 eval ('$value = $this->result' . Spyc::flatten ($groupPath) . ';');
590 } while (false);
591 $this->_containsGroupAlias = false;
595 // Adding string or numeric key to the innermost level or $this->arr.
596 if ($key)
597 $_arr[$key] = $value;
598 else {
599 if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; }
600 else { $_arr[] = $value; end ($_arr); $key = key ($_arr); }
604 $this->path[$indent] = $key;
606 eval ('$this->result' . $tempPath . ' = $_arr;');
608 if ($this->_containsGroupAnchor) {
609 $this->SavedGroups[$this->_containsGroupAnchor] = $this->path;
610 $this->_containsGroupAnchor = false;
617 private function flatten ($array) {
618 $tempPath = array();
619 if (!empty ($array)) {
620 foreach ($array as $_) {
621 if (!is_int($_)) $_ = "'$_'";
622 $tempPath[] = "[$_]";
625 //end ($tempPath); $latestKey = key($tempPath);
626 $tempPath = implode ('', $tempPath);
627 return $tempPath;
632 private function startsLiteralBlock ($line) {
633 $lastChar = substr (trim($line), -1);
634 if (in_array ($lastChar, $this->LiteralBlockMarkers))
635 return $lastChar;
636 return false;
639 private function addLiteralLine ($literalBlock, $line, $literalBlockStyle) {
640 $line = $this->stripIndent($line);
641 $line = str_replace ("\r\n", "\n", $line);
643 if ($literalBlockStyle == '|') {
644 return $literalBlock . $line;
646 if (strlen($line) == 0) return $literalBlock . "\n";
648 // echo "|$line|";
649 if ($line != "\n")
650 $line = trim ($line, "\r\n ") . " ";
652 return $literalBlock . $line;
655 function revertLiteralPlaceHolder ($lineArray, $literalBlock) {
656 foreach ($lineArray as $k => $_) {
657 if (is_array($_))
658 $lineArray[$k] = $this->revertLiteralPlaceHolder ($_, $literalBlock);
659 else if (substr($_, -1 * strlen ($this->LiteralPlaceHolder)) == $this->LiteralPlaceHolder)
660 $lineArray[$k] = rtrim ($literalBlock, " \r\n");
662 return $lineArray;
665 private function stripIndent ($line, $indent = -1) {
666 if ($indent == -1) $indent = $this->_getIndent($line);
667 return substr ($line, $indent);
670 private function getParentPathByIndent ($indent) {
672 if ($indent == 0) return array();
674 $linePath = $this->path;
675 do {
676 end($linePath); $lastIndentInParentPath = key($linePath);
677 if ($indent <= $lastIndentInParentPath) array_pop ($linePath);
678 } while ($indent <= $lastIndentInParentPath);
679 return $linePath;
683 private function clearBiggerPathValues ($indent) {
686 if ($indent == 0) $this->path = array();
687 if (empty ($this->path)) return true;
689 foreach ($this->path as $k => $_) {
690 if ($k > $indent) unset ($this->path[$k]);
693 return true;
697 private function isComment ($line) {
698 if (preg_match('/^#/', $line)) return true;
699 if (trim($line, " \r\n\t") == '---') return true;
700 return false;
703 private function isArrayElement ($line) {
704 if (!$line) return false;
705 if ($line[0] != '-') return false;
706 if (strlen ($line) > 3)
707 if (substr($line,0,3) == '---') return false;
709 return true;
712 private function isHashElement ($line) {
713 if (!preg_match('/^(.+?):/', $line, $matches)) return false;
714 $allegedKey = $matches[1];
715 if ($allegedKey) return true;
716 //if (substr_count($allegedKey, )
717 return false;
720 private function isLiteral ($line) {
721 if ($this->isArrayElement($line)) return false;
722 if ($this->isHashElement($line)) return false;
723 return true;
727 private function startsMappedSequence ($line) {
728 if (preg_match('/^-(.*):$/',$line)) return true;
731 private function returnMappedSequence ($line) {
732 $array = array();
733 $key = trim(substr(substr($line,1),0,-1));
734 $array[$key] = '';
735 return $array;
738 private function returnMappedValue ($line) {
739 $array = array();
740 $key = trim(substr($line,0,-1));
741 $array[$key] = '';
742 return $array;
745 private function startsMappedValue ($line) {
746 if (preg_match('/^(.*):$/',$line)) return true;
749 private function isPlainArray ($line) {
750 if (preg_match('/^\[(.*)\]$/', $line)) return true;
751 return false;
754 private function returnPlainArray ($line) {
755 return $this->_toType($line);
758 private function returnKeyValuePair ($line) {
760 $array = array();
762 if (preg_match('/^(.+):/',$line,$key)) {
763 // It's a key/value pair most likely
764 // If the key is in double quotes pull it out
765 if (preg_match('/^(["\'](.*)["\'](\s)*:)/',$line,$matches)) {
766 $value = trim(str_replace($matches[1],'',$line));
767 $key = $matches[2];
768 } else {
769 // Do some guesswork as to the key and the value
770 $explode = explode(':',$line);
771 $key = trim($explode[0]);
772 array_shift($explode);
773 $value = trim(implode(':',$explode));
776 // Set the type of the value. Int, string, etc
777 $value = $this->_toType($value);
778 if (empty($key)) {
779 $array[] = $value;
780 } else {
781 $array[$key] = $value;
785 return $array;
790 private function returnArrayElement ($line) {
791 if (strlen($line) <= 1) return array(array()); // Weird %)
792 $array = array();
793 $value = trim(substr($line,1));
794 $value = $this->_toType($value);
795 $array[] = $value;
796 return $array;
800 private function nodeContainsGroup ($line) {
801 $symbolsForReference = 'A-z0-9_\-';
802 if (strpos($line, '&') === false && strpos($line, '*') === false) return false; // Please die fast ;-)
803 if (preg_match('/^(&['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1];
804 if (preg_match('/^(\*['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1];
805 if (preg_match('/(&['.$symbolsForReference.']+$)/', $line, $matches)) return $matches[1];
806 if (preg_match('/(\*['.$symbolsForReference.']+$)/', $line, $matches)) return $matches[1];
807 return false;
811 private function addGroup ($line, $group) {
812 if (substr ($group, 0, 1) == '&') $this->_containsGroupAnchor = substr ($group, 1);
813 if (substr ($group, 0, 1) == '*') $this->_containsGroupAlias = substr ($group, 1);
814 //print_r ($this->path);
817 private function stripGroup ($line, $group) {
818 $line = trim(str_replace($group, '', $line));
819 return $line;