Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. <?php
  2.  
  3. protected static function _decodeYaml($currentIndent, $lines)
  4. {
  5. $config = array();
  6. $inIndent = false;
  7. foreach ($lines as $n => $line) {
  8. $lineno = $n + 1;
  9.  
  10. $line = rtrim(preg_replace("/#.*$/", "", $line));
  11. if (strlen($line) == 0) {
  12. continue;
  13. }
  14.  
  15. $indent = strspn($line, " ");
  16.  
  17. // line without the spaces
  18. $line = trim($line);
  19. if (strlen($line) == 0) {
  20. continue;
  21. }
  22.  
  23. if ($indent < $currentIndent) {
  24. // this level is done
  25. return $config;
  26. }
  27.  
  28. if (!$inIndent) {
  29. $currentIndent = $indent;
  30. $inIndent = true;
  31. }
  32.  
  33. if (preg_match("/(?!-)([\w\-]+):\s*(.*)/", $line, $m)) {
  34. // key: value
  35. if (strlen($m[2])) {
  36. // simple key: value
  37. $value = preg_replace("/#.*$/", "", $m[2]);
  38. $value = self::_parseValue($value);
  39. } else {
  40. // key: and then values on new lines
  41. $value = self::_decodeYaml($currentIndent + 1, array_slice($lines, $n+1, count($lines)-1));
  42. if (is_array($value) && !count($value)) {
  43. $value = "";
  44. }
  45. }
  46. $config[$m[1]] = $value;
  47. } elseif ($line[0] == "-") {
  48. // item in the list:
  49. // - FOO
  50. if (strlen($line) > 2) {
  51. $value = substr($line, 2);
  52.  
  53. $config[] = self::_parseValue($value);
  54. } else {
  55. $config[] = self::_decodeYaml($currentIndent + 1, array_slice($lines, $n+1, count($lines)-1));
  56. }
  57. } else {
  58. require_once 'Zend/Config/Exception.php';
  59. throw new Zend_Config_Exception(sprintf(
  60. 'Error parsing YAML at line %d - unsupported syntax: "%s"',
  61. $lineno, $line
  62. ));
  63. }
  64. }
  65. return $config;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement