Changeset 2806
- Timestamp:
- 11/25/06 08:15:29 (2 years ago)
- Files:
-
- trunk/lib/util/Spyc.class.php (modified) (24 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/util/Spyc.class.php
r2651 r2806 16 16 class YAMLNode 17 17 { 18 /**#@+19 * @access public20 * @var string21 */22 18 public $parent; 23 19 public $id; 24 /**#@+*/25 /**26 * @access public27 * @var mixed28 */29 20 public $data; 30 /**31 * @access public32 * @var int33 */34 21 public $indent; 35 /**36 * @access public37 * @var bool38 */39 22 public $children = false; 40 23 24 static protected $lastNodeId = 0; 25 41 26 /** 42 27 * The constructor assigns the node a unique ID. 43 * @access public28 * 44 29 * @return void 45 30 */ 46 public function YAMLNode()31 public function __construct() 47 32 { 48 $this->id = uniqid('');33 $this->id = ++self::$lastNodeId; 49 34 } 50 35 } … … 77 62 * print_r($array); 78 63 * </code> 79 * @access public64 * 80 65 * @return array 81 66 * @param string $input Path of YAML file or string containing YAML … … 102 87 * you can turn off wordwrap by passing in 0. 103 88 * 104 * @access public105 89 * @return string 106 90 * @param array $array PHP array … … 126 110 * print_r($array); 127 111 * </code> 128 * @access public112 * 129 113 * @return array 130 114 * @param string $input Path of YAML file or string containing YAML … … 146 130 147 131 // Initiate some objects and values 148 $base = new YAMLNode ;132 $base = new YAMLNode(); 149 133 $base->indent = 0; 150 134 $this->_lastIndent = 0; … … 177 161 { 178 162 // Create a new node and get its indent 179 $node = new YAMLNode ;163 $node = new YAMLNode(); 180 164 $node->indent = $this->_getIndent($line); 181 165 … … 332 316 * you can turn off wordwrap by passing in 0. 333 317 * 334 * @access public335 318 * @return string 336 319 * @param array $array PHP array … … 374 357 } 375 358 376 /**** Private Properties ****/ 377 378 /**#@+ 379 * @access private 380 * @var mixed 381 */ 382 private $_haveRefs; 383 private $_allNodes; 384 private $_lastIndent; 385 private $_lastNode; 386 private $_inBlock; 387 private $_isInline; 388 private $_dumpIndent; 389 private $_dumpWordWrap; 390 /**#@+*/ 391 392 /**** Private Methods ****/ 359 protected $_haveRefs; 360 protected $_allNodes; 361 protected $_lastIndent; 362 protected $_lastNode; 363 protected $_inBlock; 364 protected $_isInline; 365 protected $_dumpIndent; 366 protected $_dumpWordWrap; 393 367 394 368 /** 395 369 * Attempts to convert a key / value array item to YAML 396 * @access private370 * 397 371 * @return string 398 372 * @param $key The name of the key … … 400 374 * @param $indent The indent of the current node 401 375 */ 402 pr ivatefunction _yamlize($key, $value, $indent)376 protected function _yamlize($key, $value, $indent) 403 377 { 404 378 if (is_array($value)) … … 423 397 /** 424 398 * Attempts to convert an array to YAML 425 * @access private399 * 426 400 * @return string 427 401 * @param $array The array you want to convert 428 402 * @param $indent The indent of the current level 429 403 */ 430 pr ivatefunction _yamlizeArray($array, $indent)404 protected function _yamlizeArray($array, $indent) 431 405 { 432 406 if (is_array($array)) … … 448 422 /** 449 423 * Returns YAML from a key and a value 450 * @access private424 * 451 425 * @return string 452 426 * @param $key The name of the key … … 454 428 * @param $indent The indent of the current node 455 429 */ 456 pr ivatefunction _dumpNode($key, $value, $indent)430 protected function _dumpNode($key, $value, $indent) 457 431 { 458 432 if (is_object($value)) … … 497 471 /** 498 472 * Creates a literal block for dumping 499 * @access private473 * 500 474 * @return string 501 475 * @param $value 502 476 * @param $indent int The value of the indent 503 477 */ 504 pr ivatefunction _doLiteralBlock($value, $indent)478 protected function _doLiteralBlock($value, $indent) 505 479 { 506 480 $exploded = explode("\n", $value); … … 517 491 /** 518 492 * Folds a string of text, if necessary 519 * @access private493 * 520 494 * @return string 521 495 * @param $value The string you wish to fold 522 496 */ 523 pr ivatefunction _doFolding($value, $indent)497 protected function _doFolding($value, $indent) 524 498 { 525 499 // Don't do anything if wordwrap is set to 0 … … 544 518 /** 545 519 * Finds and returns the indentation of a YAML line 546 * @access private520 * 547 521 * @return int 548 522 * @param string $line A line from the YAML file 549 523 */ 550 pr ivatefunction _getIndent($line)524 protected function _getIndent($line) 551 525 { 552 526 preg_match('/^\s{1,}/', $line, $match); … … 565 539 /** 566 540 * Parses YAML code and returns an array for a node 567 * @access private541 * 568 542 * @return array 569 543 * @param string $line A line from the YAML file 570 544 */ 571 pr ivatefunction _parseLine($line)545 protected function _parseLine($line) 572 546 { 573 547 $line = trim($line); … … 631 605 /** 632 606 * Finds the type of the passed value, returns the value as the new type. 633 * @access private607 * 634 608 * @param string $value 635 609 * @return mixed 636 610 */ 637 pr ivatefunction _toType($value)611 protected function _toType($value) 638 612 { 639 613 if (preg_match('/^("(.*)"|\'(.*)\')/', $value, $matches)) … … 720 694 /** 721 695 * Used in inlines to check for more inlines or quoted strings 722 * @access private696 * 723 697 * @return array 724 698 */ 725 pr ivatefunction _inlineEscape($inline)699 protected function _inlineEscape($inline) 726 700 { 727 701 // There's gotta be a cleaner way to do this... … … 802 776 /** 803 777 * Builds the PHP array from all the YAML nodes we've gathered 804 * @access private778 * 805 779 * @return array 806 780 */ 807 pr ivatefunction _buildArray()781 protected function _buildArray() 808 782 { 809 783 $trunk = array(); … … 831 805 /** 832 806 * Traverses node-space and sets references (& and *) accordingly 833 * @access private807 * 834 808 * @return bool 835 809 */ 836 pr ivatefunction _linkReferences()810 protected function _linkReferences() 837 811 { 838 812 if (is_array($this->_haveRefs)) … … 901 875 /** 902 876 * Finds the children of a node and aids in the building of the PHP array 903 * @access private877 * 904 878 * @param int $nid The id of the node whose children we're gathering 905 879 * @return array 906 880 */ 907 pr ivatefunction _gatherChildren($nid)881 protected function _gatherChildren($nid) 908 882 { 909 883 $return = array(); … … 929 903 * Turns a node's data and its children's data into a PHP array 930 904 * 931 * @access private905 * 932 906 * @param array $node The node which you want to arrayize 933 907 * @return boolean 934 908 */ 935 pr ivatefunction _nodeArrayizeData(&$node)909 protected function _nodeArrayizeData(&$node) 936 910 { 937 911 if (is_array($node->data) && $node->children == true) … … 966 940 /** 967 941 * Traverses node-space and copies references to / from this object. 968 * @access private942 * 969 943 * @param object $z A node whose references we wish to make real 970 944 * @return bool 971 945 */ 972 pr ivatefunction _makeReferences(&$z)946 protected function _makeReferences(&$z) 973 947 { 974 948 // It is a reference … … 1001 975 * http://us3.php.net/manual/en/function.array-merge.php#41394 1002 976 * 1003 * @access private1004 977 * @param array $arr1 1005 978 * @param array $arr2 1006 979 * @return array 1007 980 */ 1008 private function _array_kmerge($arr1,$arr2)1009 {981 protected function _array_kmerge($arr1, $arr2) 982 { 1010 983 if (!is_array($arr1)) 1011 984 { 1012 985 $arr1 = array(); 1013 986 } 1014 1015 987 if (!is_array($arr2)) 1016 988 { 1017 $arr2 = array(); 1018 } 1019 1020 $keys1 = array_keys($arr1); 1021 $keys2 = array_keys($arr2); 1022 $keys = array_merge($keys1, $keys2); 1023 $vals1 = array_values($arr1); 1024 $vals2 = array_values($arr2); 1025 $vals = array_merge($vals1, $vals2); 989 $arr2 = array(); 990 } 991 992 $keys = array_merge(array_keys($arr1), array_keys($arr2)); 993 $vals = array_merge(array_values($arr1), array_values($arr2)); 1026 994 $ret = array(); 1027 1028 foreach($keys as $key) 995 foreach ($keys as $key) 1029 996 { 1030 997 list($unused, $val) = each($vals); 1031 // This is the good part! If a key already exists, but it's part of a 1032 // sequence (an int), just keep addin numbers until we find a fresh one. 1033 if (isset($ret[$key]) and is_int($key)) 1034 { 1035 while (array_key_exists($key, $ret)) 1036 { 1037 $key++; 1038 } 1039 } 1040 $ret[$key] = $val; 998 if (isset($ret[$key]) && is_int($key)) 999 { 1000 $ret[] = $val; 1001 } 1002 else 1003 { 1004 $ret[$key] = $val; 1005 } 1041 1006 } 1042 1007