@@ -243,14 +243,18 @@ discard block |
||
| 243 | 243 | |
| 244 | 244 | // New YAML document |
| 245 | 245 | $string = ""; |
| 246 | - if (!$no_opening_dashes) $string = "---\n"; |
|
| 246 | + if (!$no_opening_dashes) { |
|
| 247 | + $string = "---\n"; |
|
| 248 | + } |
|
| 247 | 249 | |
| 248 | 250 | // Start at the base of the array and move through it. |
| 249 | 251 | if ($array) { |
| 250 | 252 | $array = (array)$array; |
| 251 | 253 | $previous_key = -1; |
| 252 | 254 | foreach ($array as $key => $value) { |
| 253 | - if (!isset($first_key)) $first_key = $key; |
|
| 255 | + if (!isset($first_key)) { |
|
| 256 | + $first_key = $key; |
|
| 257 | + } |
|
| 254 | 258 | $string .= $this->_yamlize($key,$value,0,$previous_key, $first_key, $array); |
| 255 | 259 | $previous_key = $key; |
| 256 | 260 | } |
@@ -267,10 +271,13 @@ discard block |
||
| 267 | 271 | * @param $indent The indent of the current node |
| 268 | 272 | */ |
| 269 | 273 | private function _yamlize($key,$value,$indent, $previous_key = -1, $first_key = 0, $source_array = null) { |
| 270 | - if(is_object($value)) $value = (array)$value; |
|
| 274 | + if(is_object($value)) { |
|
| 275 | + $value = (array)$value; |
|
| 276 | + } |
|
| 271 | 277 | if (is_array($value)) { |
| 272 | - if (empty ($value)) |
|
| 273 | - return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key, $source_array); |
|
| 278 | + if (empty ($value)) { |
|
| 279 | + return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key, $source_array); |
|
| 280 | + } |
|
| 274 | 281 | // It has children. What to do? |
| 275 | 282 | // Make it the right kind of item |
| 276 | 283 | $string = $this->_dumpNode($key, self::REMPTY, $indent, $previous_key, $first_key, $source_array); |
@@ -297,7 +304,9 @@ discard block |
||
| 297 | 304 | $string = ''; |
| 298 | 305 | $previous_key = -1; |
| 299 | 306 | foreach ($array as $key => $value) { |
| 300 | - if (!isset($first_key)) $first_key = $key; |
|
| 307 | + if (!isset($first_key)) { |
|
| 308 | + $first_key = $key; |
|
| 309 | + } |
|
| 301 | 310 | $string .= $this->_yamlize($key, $value, $indent, $previous_key, $first_key, $array); |
| 302 | 311 | $previous_key = $key; |
| 303 | 312 | } |
@@ -327,20 +336,29 @@ discard block |
||
| 327 | 336 | $value = $this->_doFolding($value,$indent); |
| 328 | 337 | } |
| 329 | 338 | |
| 330 | - if ($value === array()) $value = '[ ]'; |
|
| 331 | - if ($value === "") $value = '""'; |
|
| 339 | + if ($value === array()) { |
|
| 340 | + $value = '[ ]'; |
|
| 341 | + } |
|
| 342 | + if ($value === "") { |
|
| 343 | + $value = '""'; |
|
| 344 | + } |
|
| 332 | 345 | if (self::isTranslationWord($value)) { |
| 333 | 346 | $value = $this->_doLiteralBlock($value, $indent); |
| 334 | 347 | } |
| 335 | - if (trim ($value) != $value) |
|
| 336 | - $value = $this->_doLiteralBlock($value,$indent); |
|
| 348 | + if (trim ($value) != $value) { |
|
| 349 | + $value = $this->_doLiteralBlock($value,$indent); |
|
| 350 | + } |
|
| 337 | 351 | |
| 338 | 352 | if (is_bool($value)) { |
| 339 | 353 | $value = $value ? "true" : "false"; |
| 340 | 354 | } |
| 341 | 355 | |
| 342 | - if ($value === null) $value = 'null'; |
|
| 343 | - if ($value === "'" . self::REMPTY . "'") $value = null; |
|
| 356 | + if ($value === null) { |
|
| 357 | + $value = 'null'; |
|
| 358 | + } |
|
| 359 | + if ($value === "'" . self::REMPTY . "'") { |
|
| 360 | + $value = null; |
|
| 361 | + } |
|
| 344 | 362 | |
| 345 | 363 | $spaces = str_repeat(' ',$indent); |
| 346 | 364 | |
@@ -365,7 +383,9 @@ discard block |
||
| 365 | 383 | * @param $indent int The value of the indent |
| 366 | 384 | */ |
| 367 | 385 | private function _doLiteralBlock($value,$indent) { |
| 368 | - if ($value === "\n") return '\n'; |
|
| 386 | + if ($value === "\n") { |
|
| 387 | + return '\n'; |
|
| 388 | + } |
|
| 369 | 389 | if (strpos($value, "\n") === false && strpos($value, "'") === false) { |
| 370 | 390 | return sprintf ("'%s'", $value); |
| 371 | 391 | } |
@@ -405,10 +425,12 @@ discard block |
||
| 405 | 425 | $wrapped = wordwrap($value,$this->_dumpWordWrap,"\n$indent"); |
| 406 | 426 | $value = ">\n".$indent.$wrapped; |
| 407 | 427 | } else { |
| 408 | - if ($this->setting_dump_force_quotes && is_string ($value) && $value !== self::REMPTY) |
|
| 409 | - $value = '"' . $value . '"'; |
|
| 410 | - if (is_numeric($value) && is_string($value)) |
|
| 411 | - $value = '"' . $value . '"'; |
|
| 428 | + if ($this->setting_dump_force_quotes && is_string ($value) && $value !== self::REMPTY) { |
|
| 429 | + $value = '"' . $value . '"'; |
|
| 430 | + } |
|
| 431 | + if (is_numeric($value) && is_string($value)) { |
|
| 432 | + $value = '"' . $value . '"'; |
|
| 433 | + } |
|
| 412 | 434 | } |
| 413 | 435 | |
| 414 | 436 | |
@@ -482,7 +504,9 @@ discard block |
||
| 482 | 504 | } |
| 483 | 505 | |
| 484 | 506 | private function loadWithSource($Source) { |
| 485 | - if (empty ($Source)) return array(); |
|
| 507 | + if (empty ($Source)) { |
|
| 508 | + return array(); |
|
| 509 | + } |
|
| 486 | 510 | if ($this->setting_use_syck_is_possible && function_exists ('syck_load')) { |
| 487 | 511 | $array = syck_load (implode ("\n", $Source)); |
| 488 | 512 | return is_array($array) ? $array : array(); |
@@ -498,8 +522,12 @@ discard block |
||
| 498 | 522 | $this->indent = strlen($line) - strlen(ltrim($line)); |
| 499 | 523 | $tempPath = $this->getParentPathByIndent($this->indent); |
| 500 | 524 | $line = self::stripIndent($line, $this->indent); |
| 501 | - if (self::isComment($line)) continue; |
|
| 502 | - if (self::isEmpty($line)) continue; |
|
| 525 | + if (self::isComment($line)) { |
|
| 526 | + continue; |
|
| 527 | + } |
|
| 528 | + if (self::isEmpty($line)) { |
|
| 529 | + continue; |
|
| 530 | + } |
|
| 503 | 531 | $this->path = $tempPath; |
| 504 | 532 | |
| 505 | 533 | $literalBlockStyle = self::startsLiteralBlock($line); |
@@ -526,13 +554,15 @@ discard block |
||
| 526 | 554 | |
| 527 | 555 | $lineArray = $this->_parseLine($line); |
| 528 | 556 | |
| 529 | - if ($literalBlockStyle) |
|
| 530 | - $lineArray = $this->revertLiteralPlaceHolder ($lineArray, $literalBlock); |
|
| 557 | + if ($literalBlockStyle) { |
|
| 558 | + $lineArray = $this->revertLiteralPlaceHolder ($lineArray, $literalBlock); |
|
| 559 | + } |
|
| 531 | 560 | |
| 532 | 561 | $this->addArray($lineArray, $this->indent); |
| 533 | 562 | |
| 534 | - foreach ($this->delayedPath as $indent => $delayedPath) |
|
| 535 | - $this->path[$indent] = $delayedPath; |
|
| 563 | + foreach ($this->delayedPath as $indent => $delayedPath) { |
|
| 564 | + $this->path[$indent] = $delayedPath; |
|
| 565 | + } |
|
| 536 | 566 | |
| 537 | 567 | $this->delayedPath = array(); |
| 538 | 568 | |
@@ -541,8 +571,9 @@ discard block |
||
| 541 | 571 | } |
| 542 | 572 | |
| 543 | 573 | private function loadFromSource ($input) { |
| 544 | - if (!empty($input) && strpos($input, "\n") === false && file_exists($input)) |
|
| 545 | - $input = file_get_contents($input); |
|
| 574 | + if (!empty($input) && strpos($input, "\n") === false && file_exists($input)) { |
|
| 575 | + $input = file_get_contents($input); |
|
| 576 | + } |
|
| 546 | 577 | |
| 547 | 578 | return $this->loadFromString($input); |
| 548 | 579 | } |
@@ -562,9 +593,13 @@ discard block |
||
| 562 | 593 | * @param string $line A line from the YAML file |
| 563 | 594 | */ |
| 564 | 595 | private function _parseLine($line) { |
| 565 | - if (!$line) return array(); |
|
| 596 | + if (!$line) { |
|
| 597 | + return array(); |
|
| 598 | + } |
|
| 566 | 599 | $line = trim($line); |
| 567 | - if (!$line) return array(); |
|
| 600 | + if (!$line) { |
|
| 601 | + return array(); |
|
| 602 | + } |
|
| 568 | 603 | |
| 569 | 604 | $array = array(); |
| 570 | 605 | |
@@ -574,17 +609,21 @@ discard block |
||
| 574 | 609 | $line = $this->stripGroup ($line, $group); |
| 575 | 610 | } |
| 576 | 611 | |
| 577 | - if ($this->startsMappedSequence($line)) |
|
| 578 | - return $this->returnMappedSequence($line); |
|
| 612 | + if ($this->startsMappedSequence($line)) { |
|
| 613 | + return $this->returnMappedSequence($line); |
|
| 614 | + } |
|
| 579 | 615 | |
| 580 | - if ($this->startsMappedValue($line)) |
|
| 581 | - return $this->returnMappedValue($line); |
|
| 616 | + if ($this->startsMappedValue($line)) { |
|
| 617 | + return $this->returnMappedValue($line); |
|
| 618 | + } |
|
| 582 | 619 | |
| 583 | - if ($this->isArrayElement($line)) |
|
| 584 | - return $this->returnArrayElement($line); |
|
| 620 | + if ($this->isArrayElement($line)) { |
|
| 621 | + return $this->returnArrayElement($line); |
|
| 622 | + } |
|
| 585 | 623 | |
| 586 | - if ($this->isPlainArray($line)) |
|
| 587 | - return $this->returnPlainArray($line); |
|
| 624 | + if ($this->isPlainArray($line)) { |
|
| 625 | + return $this->returnPlainArray($line); |
|
| 626 | + } |
|
| 588 | 627 | |
| 589 | 628 | |
| 590 | 629 | return $this->returnKeyValuePair($line); |
@@ -598,32 +637,44 @@ discard block |
||
| 598 | 637 | * @return mixed |
| 599 | 638 | */ |
| 600 | 639 | private function _toType($value) { |
| 601 | - if ($value === '') return ""; |
|
| 640 | + if ($value === '') { |
|
| 641 | + return ""; |
|
| 642 | + } |
|
| 602 | 643 | $first_character = $value[0]; |
| 603 | 644 | $last_character = substr($value, -1, 1); |
| 604 | 645 | |
| 605 | 646 | $is_quoted = false; |
| 606 | 647 | do { |
| 607 | - if (!$value) break; |
|
| 608 | - if ($first_character != '"' && $first_character != "'") break; |
|
| 609 | - if ($last_character != '"' && $last_character != "'") break; |
|
| 648 | + if (!$value) { |
|
| 649 | + break; |
|
| 650 | + } |
|
| 651 | + if ($first_character != '"' && $first_character != "'") { |
|
| 652 | + break; |
|
| 653 | + } |
|
| 654 | + if ($last_character != '"' && $last_character != "'") { |
|
| 655 | + break; |
|
| 656 | + } |
|
| 610 | 657 | $is_quoted = true; |
| 611 | 658 | } while (0); |
| 612 | 659 | |
| 613 | 660 | if ($is_quoted) { |
| 614 | 661 | $value = str_replace('\n', "\n", $value); |
| 615 | - if ($first_character == "'") |
|
| 616 | - return strtr(substr ($value, 1, -1), array ('\'\'' => '\'', '\\\''=> '\'')); |
|
| 662 | + if ($first_character == "'") { |
|
| 663 | + return strtr(substr ($value, 1, -1), array ('\'\'' => '\'', '\\\''=> '\'')); |
|
| 664 | + } |
|
| 617 | 665 | return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\\\''=> '\'')); |
| 618 | 666 | } |
| 619 | 667 | |
| 620 | - if (strpos($value, ' #') !== false && !$is_quoted) |
|
| 621 | - $value = preg_replace('/\s+#(.+)$/','',$value); |
|
| 668 | + if (strpos($value, ' #') !== false && !$is_quoted) { |
|
| 669 | + $value = preg_replace('/\s+#(.+)$/','',$value); |
|
| 670 | + } |
|
| 622 | 671 | |
| 623 | 672 | if ($first_character == '[' && $last_character == ']') { |
| 624 | 673 | // Take out strings sequences and mappings |
| 625 | 674 | $innerValue = trim(substr ($value, 1, -1)); |
| 626 | - if ($innerValue === '') return array(); |
|
| 675 | + if ($innerValue === '') { |
|
| 676 | + return array(); |
|
| 677 | + } |
|
| 627 | 678 | $explode = $this->_inlineEscape($innerValue); |
| 628 | 679 | // Propagate value array |
| 629 | 680 | $value = array(); |
@@ -644,7 +695,9 @@ discard block |
||
| 644 | 695 | |
| 645 | 696 | if ($first_character == '{' && $last_character == '}') { |
| 646 | 697 | $innerValue = trim(substr ($value, 1, -1)); |
| 647 | - if ($innerValue === '') return array(); |
|
| 698 | + if ($innerValue === '') { |
|
| 699 | + return array(); |
|
| 700 | + } |
|
| 648 | 701 | // Inline Mapping |
| 649 | 702 | // Take out strings sequences and mappings |
| 650 | 703 | $explode = $this->_inlineEscape($innerValue); |
@@ -652,7 +705,9 @@ discard block |
||
| 652 | 705 | $array = array(); |
| 653 | 706 | foreach ($explode as $v) { |
| 654 | 707 | $SubArr = $this->_toType($v); |
| 655 | - if (empty($SubArr)) continue; |
|
| 708 | + if (empty($SubArr)) { |
|
| 709 | + continue; |
|
| 710 | + } |
|
| 656 | 711 | if (is_array ($SubArr)) { |
| 657 | 712 | $array[key($SubArr)] = $SubArr[key($SubArr)]; continue; |
| 658 | 713 | } |
@@ -667,8 +722,9 @@ discard block |
||
| 667 | 722 | |
| 668 | 723 | if ( is_numeric($value) && preg_match ('/^(-|)[1-9]+[0-9]*$/', $value) ){ |
| 669 | 724 | $intvalue = (int)$value; |
| 670 | - if ($intvalue != PHP_INT_MAX && $intvalue != ~PHP_INT_MAX) |
|
| 671 | - $value = $intvalue; |
|
| 725 | + if ($intvalue != PHP_INT_MAX && $intvalue != ~PHP_INT_MAX) { |
|
| 726 | + $value = $intvalue; |
|
| 727 | + } |
|
| 672 | 728 | return $value; |
| 673 | 729 | } |
| 674 | 730 | |
@@ -680,9 +736,12 @@ discard block |
||
| 680 | 736 | $this->coerceValue($value); |
| 681 | 737 | |
| 682 | 738 | if (is_numeric($value)) { |
| 683 | - if ($value === '0') return 0; |
|
| 684 | - if (rtrim ($value, 0) === $value) |
|
| 685 | - $value = (float)$value; |
|
| 739 | + if ($value === '0') { |
|
| 740 | + return 0; |
|
| 741 | + } |
|
| 742 | + if (rtrim ($value, 0) === $value) { |
|
| 743 | + $value = (float)$value; |
|
| 744 | + } |
|
| 686 | 745 | return $value; |
| 687 | 746 | } |
| 688 | 747 | |
@@ -738,7 +797,9 @@ discard block |
||
| 738 | 797 | $inline = preg_replace('/{([^\[\]{}]+)}/U', ('YAMLMap' . (count($maps) - 1) . 's'), $inline, 1); |
| 739 | 798 | } |
| 740 | 799 | |
| 741 | - if ($i++ >= 10) break; |
|
| 800 | + if ($i++ >= 10) { |
|
| 801 | + break; |
|
| 802 | + } |
|
| 742 | 803 | |
| 743 | 804 | } while (strpos ($inline, '[') !== false || strpos ($inline, '{') !== false); |
| 744 | 805 | |
@@ -811,11 +872,15 @@ discard block |
||
| 811 | 872 | $finished = false; break; |
| 812 | 873 | } |
| 813 | 874 | } |
| 814 | - if ($finished) break; |
|
| 875 | + if ($finished) { |
|
| 876 | + break; |
|
| 877 | + } |
|
| 815 | 878 | |
| 816 | 879 | $i++; |
| 817 | - if ($i > 10) |
|
| 818 | - break; // Prevent infinite loops. |
|
| 880 | + if ($i > 10) { |
|
| 881 | + break; |
|
| 882 | + } |
|
| 883 | + // Prevent infinite loops. |
|
| 819 | 884 | } |
| 820 | 885 | |
| 821 | 886 | |
@@ -823,8 +888,12 @@ discard block |
||
| 823 | 888 | } |
| 824 | 889 | |
| 825 | 890 | private function literalBlockContinues ($line, $lineIndent) { |
| 826 | - if (!trim($line)) return true; |
|
| 827 | - if (strlen($line) - strlen(ltrim($line)) > $lineIndent) return true; |
|
| 891 | + if (!trim($line)) { |
|
| 892 | + return true; |
|
| 893 | + } |
|
| 894 | + if (strlen($line) - strlen(ltrim($line)) > $lineIndent) { |
|
| 895 | + return true; |
|
| 896 | + } |
|
| 828 | 897 | return false; |
| 829 | 898 | } |
| 830 | 899 | |
@@ -842,7 +911,9 @@ discard block |
||
| 842 | 911 | |
| 843 | 912 | private function addArrayInline ($array, $indent) { |
| 844 | 913 | $CommonGroupPath = $this->path; |
| 845 | - if (empty ($array)) return false; |
|
| 914 | + if (empty ($array)) { |
|
| 915 | + return false; |
|
| 916 | + } |
|
| 846 | 917 | |
| 847 | 918 | foreach ($array as $k => $_) { |
| 848 | 919 | $this->addArray(array($k => $_), $indent); |
@@ -855,12 +926,15 @@ discard block |
||
| 855 | 926 | |
| 856 | 927 | // print_r ($incoming_data); |
| 857 | 928 | |
| 858 | - if (count ($incoming_data) > 1) |
|
| 859 | - return $this->addArrayInline ($incoming_data, $incoming_indent); |
|
| 929 | + if (count ($incoming_data) > 1) { |
|
| 930 | + return $this->addArrayInline ($incoming_data, $incoming_indent); |
|
| 931 | + } |
|
| 860 | 932 | |
| 861 | 933 | $key = key ($incoming_data); |
| 862 | 934 | $value = isset($incoming_data[$key]) ? $incoming_data[$key] : null; |
| 863 | - if ($key === '__!YAMLZero') $key = '0'; |
|
| 935 | + if ($key === '__!YAMLZero') { |
|
| 936 | + $key = '0'; |
|
| 937 | + } |
|
| 864 | 938 | |
| 865 | 939 | if ($incoming_indent == 0 && !$this->_containsGroupAlias && !$this->_containsGroupAnchor) { // Shortcut for root-level values. |
| 866 | 940 | if ($key || $key === '' || $key === '0') { |
@@ -893,13 +967,13 @@ discard block |
||
| 893 | 967 | |
| 894 | 968 | $_arr = array_merge ($_arr, $value); |
| 895 | 969 | } else if ($key || $key === '' || $key === '0') { |
| 896 | - if (!is_array ($_arr)) |
|
| 897 | - $_arr = array ($key=>$value); |
|
| 898 | - else |
|
| 899 | - $_arr[$key] = $value; |
|
| 970 | + if (!is_array ($_arr)) { |
|
| 971 | + $_arr = array ($key=>$value); |
|
| 972 | + } else { |
|
| 973 | + $_arr[$key] = $value; |
|
| 974 | + } |
|
| 900 | 975 | } else { |
| 901 | - if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; } |
|
| 902 | - else { $_arr[] = $value; end ($_arr); $key = key ($_arr); } |
|
| 976 | + if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; } else { $_arr[] = $value; end ($_arr); $key = key ($_arr); } |
|
| 903 | 977 | } |
| 904 | 978 | |
| 905 | 979 | $reverse_path = array_reverse($this->path); |
@@ -928,19 +1002,33 @@ discard block |
||
| 928 | 1002 | |
| 929 | 1003 | private static function startsLiteralBlock ($line) { |
| 930 | 1004 | $lastChar = substr (trim($line), -1); |
| 931 | - if ($lastChar != '>' && $lastChar != '|') return false; |
|
| 932 | - if ($lastChar == '|') return $lastChar; |
|
| 1005 | + if ($lastChar != '>' && $lastChar != '|') { |
|
| 1006 | + return false; |
|
| 1007 | + } |
|
| 1008 | + if ($lastChar == '|') { |
|
| 1009 | + return $lastChar; |
|
| 1010 | + } |
|
| 933 | 1011 | // HTML tags should not be counted as literal blocks. |
| 934 | - if (preg_match ('#<.*?>$#', $line)) return false; |
|
| 1012 | + if (preg_match ('#<.*?>$#', $line)) { |
|
| 1013 | + return false; |
|
| 1014 | + } |
|
| 935 | 1015 | return $lastChar; |
| 936 | 1016 | } |
| 937 | 1017 | |
| 938 | 1018 | private static function greedilyNeedNextLine($line) { |
| 939 | 1019 | $line = trim ($line); |
| 940 | - if (!strlen($line)) return false; |
|
| 941 | - if (substr ($line, -1, 1) == ']') return false; |
|
| 942 | - if ($line[0] == '[') return true; |
|
| 943 | - if (preg_match ('#^[^:]+?:\s*\[#', $line)) return true; |
|
| 1020 | + if (!strlen($line)) { |
|
| 1021 | + return false; |
|
| 1022 | + } |
|
| 1023 | + if (substr ($line, -1, 1) == ']') { |
|
| 1024 | + return false; |
|
| 1025 | + } |
|
| 1026 | + if ($line[0] == '[') { |
|
| 1027 | + return true; |
|
| 1028 | + } |
|
| 1029 | + if (preg_match ('#^[^:]+?:\s*\[#', $line)) { |
|
| 1030 | + return true; |
|
| 1031 | + } |
|
| 944 | 1032 | return false; |
| 945 | 1033 | } |
| 946 | 1034 | |
@@ -953,37 +1041,46 @@ discard block |
||
| 953 | 1041 | if ($literalBlockStyle == '|') { |
| 954 | 1042 | return $literalBlock . $line; |
| 955 | 1043 | } |
| 956 | - if (strlen($line) == 0) |
|
| 957 | - return rtrim($literalBlock, ' ') . "\n"; |
|
| 1044 | + if (strlen($line) == 0) { |
|
| 1045 | + return rtrim($literalBlock, ' ') . "\n"; |
|
| 1046 | + } |
|
| 958 | 1047 | if ($line == "\n" && $literalBlockStyle == '>') { |
| 959 | 1048 | return rtrim ($literalBlock, " \t") . "\n"; |
| 960 | 1049 | } |
| 961 | - if ($line != "\n") |
|
| 962 | - $line = trim ($line, "\r\n ") . " "; |
|
| 1050 | + if ($line != "\n") { |
|
| 1051 | + $line = trim ($line, "\r\n ") . " "; |
|
| 1052 | + } |
|
| 963 | 1053 | return $literalBlock . $line; |
| 964 | 1054 | } |
| 965 | 1055 | |
| 966 | 1056 | function revertLiteralPlaceHolder ($lineArray, $literalBlock) { |
| 967 | 1057 | foreach ($lineArray as $k => $_) { |
| 968 | - if (is_array($_)) |
|
| 969 | - $lineArray[$k] = $this->revertLiteralPlaceHolder ($_, $literalBlock); |
|
| 970 | - else if (substr($_, -1 * strlen ($this->LiteralPlaceHolder)) == $this->LiteralPlaceHolder) |
|
| 971 | - $lineArray[$k] = rtrim ($literalBlock, " \r\n"); |
|
| 1058 | + if (is_array($_)) { |
|
| 1059 | + $lineArray[$k] = $this->revertLiteralPlaceHolder ($_, $literalBlock); |
|
| 1060 | + } else if (substr($_, -1 * strlen ($this->LiteralPlaceHolder)) == $this->LiteralPlaceHolder) { |
|
| 1061 | + $lineArray[$k] = rtrim ($literalBlock, " \r\n"); |
|
| 1062 | + } |
|
| 972 | 1063 | } |
| 973 | 1064 | return $lineArray; |
| 974 | 1065 | } |
| 975 | 1066 | |
| 976 | 1067 | private static function stripIndent ($line, $indent = -1) { |
| 977 | - if ($indent == -1) $indent = strlen($line) - strlen(ltrim($line)); |
|
| 1068 | + if ($indent == -1) { |
|
| 1069 | + $indent = strlen($line) - strlen(ltrim($line)); |
|
| 1070 | + } |
|
| 978 | 1071 | return substr ($line, $indent); |
| 979 | 1072 | } |
| 980 | 1073 | |
| 981 | 1074 | private function getParentPathByIndent ($indent) { |
| 982 | - if ($indent == 0) return array(); |
|
| 1075 | + if ($indent == 0) { |
|
| 1076 | + return array(); |
|
| 1077 | + } |
|
| 983 | 1078 | $linePath = $this->path; |
| 984 | 1079 | do { |
| 985 | 1080 | end($linePath); $lastIndentInParentPath = key($linePath); |
| 986 | - if ($indent <= $lastIndentInParentPath) array_pop ($linePath); |
|
| 1081 | + if ($indent <= $lastIndentInParentPath) { |
|
| 1082 | + array_pop ($linePath); |
|
| 1083 | + } |
|
| 987 | 1084 | } while ($indent <= $lastIndentInParentPath); |
| 988 | 1085 | return $linePath; |
| 989 | 1086 | } |
@@ -992,11 +1089,17 @@ discard block |
||
| 992 | 1089 | private function clearBiggerPathValues ($indent) { |
| 993 | 1090 | |
| 994 | 1091 | |
| 995 | - if ($indent == 0) $this->path = array(); |
|
| 996 | - if (empty ($this->path)) return true; |
|
| 1092 | + if ($indent == 0) { |
|
| 1093 | + $this->path = array(); |
|
| 1094 | + } |
|
| 1095 | + if (empty ($this->path)) { |
|
| 1096 | + return true; |
|
| 1097 | + } |
|
| 997 | 1098 | |
| 998 | 1099 | foreach ($this->path as $k => $_) { |
| 999 | - if ($k > $indent) unset ($this->path[$k]); |
|
| 1100 | + if ($k > $indent) { |
|
| 1101 | + unset ($this->path[$k]); |
|
| 1102 | + } |
|
| 1000 | 1103 | } |
| 1001 | 1104 | |
| 1002 | 1105 | return true; |
@@ -1004,9 +1107,15 @@ discard block |
||
| 1004 | 1107 | |
| 1005 | 1108 | |
| 1006 | 1109 | private static function isComment ($line) { |
| 1007 | - if (!$line) return false; |
|
| 1008 | - if ($line[0] == '#') return true; |
|
| 1009 | - if (trim($line, " \r\n\t") == '---') return true; |
|
| 1110 | + if (!$line) { |
|
| 1111 | + return false; |
|
| 1112 | + } |
|
| 1113 | + if ($line[0] == '#') { |
|
| 1114 | + return true; |
|
| 1115 | + } |
|
| 1116 | + if (trim($line, " \r\n\t") == '---') { |
|
| 1117 | + return true; |
|
| 1118 | + } |
|
| 1010 | 1119 | return false; |
| 1011 | 1120 | } |
| 1012 | 1121 | |
@@ -1016,10 +1125,15 @@ discard block |
||
| 1016 | 1125 | |
| 1017 | 1126 | |
| 1018 | 1127 | private function isArrayElement ($line) { |
| 1019 | - if (!$line || !is_scalar($line)) return false; |
|
| 1020 | - if (substr($line, 0, 2) != '- ') return false; |
|
| 1021 | - if (strlen ($line) > 3) |
|
| 1022 | - if (substr($line,0,3) == '---') return false; |
|
| 1128 | + if (!$line || !is_scalar($line)) { |
|
| 1129 | + return false; |
|
| 1130 | + } |
|
| 1131 | + if (substr($line, 0, 2) != '- ') { |
|
| 1132 | + return false; |
|
| 1133 | + } |
|
| 1134 | + if (strlen ($line) > 3) { |
|
| 1135 | + if (substr($line,0,3) == '---') return false; |
|
| 1136 | + } |
|
| 1023 | 1137 | |
| 1024 | 1138 | return true; |
| 1025 | 1139 | } |
@@ -1029,17 +1143,29 @@ discard block |
||
| 1029 | 1143 | } |
| 1030 | 1144 | |
| 1031 | 1145 | private function isLiteral ($line) { |
| 1032 | - if ($this->isArrayElement($line)) return false; |
|
| 1033 | - if ($this->isHashElement($line)) return false; |
|
| 1146 | + if ($this->isArrayElement($line)) { |
|
| 1147 | + return false; |
|
| 1148 | + } |
|
| 1149 | + if ($this->isHashElement($line)) { |
|
| 1150 | + return false; |
|
| 1151 | + } |
|
| 1034 | 1152 | return true; |
| 1035 | 1153 | } |
| 1036 | 1154 | |
| 1037 | 1155 | |
| 1038 | 1156 | private static function unquote ($value) { |
| 1039 | - if (!$value) return $value; |
|
| 1040 | - if (!is_string($value)) return $value; |
|
| 1041 | - if ($value[0] == '\'') return trim ($value, '\''); |
|
| 1042 | - if ($value[0] == '"') return trim ($value, '"'); |
|
| 1157 | + if (!$value) { |
|
| 1158 | + return $value; |
|
| 1159 | + } |
|
| 1160 | + if (!is_string($value)) { |
|
| 1161 | + return $value; |
|
| 1162 | + } |
|
| 1163 | + if ($value[0] == '\'') { |
|
| 1164 | + return trim ($value, '\''); |
|
| 1165 | + } |
|
| 1166 | + if ($value[0] == '"') { |
|
| 1167 | + return trim ($value, '"'); |
|
| 1168 | + } |
|
| 1043 | 1169 | return $value; |
| 1044 | 1170 | } |
| 1045 | 1171 | |
@@ -1101,7 +1227,9 @@ discard block |
||
| 1101 | 1227 | } |
| 1102 | 1228 | // Set the type of the value. Int, string, etc |
| 1103 | 1229 | $value = $this->_toType($value); |
| 1104 | - if ($key === '0') $key = '__!YAMLZero'; |
|
| 1230 | + if ($key === '0') { |
|
| 1231 | + $key = '__!YAMLZero'; |
|
| 1232 | + } |
|
| 1105 | 1233 | $array[$key] = $value; |
| 1106 | 1234 | } else { |
| 1107 | 1235 | $array = array ($line); |
@@ -1112,7 +1240,10 @@ discard block |
||
| 1112 | 1240 | |
| 1113 | 1241 | |
| 1114 | 1242 | private function returnArrayElement ($line) { |
| 1115 | - if (strlen($line) <= 1) return array(array()); // Weird %) |
|
| 1243 | + if (strlen($line) <= 1) { |
|
| 1244 | + return array(array()); |
|
| 1245 | + } |
|
| 1246 | + // Weird %) |
|
| 1116 | 1247 | $array = array(); |
| 1117 | 1248 | $value = trim(substr($line,1)); |
| 1118 | 1249 | $value = $this->_toType($value); |
@@ -1126,19 +1257,36 @@ discard block |
||
| 1126 | 1257 | |
| 1127 | 1258 | private function nodeContainsGroup ($line) { |
| 1128 | 1259 | $symbolsForReference = 'A-z0-9_\-'; |
| 1129 | - if (strpos($line, '&') === false && strpos($line, '*') === false) return false; // Please die fast ;-) |
|
| 1130 | - if ($line[0] == '&' && preg_match('/^(&['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1]; |
|
| 1131 | - if ($line[0] == '*' && preg_match('/^(\*['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1]; |
|
| 1132 | - if (preg_match('/(&['.$symbolsForReference.']+)$/', $line, $matches)) return $matches[1]; |
|
| 1133 | - if (preg_match('/(\*['.$symbolsForReference.']+$)/', $line, $matches)) return $matches[1]; |
|
| 1134 | - if (preg_match ('#^\s*<<\s*:\s*(\*[^\s]+).*$#', $line, $matches)) return $matches[1]; |
|
| 1260 | + if (strpos($line, '&') === false && strpos($line, '*') === false) { |
|
| 1261 | + return false; |
|
| 1262 | + } |
|
| 1263 | + // Please die fast ;-) |
|
| 1264 | + if ($line[0] == '&' && preg_match('/^(&['.$symbolsForReference.']+)/', $line, $matches)) { |
|
| 1265 | + return $matches[1]; |
|
| 1266 | + } |
|
| 1267 | + if ($line[0] == '*' && preg_match('/^(\*['.$symbolsForReference.']+)/', $line, $matches)) { |
|
| 1268 | + return $matches[1]; |
|
| 1269 | + } |
|
| 1270 | + if (preg_match('/(&['.$symbolsForReference.']+)$/', $line, $matches)) { |
|
| 1271 | + return $matches[1]; |
|
| 1272 | + } |
|
| 1273 | + if (preg_match('/(\*['.$symbolsForReference.']+$)/', $line, $matches)) { |
|
| 1274 | + return $matches[1]; |
|
| 1275 | + } |
|
| 1276 | + if (preg_match ('#^\s*<<\s*:\s*(\*[^\s]+).*$#', $line, $matches)) { |
|
| 1277 | + return $matches[1]; |
|
| 1278 | + } |
|
| 1135 | 1279 | return false; |
| 1136 | 1280 | |
| 1137 | 1281 | } |
| 1138 | 1282 | |
| 1139 | 1283 | private function addGroup ($line, $group) { |
| 1140 | - if ($group[0] == '&') $this->_containsGroupAnchor = substr ($group, 1); |
|
| 1141 | - if ($group[0] == '*') $this->_containsGroupAlias = substr ($group, 1); |
|
| 1284 | + if ($group[0] == '&') { |
|
| 1285 | + $this->_containsGroupAnchor = substr ($group, 1); |
|
| 1286 | + } |
|
| 1287 | + if ($group[0] == '*') { |
|
| 1288 | + $this->_containsGroupAlias = substr ($group, 1); |
|
| 1289 | + } |
|
| 1142 | 1290 | //print_r ($this->path); |
| 1143 | 1291 | } |
| 1144 | 1292 | |
@@ -1153,9 +1301,15 @@ discard block |
||
| 1153 | 1301 | // The syntax is the following: php Spyc.php spyc.yaml |
| 1154 | 1302 | |
| 1155 | 1303 | do { |
| 1156 | - if (PHP_SAPI != 'cli') break; |
|
| 1157 | - if (empty ($_SERVER['argc']) || $_SERVER['argc'] < 2) break; |
|
| 1158 | - if (empty ($_SERVER['PHP_SELF']) || FALSE === strpos ($_SERVER['PHP_SELF'], 'Spyc.php') ) break; |
|
| 1304 | + if (PHP_SAPI != 'cli') { |
|
| 1305 | + break; |
|
| 1306 | + } |
|
| 1307 | + if (empty ($_SERVER['argc']) || $_SERVER['argc'] < 2) { |
|
| 1308 | + break; |
|
| 1309 | + } |
|
| 1310 | + if (empty ($_SERVER['PHP_SELF']) || FALSE === strpos ($_SERVER['PHP_SELF'], 'Spyc.php') ) { |
|
| 1311 | + break; |
|
| 1312 | + } |
|
| 1159 | 1313 | $file = $argv[1]; |
| 1160 | 1314 | echo json_encode (spyc_load_file ($file)); |
| 1161 | 1315 | } while (0); |