php4
2 years ago
COPYING
2 years ago
Spyc.php
2 years ago
phpunit.xml
2 years ago
spyc.yaml
2 years ago
Spyc.php
1261 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED; |
| 4 | |
| 5 | /** |
| 6 | * Spyc -- A Simple PHP YAML Class |
| 7 | * @version 0.6.2 |
| 8 | * @author Vlad Andersen <vlad.andersen@gmail.com> |
| 9 | * @author Chris Wanstrath <chris@ozmm.org> |
| 10 | * @link https://github.com/mustangostang/spyc/ |
| 11 | * @copyright Copyright 2005-2006 Chris Wanstrath, 2006-2011 Vlad Andersen |
| 12 | * @license http://www.opensource.org/licenses/mit-license.php MIT License |
| 13 | * @package Spyc |
| 14 | */ |
| 15 | if (!\function_exists('IAWPSCOPED\\spyc_load')) { |
| 16 | /** |
| 17 | * Parses YAML to array. |
| 18 | * @param string $string YAML string. |
| 19 | * @return array |
| 20 | * @internal |
| 21 | */ |
| 22 | function spyc_load($string) |
| 23 | { |
| 24 | return Spyc::YAMLLoadString($string); |
| 25 | } |
| 26 | } |
| 27 | if (!\function_exists('IAWPSCOPED\\spyc_load_file')) { |
| 28 | /** |
| 29 | * Parses YAML to array. |
| 30 | * @param string $file Path to YAML file. |
| 31 | * @return array |
| 32 | * @internal |
| 33 | */ |
| 34 | function spyc_load_file($file) |
| 35 | { |
| 36 | return Spyc::YAMLLoad($file); |
| 37 | } |
| 38 | } |
| 39 | if (!\function_exists('IAWPSCOPED\\spyc_dump')) { |
| 40 | /** |
| 41 | * Dumps array to YAML. |
| 42 | * @param array $data Array. |
| 43 | * @return string |
| 44 | * @internal |
| 45 | */ |
| 46 | function spyc_dump($data) |
| 47 | { |
| 48 | return Spyc::YAMLDump($data, \false, \false, \true); |
| 49 | } |
| 50 | } |
| 51 | if (!\class_exists('IAWPSCOPED\\Spyc')) { |
| 52 | /** |
| 53 | * The Simple PHP YAML Class. |
| 54 | * |
| 55 | * This class can be used to read a YAML file and convert its contents |
| 56 | * into a PHP array. It currently supports a very limited subsection of |
| 57 | * the YAML spec. |
| 58 | * |
| 59 | * Usage: |
| 60 | * <code> |
| 61 | * $Spyc = new Spyc; |
| 62 | * $array = $Spyc->load($file); |
| 63 | * </code> |
| 64 | * or: |
| 65 | * <code> |
| 66 | * $array = Spyc::YAMLLoad($file); |
| 67 | * </code> |
| 68 | * or: |
| 69 | * <code> |
| 70 | * $array = spyc_load_file($file); |
| 71 | * </code> |
| 72 | * @package Spyc |
| 73 | * @internal |
| 74 | */ |
| 75 | class Spyc |
| 76 | { |
| 77 | // SETTINGS |
| 78 | const REMPTY = "\x00\x00\x00\x00\x00"; |
| 79 | /** |
| 80 | * Setting this to true will force YAMLDump to enclose any string value in |
| 81 | * quotes. False by default. |
| 82 | * |
| 83 | * @var bool |
| 84 | */ |
| 85 | public $setting_dump_force_quotes = \false; |
| 86 | /** |
| 87 | * Setting this to true will forse YAMLLoad to use syck_load function when |
| 88 | * possible. False by default. |
| 89 | * @var bool |
| 90 | */ |
| 91 | public $setting_use_syck_is_possible = \false; |
| 92 | /** |
| 93 | * Setting this to true will forse YAMLLoad to use syck_load function when |
| 94 | * possible. False by default. |
| 95 | * @var bool |
| 96 | */ |
| 97 | public $setting_empty_hash_as_object = \false; |
| 98 | /**#@+ |
| 99 | * @access private |
| 100 | * @var mixed |
| 101 | */ |
| 102 | private $_dumpIndent; |
| 103 | private $_dumpWordWrap; |
| 104 | private $_containsGroupAnchor = \false; |
| 105 | private $_containsGroupAlias = \false; |
| 106 | private $path; |
| 107 | private $result; |
| 108 | private $LiteralPlaceHolder = '___YAML_Literal_Block___'; |
| 109 | private $SavedGroups = array(); |
| 110 | private $indent; |
| 111 | /** |
| 112 | * Path modifier that should be applied after adding current element. |
| 113 | * @var array |
| 114 | */ |
| 115 | private $delayedPath = array(); |
| 116 | /**#@+ |
| 117 | * @access public |
| 118 | * @var mixed |
| 119 | */ |
| 120 | public $_nodeId; |
| 121 | /** |
| 122 | * Load a valid YAML string to Spyc. |
| 123 | * @param string $input |
| 124 | * @return array |
| 125 | */ |
| 126 | public function load($input) |
| 127 | { |
| 128 | return $this->_loadString($input); |
| 129 | } |
| 130 | /** |
| 131 | * Load a valid YAML file to Spyc. |
| 132 | * @param string $file |
| 133 | * @return array |
| 134 | */ |
| 135 | public function loadFile($file) |
| 136 | { |
| 137 | return $this->_load($file); |
| 138 | } |
| 139 | /** |
| 140 | * Load YAML into a PHP array statically |
| 141 | * |
| 142 | * The load method, when supplied with a YAML stream (string or file), |
| 143 | * will do its best to convert YAML in a file into a PHP array. Pretty |
| 144 | * simple. |
| 145 | * Usage: |
| 146 | * <code> |
| 147 | * $array = Spyc::YAMLLoad('lucky.yaml'); |
| 148 | * print_r($array); |
| 149 | * </code> |
| 150 | * @access public |
| 151 | * @return array |
| 152 | * @param string $input Path of YAML file or string containing YAML |
| 153 | * @param array set options |
| 154 | */ |
| 155 | public static function YAMLLoad($input, $options = []) |
| 156 | { |
| 157 | $Spyc = new Spyc(); |
| 158 | foreach ($options as $key => $value) { |
| 159 | if (\property_exists($Spyc, $key)) { |
| 160 | $Spyc->{$key} = $value; |
| 161 | } |
| 162 | } |
| 163 | return $Spyc->_load($input); |
| 164 | } |
| 165 | /** |
| 166 | * Load a string of YAML into a PHP array statically |
| 167 | * |
| 168 | * The load method, when supplied with a YAML string, will do its best |
| 169 | * to convert YAML in a string into a PHP array. Pretty simple. |
| 170 | * |
| 171 | * Note: use this function if you don't want files from the file system |
| 172 | * loaded and processed as YAML. This is of interest to people concerned |
| 173 | * about security whose input is from a string. |
| 174 | * |
| 175 | * Usage: |
| 176 | * <code> |
| 177 | * $array = Spyc::YAMLLoadString("---\n0: hello world\n"); |
| 178 | * print_r($array); |
| 179 | * </code> |
| 180 | * @access public |
| 181 | * @return array |
| 182 | * @param string $input String containing YAML |
| 183 | * @param array set options |
| 184 | */ |
| 185 | public static function YAMLLoadString($input, $options = []) |
| 186 | { |
| 187 | $Spyc = new Spyc(); |
| 188 | foreach ($options as $key => $value) { |
| 189 | if (\property_exists($Spyc, $key)) { |
| 190 | $Spyc->{$key} = $value; |
| 191 | } |
| 192 | } |
| 193 | return $Spyc->_loadString($input); |
| 194 | } |
| 195 | /** |
| 196 | * Dump YAML from PHP array statically |
| 197 | * |
| 198 | * The dump method, when supplied with an array, will do its best |
| 199 | * to convert the array into friendly YAML. Pretty simple. Feel free to |
| 200 | * save the returned string as nothing.yaml and pass it around. |
| 201 | * |
| 202 | * Oh, and you can decide how big the indent is and what the wordwrap |
| 203 | * for folding is. Pretty cool -- just pass in 'false' for either if |
| 204 | * you want to use the default. |
| 205 | * |
| 206 | * Indent's default is 2 spaces, wordwrap's default is 40 characters. And |
| 207 | * you can turn off wordwrap by passing in 0. |
| 208 | * |
| 209 | * @access public |
| 210 | * @return string |
| 211 | * @param array|\stdClass $array PHP array |
| 212 | * @param int $indent Pass in false to use the default, which is 2 |
| 213 | * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40) |
| 214 | * @param bool $no_opening_dashes Do not start YAML file with "---\n" |
| 215 | */ |
| 216 | public static function YAMLDump($array, $indent = \false, $wordwrap = \false, $no_opening_dashes = \false) |
| 217 | { |
| 218 | $spyc = new Spyc(); |
| 219 | return $spyc->dump($array, $indent, $wordwrap, $no_opening_dashes); |
| 220 | } |
| 221 | /** |
| 222 | * Dump PHP array to YAML |
| 223 | * |
| 224 | * The dump method, when supplied with an array, will do its best |
| 225 | * to convert the array into friendly YAML. Pretty simple. Feel free to |
| 226 | * save the returned string as tasteful.yaml and pass it around. |
| 227 | * |
| 228 | * Oh, and you can decide how big the indent is and what the wordwrap |
| 229 | * for folding is. Pretty cool -- just pass in 'false' for either if |
| 230 | * you want to use the default. |
| 231 | * |
| 232 | * Indent's default is 2 spaces, wordwrap's default is 40 characters. And |
| 233 | * you can turn off wordwrap by passing in 0. |
| 234 | * |
| 235 | * @access public |
| 236 | * @return string |
| 237 | * @param array $array PHP array |
| 238 | * @param int $indent Pass in false to use the default, which is 2 |
| 239 | * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40) |
| 240 | */ |
| 241 | public function dump($array, $indent = \false, $wordwrap = \false, $no_opening_dashes = \false) |
| 242 | { |
| 243 | // Dumps to some very clean YAML. We'll have to add some more features |
| 244 | // and options soon. And better support for folding. |
| 245 | // New features and options. |
| 246 | if ($indent === \false or !\is_numeric($indent)) { |
| 247 | $this->_dumpIndent = 2; |
| 248 | } else { |
| 249 | $this->_dumpIndent = $indent; |
| 250 | } |
| 251 | if ($wordwrap === \false or !\is_numeric($wordwrap)) { |
| 252 | $this->_dumpWordWrap = 40; |
| 253 | } else { |
| 254 | $this->_dumpWordWrap = $wordwrap; |
| 255 | } |
| 256 | // New YAML document |
| 257 | $string = ""; |
| 258 | if (!$no_opening_dashes) { |
| 259 | $string = "---\n"; |
| 260 | } |
| 261 | // Start at the base of the array and move through it. |
| 262 | if ($array) { |
| 263 | $array = (array) $array; |
| 264 | $previous_key = -1; |
| 265 | foreach ($array as $key => $value) { |
| 266 | if (!isset($first_key)) { |
| 267 | $first_key = $key; |
| 268 | } |
| 269 | $string .= $this->_yamlize($key, $value, 0, $previous_key, $first_key, $array); |
| 270 | $previous_key = $key; |
| 271 | } |
| 272 | } |
| 273 | return $string; |
| 274 | } |
| 275 | /** |
| 276 | * Attempts to convert a key / value array item to YAML |
| 277 | * @access private |
| 278 | * @return string |
| 279 | * @param $key The name of the key |
| 280 | * @param $value The value of the item |
| 281 | * @param $indent The indent of the current node |
| 282 | */ |
| 283 | private function _yamlize($key, $value, $indent, $previous_key = -1, $first_key = 0, $source_array = null) |
| 284 | { |
| 285 | if (\is_object($value)) { |
| 286 | $value = (array) $value; |
| 287 | } |
| 288 | if (\is_array($value)) { |
| 289 | if (empty($value)) { |
| 290 | return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key, $source_array); |
| 291 | } |
| 292 | // It has children. What to do? |
| 293 | // Make it the right kind of item |
| 294 | $string = $this->_dumpNode($key, self::REMPTY, $indent, $previous_key, $first_key, $source_array); |
| 295 | // Add the indent |
| 296 | $indent += $this->_dumpIndent; |
| 297 | // Yamlize the array |
| 298 | $string .= $this->_yamlizeArray($value, $indent); |
| 299 | } elseif (!\is_array($value)) { |
| 300 | // It doesn't have children. Yip. |
| 301 | $string = $this->_dumpNode($key, $value, $indent, $previous_key, $first_key, $source_array); |
| 302 | } |
| 303 | return $string; |
| 304 | } |
| 305 | /** |
| 306 | * Attempts to convert an array to YAML |
| 307 | * @access private |
| 308 | * @return string |
| 309 | * @param $array The array you want to convert |
| 310 | * @param $indent The indent of the current level |
| 311 | */ |
| 312 | private function _yamlizeArray($array, $indent) |
| 313 | { |
| 314 | if (\is_array($array)) { |
| 315 | $string = ''; |
| 316 | $previous_key = -1; |
| 317 | foreach ($array as $key => $value) { |
| 318 | if (!isset($first_key)) { |
| 319 | $first_key = $key; |
| 320 | } |
| 321 | $string .= $this->_yamlize($key, $value, $indent, $previous_key, $first_key, $array); |
| 322 | $previous_key = $key; |
| 323 | } |
| 324 | return $string; |
| 325 | } else { |
| 326 | return \false; |
| 327 | } |
| 328 | } |
| 329 | /** |
| 330 | * Returns YAML from a key and a value |
| 331 | * @access private |
| 332 | * @return string |
| 333 | * @param $key The name of the key |
| 334 | * @param $value The value of the item |
| 335 | * @param $indent The indent of the current node |
| 336 | */ |
| 337 | private function _dumpNode($key, $value, $indent, $previous_key = -1, $first_key = 0, $source_array = null) |
| 338 | { |
| 339 | // do some folding here, for blocks |
| 340 | if (\is_string($value) && (\strpos($value, "\n") !== \false || \strpos($value, ": ") !== \false || \strpos($value, "- ") !== \false || \strpos($value, "*") !== \false || \strpos($value, "#") !== \false || \strpos($value, "<") !== \false || \strpos($value, ">") !== \false || \strpos($value, '%') !== \false || \strpos($value, ' ') !== \false || \strpos($value, "[") !== \false || \strpos($value, "]") !== \false || \strpos($value, "{") !== \false || \strpos($value, "}") !== \false || \strpos($value, "&") !== \false || \strpos($value, "'") !== \false || \strpos($value, "!") === 0 || \substr($value, -1, 1) == ':')) { |
| 341 | $value = $this->_doLiteralBlock($value, $indent); |
| 342 | } else { |
| 343 | $value = $this->_doFolding($value, $indent); |
| 344 | } |
| 345 | if ($value === array()) { |
| 346 | $value = '[ ]'; |
| 347 | } |
| 348 | if ($value === "") { |
| 349 | $value = '""'; |
| 350 | } |
| 351 | if (self::isTranslationWord($value)) { |
| 352 | $value = $this->_doLiteralBlock($value, $indent); |
| 353 | } |
| 354 | if (\trim($value) != $value) { |
| 355 | $value = $this->_doLiteralBlock($value, $indent); |
| 356 | } |
| 357 | if (\is_bool($value)) { |
| 358 | $value = $value ? "true" : "false"; |
| 359 | } |
| 360 | if ($value === null) { |
| 361 | $value = 'null'; |
| 362 | } |
| 363 | if ($value === "'" . self::REMPTY . "'") { |
| 364 | $value = null; |
| 365 | } |
| 366 | $spaces = \str_repeat(' ', $indent); |
| 367 | //if (is_int($key) && $key - 1 == $previous_key && $first_key===0) { |
| 368 | if (\is_array($source_array) && \array_keys($source_array) === \range(0, \count($source_array) - 1)) { |
| 369 | // It's a sequence |
| 370 | $string = $spaces . '- ' . $value . "\n"; |
| 371 | } else { |
| 372 | // if ($first_key===0) throw new Exception('Keys are all screwy. The first one was zero, now it\'s "'. $key .'"'); |
| 373 | // It's mapped |
| 374 | if (\strpos($key, ":") !== \false || \strpos($key, "#") !== \false) { |
| 375 | $key = '"' . $key . '"'; |
| 376 | } |
| 377 | $string = \rtrim($spaces . $key . ': ' . $value) . "\n"; |
| 378 | } |
| 379 | return $string; |
| 380 | } |
| 381 | /** |
| 382 | * Creates a literal block for dumping |
| 383 | * @access private |
| 384 | * @return string |
| 385 | * @param $value |
| 386 | * @param $indent int The value of the indent |
| 387 | */ |
| 388 | private function _doLiteralBlock($value, $indent) |
| 389 | { |
| 390 | if ($value === "\n") { |
| 391 | return '\\n'; |
| 392 | } |
| 393 | if (\strpos($value, "\n") === \false && \strpos($value, "'") === \false) { |
| 394 | return \sprintf("'%s'", $value); |
| 395 | } |
| 396 | if (\strpos($value, "\n") === \false && \strpos($value, '"') === \false) { |
| 397 | return \sprintf('"%s"', $value); |
| 398 | } |
| 399 | $exploded = \explode("\n", $value); |
| 400 | $newValue = '|'; |
| 401 | if (isset($exploded[0]) && ($exploded[0] == "|" || $exploded[0] == "|-" || $exploded[0] == ">")) { |
| 402 | $newValue = $exploded[0]; |
| 403 | unset($exploded[0]); |
| 404 | } |
| 405 | $indent += $this->_dumpIndent; |
| 406 | $spaces = \str_repeat(' ', $indent); |
| 407 | foreach ($exploded as $line) { |
| 408 | $line = \trim($line); |
| 409 | if (\strpos($line, '"') === 0 && \strrpos($line, '"') == \strlen($line) - 1 || \strpos($line, "'") === 0 && \strrpos($line, "'") == \strlen($line) - 1) { |
| 410 | $line = \substr($line, 1, -1); |
| 411 | } |
| 412 | $newValue .= "\n" . $spaces . $line; |
| 413 | } |
| 414 | return $newValue; |
| 415 | } |
| 416 | /** |
| 417 | * Folds a string of text, if necessary |
| 418 | * @access private |
| 419 | * @return string |
| 420 | * @param $value The string you wish to fold |
| 421 | */ |
| 422 | private function _doFolding($value, $indent) |
| 423 | { |
| 424 | // Don't do anything if wordwrap is set to 0 |
| 425 | if ($this->_dumpWordWrap !== 0 && \is_string($value) && \strlen($value) > $this->_dumpWordWrap) { |
| 426 | $indent += $this->_dumpIndent; |
| 427 | $indent = \str_repeat(' ', $indent); |
| 428 | $wrapped = \wordwrap($value, $this->_dumpWordWrap, "\n{$indent}"); |
| 429 | $value = ">\n" . $indent . $wrapped; |
| 430 | } else { |
| 431 | if ($this->setting_dump_force_quotes && \is_string($value) && $value !== self::REMPTY) { |
| 432 | $value = '"' . $value . '"'; |
| 433 | } |
| 434 | if (\is_numeric($value) && \is_string($value)) { |
| 435 | $value = '"' . $value . '"'; |
| 436 | } |
| 437 | } |
| 438 | return $value; |
| 439 | } |
| 440 | private function isTrueWord($value) |
| 441 | { |
| 442 | $words = self::getTranslations(array('true', 'on', 'yes', 'y')); |
| 443 | return \in_array($value, $words, \true); |
| 444 | } |
| 445 | private function isFalseWord($value) |
| 446 | { |
| 447 | $words = self::getTranslations(array('false', 'off', 'no', 'n')); |
| 448 | return \in_array($value, $words, \true); |
| 449 | } |
| 450 | private function isNullWord($value) |
| 451 | { |
| 452 | $words = self::getTranslations(array('null', '~')); |
| 453 | return \in_array($value, $words, \true); |
| 454 | } |
| 455 | private function isTranslationWord($value) |
| 456 | { |
| 457 | return self::isTrueWord($value) || self::isFalseWord($value) || self::isNullWord($value); |
| 458 | } |
| 459 | /** |
| 460 | * Coerce a string into a native type |
| 461 | * Reference: http://yaml.org/type/bool.html |
| 462 | * TODO: Use only words from the YAML spec. |
| 463 | * @access private |
| 464 | * @param $value The value to coerce |
| 465 | */ |
| 466 | private function coerceValue(&$value) |
| 467 | { |
| 468 | if (self::isTrueWord($value)) { |
| 469 | $value = \true; |
| 470 | } else { |
| 471 | if (self::isFalseWord($value)) { |
| 472 | $value = \false; |
| 473 | } else { |
| 474 | if (self::isNullWord($value)) { |
| 475 | $value = null; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | /** |
| 481 | * Given a set of words, perform the appropriate translations on them to |
| 482 | * match the YAML 1.1 specification for type coercing. |
| 483 | * @param $words The words to translate |
| 484 | * @access private |
| 485 | */ |
| 486 | private static function getTranslations(array $words) |
| 487 | { |
| 488 | $result = array(); |
| 489 | foreach ($words as $i) { |
| 490 | $result = \array_merge($result, array(\ucfirst($i), \strtoupper($i), \strtolower($i))); |
| 491 | } |
| 492 | return $result; |
| 493 | } |
| 494 | // LOADING FUNCTIONS |
| 495 | private function _load($input) |
| 496 | { |
| 497 | $Source = $this->loadFromSource($input); |
| 498 | return $this->loadWithSource($Source); |
| 499 | } |
| 500 | private function _loadString($input) |
| 501 | { |
| 502 | $Source = $this->loadFromString($input); |
| 503 | return $this->loadWithSource($Source); |
| 504 | } |
| 505 | private function loadWithSource($Source) |
| 506 | { |
| 507 | if (empty($Source)) { |
| 508 | return array(); |
| 509 | } |
| 510 | if ($this->setting_use_syck_is_possible && \function_exists('IAWPSCOPED\\syck_load')) { |
| 511 | $array = syck_load(\implode("\n", $Source)); |
| 512 | return \is_array($array) ? $array : array(); |
| 513 | } |
| 514 | $this->path = array(); |
| 515 | $this->result = array(); |
| 516 | $cnt = \count($Source); |
| 517 | for ($i = 0; $i < $cnt; $i++) { |
| 518 | $line = $Source[$i]; |
| 519 | $this->indent = \strlen($line) - \strlen(\ltrim($line)); |
| 520 | $tempPath = $this->getParentPathByIndent($this->indent); |
| 521 | $line = self::stripIndent($line, $this->indent); |
| 522 | if (self::isComment($line)) { |
| 523 | continue; |
| 524 | } |
| 525 | if (self::isEmpty($line)) { |
| 526 | continue; |
| 527 | } |
| 528 | $this->path = $tempPath; |
| 529 | $literalBlockStyle = self::startsLiteralBlock($line); |
| 530 | if ($literalBlockStyle) { |
| 531 | $line = \rtrim($line, $literalBlockStyle . " \n"); |
| 532 | $literalBlock = ''; |
| 533 | $line .= ' ' . $this->LiteralPlaceHolder; |
| 534 | $literal_block_indent = \strlen($Source[$i + 1]) - \strlen(\ltrim($Source[$i + 1])); |
| 535 | while (++$i < $cnt && $this->literalBlockContinues($Source[$i], $this->indent)) { |
| 536 | $literalBlock = $this->addLiteralLine($literalBlock, $Source[$i], $literalBlockStyle, $literal_block_indent); |
| 537 | } |
| 538 | $i--; |
| 539 | } |
| 540 | // Strip out comments |
| 541 | if (\strpos($line, '#')) { |
| 542 | $line = \preg_replace('/\\s*#([^"\']+)$/', '', $line); |
| 543 | } |
| 544 | while (++$i < $cnt && self::greedilyNeedNextLine($line)) { |
| 545 | $line = \rtrim($line, " \n\t\r") . ' ' . \ltrim($Source[$i], " \t"); |
| 546 | } |
| 547 | $i--; |
| 548 | $lineArray = $this->_parseLine($line); |
| 549 | if ($literalBlockStyle) { |
| 550 | $lineArray = $this->revertLiteralPlaceHolder($lineArray, $literalBlock); |
| 551 | } |
| 552 | $this->addArray($lineArray, $this->indent); |
| 553 | foreach ($this->delayedPath as $indent => $delayedPath) { |
| 554 | $this->path[$indent] = $delayedPath; |
| 555 | } |
| 556 | $this->delayedPath = array(); |
| 557 | } |
| 558 | return $this->result; |
| 559 | } |
| 560 | private function loadFromSource($input) |
| 561 | { |
| 562 | if (!empty($input) && \strpos($input, "\n") === \false && \file_exists($input)) { |
| 563 | $input = \file_get_contents($input); |
| 564 | } |
| 565 | return $this->loadFromString($input); |
| 566 | } |
| 567 | private function loadFromString($input) |
| 568 | { |
| 569 | $lines = \explode("\n", $input); |
| 570 | foreach ($lines as $k => $_) { |
| 571 | $lines[$k] = \rtrim($_, "\r"); |
| 572 | } |
| 573 | return $lines; |
| 574 | } |
| 575 | /** |
| 576 | * Parses YAML code and returns an array for a node |
| 577 | * @access private |
| 578 | * @return array |
| 579 | * @param string $line A line from the YAML file |
| 580 | */ |
| 581 | private function _parseLine($line) |
| 582 | { |
| 583 | if (!$line) { |
| 584 | return array(); |
| 585 | } |
| 586 | $line = \trim($line); |
| 587 | if (!$line) { |
| 588 | return array(); |
| 589 | } |
| 590 | $array = array(); |
| 591 | $group = $this->nodeContainsGroup($line); |
| 592 | if ($group) { |
| 593 | $this->addGroup($line, $group); |
| 594 | $line = $this->stripGroup($line, $group); |
| 595 | } |
| 596 | if ($this->startsMappedSequence($line)) { |
| 597 | return $this->returnMappedSequence($line); |
| 598 | } |
| 599 | if ($this->startsMappedValue($line)) { |
| 600 | return $this->returnMappedValue($line); |
| 601 | } |
| 602 | if ($this->isArrayElement($line)) { |
| 603 | return $this->returnArrayElement($line); |
| 604 | } |
| 605 | if ($this->isPlainArray($line)) { |
| 606 | return $this->returnPlainArray($line); |
| 607 | } |
| 608 | return $this->returnKeyValuePair($line); |
| 609 | } |
| 610 | /** |
| 611 | * Finds the type of the passed value, returns the value as the new type. |
| 612 | * @access private |
| 613 | * @param string $value |
| 614 | * @return mixed |
| 615 | */ |
| 616 | private function _toType($value) |
| 617 | { |
| 618 | if ($value === '') { |
| 619 | return ""; |
| 620 | } |
| 621 | if ($this->setting_empty_hash_as_object && $value === '{}') { |
| 622 | return new \stdClass(); |
| 623 | } |
| 624 | $first_character = $value[0]; |
| 625 | $last_character = \substr($value, -1, 1); |
| 626 | $is_quoted = \false; |
| 627 | do { |
| 628 | if (!$value) { |
| 629 | break; |
| 630 | } |
| 631 | if ($first_character != '"' && $first_character != "'") { |
| 632 | break; |
| 633 | } |
| 634 | if ($last_character != '"' && $last_character != "'") { |
| 635 | break; |
| 636 | } |
| 637 | $is_quoted = \true; |
| 638 | } while (0); |
| 639 | if ($is_quoted) { |
| 640 | $value = \str_replace('\\n', "\n", $value); |
| 641 | if ($first_character == "'") { |
| 642 | return \strtr(\substr($value, 1, -1), array('\'\'' => '\'', '\\\'' => '\'')); |
| 643 | } |
| 644 | return \strtr(\substr($value, 1, -1), array('\\"' => '"', '\\\'' => '\'')); |
| 645 | } |
| 646 | if (\strpos($value, ' #') !== \false && !$is_quoted) { |
| 647 | $value = \preg_replace('/\\s+#(.+)$/', '', $value); |
| 648 | } |
| 649 | if ($first_character == '[' && $last_character == ']') { |
| 650 | // Take out strings sequences and mappings |
| 651 | $innerValue = \trim(\substr($value, 1, -1)); |
| 652 | if ($innerValue === '') { |
| 653 | return array(); |
| 654 | } |
| 655 | $explode = $this->_inlineEscape($innerValue); |
| 656 | // Propagate value array |
| 657 | $value = array(); |
| 658 | foreach ($explode as $v) { |
| 659 | $value[] = $this->_toType($v); |
| 660 | } |
| 661 | return $value; |
| 662 | } |
| 663 | if (\strpos($value, ': ') !== \false && $first_character != '{') { |
| 664 | $array = \explode(': ', $value); |
| 665 | $key = \trim($array[0]); |
| 666 | \array_shift($array); |
| 667 | $value = \trim(\implode(': ', $array)); |
| 668 | $value = $this->_toType($value); |
| 669 | return array($key => $value); |
| 670 | } |
| 671 | if ($first_character == '{' && $last_character == '}') { |
| 672 | $innerValue = \trim(\substr($value, 1, -1)); |
| 673 | if ($innerValue === '') { |
| 674 | return array(); |
| 675 | } |
| 676 | // Inline Mapping |
| 677 | // Take out strings sequences and mappings |
| 678 | $explode = $this->_inlineEscape($innerValue); |
| 679 | // Propagate value array |
| 680 | $array = array(); |
| 681 | foreach ($explode as $v) { |
| 682 | $SubArr = $this->_toType($v); |
| 683 | if (empty($SubArr)) { |
| 684 | continue; |
| 685 | } |
| 686 | if (\is_array($SubArr)) { |
| 687 | $array[\key($SubArr)] = $SubArr[\key($SubArr)]; |
| 688 | continue; |
| 689 | } |
| 690 | $array[] = $SubArr; |
| 691 | } |
| 692 | return $array; |
| 693 | } |
| 694 | if ($value == 'null' || $value == 'NULL' || $value == 'Null' || $value == '' || $value == '~') { |
| 695 | return null; |
| 696 | } |
| 697 | if (\is_numeric($value) && \preg_match('/^(-|)[1-9]+[0-9]*$/', $value)) { |
| 698 | $intvalue = (int) $value; |
| 699 | if ($intvalue != \PHP_INT_MAX && $intvalue != ~\PHP_INT_MAX) { |
| 700 | $value = $intvalue; |
| 701 | } |
| 702 | return $value; |
| 703 | } |
| 704 | if (\is_string($value) && \preg_match('/^0[xX][0-9a-fA-F]+$/', $value)) { |
| 705 | // Hexadecimal value. |
| 706 | return \hexdec($value); |
| 707 | } |
| 708 | $this->coerceValue($value); |
| 709 | if (\is_numeric($value)) { |
| 710 | if ($value === '0') { |
| 711 | return 0; |
| 712 | } |
| 713 | if (\rtrim($value, 0) === $value) { |
| 714 | $value = (float) $value; |
| 715 | } |
| 716 | return $value; |
| 717 | } |
| 718 | return $value; |
| 719 | } |
| 720 | /** |
| 721 | * Used in inlines to check for more inlines or quoted strings |
| 722 | * @access private |
| 723 | * @return array |
| 724 | */ |
| 725 | private function _inlineEscape($inline) |
| 726 | { |
| 727 | // There's gotta be a cleaner way to do this... |
| 728 | // While pure sequences seem to be nesting just fine, |
| 729 | // pure mappings and mappings with sequences inside can't go very |
| 730 | // deep. This needs to be fixed. |
| 731 | $seqs = array(); |
| 732 | $maps = array(); |
| 733 | $saved_strings = array(); |
| 734 | $saved_empties = array(); |
| 735 | // Check for empty strings |
| 736 | $regex = '/("")|(\'\')/'; |
| 737 | if (\preg_match_all($regex, $inline, $strings)) { |
| 738 | $saved_empties = $strings[0]; |
| 739 | $inline = \preg_replace($regex, 'YAMLEmpty', $inline); |
| 740 | } |
| 741 | unset($regex); |
| 742 | // Check for strings |
| 743 | $regex = '/(?:(")|(?:\'))((?(1)[^"]+|[^\']+))(?(1)"|\')/'; |
| 744 | if (\preg_match_all($regex, $inline, $strings)) { |
| 745 | $saved_strings = $strings[0]; |
| 746 | $inline = \preg_replace($regex, 'YAMLString', $inline); |
| 747 | } |
| 748 | unset($regex); |
| 749 | // echo $inline; |
| 750 | $i = 0; |
| 751 | do { |
| 752 | // Check for sequences |
| 753 | while (\preg_match('/\\[([^{}\\[\\]]+)\\]/U', $inline, $matchseqs)) { |
| 754 | $seqs[] = $matchseqs[0]; |
| 755 | $inline = \preg_replace('/\\[([^{}\\[\\]]+)\\]/U', 'YAMLSeq' . (\count($seqs) - 1) . 's', $inline, 1); |
| 756 | } |
| 757 | // Check for mappings |
| 758 | while (\preg_match('/{([^\\[\\]{}]+)}/U', $inline, $matchmaps)) { |
| 759 | $maps[] = $matchmaps[0]; |
| 760 | $inline = \preg_replace('/{([^\\[\\]{}]+)}/U', 'YAMLMap' . (\count($maps) - 1) . 's', $inline, 1); |
| 761 | } |
| 762 | if ($i++ >= 10) { |
| 763 | break; |
| 764 | } |
| 765 | } while (\strpos($inline, '[') !== \false || \strpos($inline, '{') !== \false); |
| 766 | $explode = \explode(',', $inline); |
| 767 | $explode = \array_map('trim', $explode); |
| 768 | $stringi = 0; |
| 769 | $i = 0; |
| 770 | while (1) { |
| 771 | // Re-add the sequences |
| 772 | if (!empty($seqs)) { |
| 773 | foreach ($explode as $key => $value) { |
| 774 | if (\strpos($value, 'YAMLSeq') !== \false) { |
| 775 | foreach ($seqs as $seqk => $seq) { |
| 776 | $explode[$key] = \str_replace('YAMLSeq' . $seqk . 's', $seq, $value); |
| 777 | $value = $explode[$key]; |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | // Re-add the mappings |
| 783 | if (!empty($maps)) { |
| 784 | foreach ($explode as $key => $value) { |
| 785 | if (\strpos($value, 'YAMLMap') !== \false) { |
| 786 | foreach ($maps as $mapk => $map) { |
| 787 | $explode[$key] = \str_replace('YAMLMap' . $mapk . 's', $map, $value); |
| 788 | $value = $explode[$key]; |
| 789 | } |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | // Re-add the strings |
| 794 | if (!empty($saved_strings)) { |
| 795 | foreach ($explode as $key => $value) { |
| 796 | while (\strpos($value, 'YAMLString') !== \false) { |
| 797 | $explode[$key] = \preg_replace('/YAMLString/', $saved_strings[$stringi], $value, 1); |
| 798 | unset($saved_strings[$stringi]); |
| 799 | ++$stringi; |
| 800 | $value = $explode[$key]; |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | // Re-add the empties |
| 805 | if (!empty($saved_empties)) { |
| 806 | foreach ($explode as $key => $value) { |
| 807 | while (\strpos($value, 'YAMLEmpty') !== \false) { |
| 808 | $explode[$key] = \preg_replace('/YAMLEmpty/', '', $value, 1); |
| 809 | $value = $explode[$key]; |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | $finished = \true; |
| 814 | foreach ($explode as $key => $value) { |
| 815 | if (\strpos($value, 'YAMLSeq') !== \false) { |
| 816 | $finished = \false; |
| 817 | break; |
| 818 | } |
| 819 | if (\strpos($value, 'YAMLMap') !== \false) { |
| 820 | $finished = \false; |
| 821 | break; |
| 822 | } |
| 823 | if (\strpos($value, 'YAMLString') !== \false) { |
| 824 | $finished = \false; |
| 825 | break; |
| 826 | } |
| 827 | if (\strpos($value, 'YAMLEmpty') !== \false) { |
| 828 | $finished = \false; |
| 829 | break; |
| 830 | } |
| 831 | } |
| 832 | if ($finished) { |
| 833 | break; |
| 834 | } |
| 835 | $i++; |
| 836 | if ($i > 10) { |
| 837 | break; |
| 838 | } |
| 839 | // Prevent infinite loops. |
| 840 | } |
| 841 | return $explode; |
| 842 | } |
| 843 | private function literalBlockContinues($line, $lineIndent) |
| 844 | { |
| 845 | if (!\trim($line)) { |
| 846 | return \true; |
| 847 | } |
| 848 | if (\strlen($line) - \strlen(\ltrim($line)) > $lineIndent) { |
| 849 | return \true; |
| 850 | } |
| 851 | return \false; |
| 852 | } |
| 853 | private function referenceContentsByAlias($alias) |
| 854 | { |
| 855 | do { |
| 856 | if (!isset($this->SavedGroups[$alias])) { |
| 857 | echo "Bad group name: {$alias}."; |
| 858 | break; |
| 859 | } |
| 860 | $groupPath = $this->SavedGroups[$alias]; |
| 861 | $value = $this->result; |
| 862 | foreach ($groupPath as $k) { |
| 863 | $value = $value[$k]; |
| 864 | } |
| 865 | } while (\false); |
| 866 | return $value; |
| 867 | } |
| 868 | private function addArrayInline($array, $indent) |
| 869 | { |
| 870 | $CommonGroupPath = $this->path; |
| 871 | if (empty($array)) { |
| 872 | return \false; |
| 873 | } |
| 874 | foreach ($array as $k => $_) { |
| 875 | $this->addArray(array($k => $_), $indent); |
| 876 | $this->path = $CommonGroupPath; |
| 877 | } |
| 878 | return \true; |
| 879 | } |
| 880 | private function addArray($incoming_data, $incoming_indent) |
| 881 | { |
| 882 | // print_r ($incoming_data); |
| 883 | if (\count($incoming_data) > 1) { |
| 884 | return $this->addArrayInline($incoming_data, $incoming_indent); |
| 885 | } |
| 886 | $key = \key($incoming_data); |
| 887 | $value = isset($incoming_data[$key]) ? $incoming_data[$key] : null; |
| 888 | if ($key === '__!YAMLZero') { |
| 889 | $key = '0'; |
| 890 | } |
| 891 | if ($incoming_indent == 0 && !$this->_containsGroupAlias && !$this->_containsGroupAnchor) { |
| 892 | // Shortcut for root-level values. |
| 893 | if ($key || $key === '' || $key === '0') { |
| 894 | $this->result[$key] = $value; |
| 895 | } else { |
| 896 | $this->result[] = $value; |
| 897 | \end($this->result); |
| 898 | $key = \key($this->result); |
| 899 | } |
| 900 | $this->path[$incoming_indent] = $key; |
| 901 | return; |
| 902 | } |
| 903 | $history = array(); |
| 904 | // Unfolding inner array tree. |
| 905 | $history[] = $_arr = $this->result; |
| 906 | foreach ($this->path as $k) { |
| 907 | $history[] = $_arr = $_arr[$k]; |
| 908 | } |
| 909 | if ($this->_containsGroupAlias) { |
| 910 | $value = $this->referenceContentsByAlias($this->_containsGroupAlias); |
| 911 | $this->_containsGroupAlias = \false; |
| 912 | } |
| 913 | // Adding string or numeric key to the innermost level or $this->arr. |
| 914 | if (\is_string($key) && $key == '<<') { |
| 915 | if (!\is_array($_arr)) { |
| 916 | $_arr = array(); |
| 917 | } |
| 918 | $_arr = \array_merge($_arr, $value); |
| 919 | } else { |
| 920 | if ($key || $key === '' || $key === '0') { |
| 921 | if (!\is_array($_arr)) { |
| 922 | $_arr = array($key => $value); |
| 923 | } else { |
| 924 | $_arr[$key] = $value; |
| 925 | } |
| 926 | } else { |
| 927 | if (!\is_array($_arr)) { |
| 928 | $_arr = array($value); |
| 929 | $key = 0; |
| 930 | } else { |
| 931 | $_arr[] = $value; |
| 932 | \end($_arr); |
| 933 | $key = \key($_arr); |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | $reverse_path = \array_reverse($this->path); |
| 938 | $reverse_history = \array_reverse($history); |
| 939 | $reverse_history[0] = $_arr; |
| 940 | $cnt = \count($reverse_history) - 1; |
| 941 | for ($i = 0; $i < $cnt; $i++) { |
| 942 | $reverse_history[$i + 1][$reverse_path[$i]] = $reverse_history[$i]; |
| 943 | } |
| 944 | $this->result = $reverse_history[$cnt]; |
| 945 | $this->path[$incoming_indent] = $key; |
| 946 | if ($this->_containsGroupAnchor) { |
| 947 | $this->SavedGroups[$this->_containsGroupAnchor] = $this->path; |
| 948 | if (\is_array($value)) { |
| 949 | $k = \key($value); |
| 950 | if (!\is_int($k)) { |
| 951 | $this->SavedGroups[$this->_containsGroupAnchor][$incoming_indent + 2] = $k; |
| 952 | } |
| 953 | } |
| 954 | $this->_containsGroupAnchor = \false; |
| 955 | } |
| 956 | } |
| 957 | private static function startsLiteralBlock($line) |
| 958 | { |
| 959 | $lastChar = \substr(\trim($line), -1); |
| 960 | if ($lastChar != '>' && $lastChar != '|') { |
| 961 | return \false; |
| 962 | } |
| 963 | if ($lastChar == '|') { |
| 964 | return $lastChar; |
| 965 | } |
| 966 | // HTML tags should not be counted as literal blocks. |
| 967 | if (\preg_match('#<.*?>$#', $line)) { |
| 968 | return \false; |
| 969 | } |
| 970 | return $lastChar; |
| 971 | } |
| 972 | private static function greedilyNeedNextLine($line) |
| 973 | { |
| 974 | $line = \trim($line); |
| 975 | if (!\strlen($line)) { |
| 976 | return \false; |
| 977 | } |
| 978 | if (\substr($line, -1, 1) == ']') { |
| 979 | return \false; |
| 980 | } |
| 981 | if ($line[0] == '[') { |
| 982 | return \true; |
| 983 | } |
| 984 | if (\preg_match('#^[^:]+?:\\s*\\[#', $line)) { |
| 985 | return \true; |
| 986 | } |
| 987 | return \false; |
| 988 | } |
| 989 | private function addLiteralLine($literalBlock, $line, $literalBlockStyle, $indent = -1) |
| 990 | { |
| 991 | $line = self::stripIndent($line, $indent); |
| 992 | if ($literalBlockStyle !== '|') { |
| 993 | $line = self::stripIndent($line); |
| 994 | } |
| 995 | $line = \rtrim($line, "\r\n\t ") . "\n"; |
| 996 | if ($literalBlockStyle == '|') { |
| 997 | return $literalBlock . $line; |
| 998 | } |
| 999 | if (\strlen($line) == 0) { |
| 1000 | return \rtrim($literalBlock, ' ') . "\n"; |
| 1001 | } |
| 1002 | if ($line == "\n" && $literalBlockStyle == '>') { |
| 1003 | return \rtrim($literalBlock, " \t") . "\n"; |
| 1004 | } |
| 1005 | if ($line != "\n") { |
| 1006 | $line = \trim($line, "\r\n ") . " "; |
| 1007 | } |
| 1008 | return $literalBlock . $line; |
| 1009 | } |
| 1010 | function revertLiteralPlaceHolder($lineArray, $literalBlock) |
| 1011 | { |
| 1012 | foreach ($lineArray as $k => $_) { |
| 1013 | if (\is_array($_)) { |
| 1014 | $lineArray[$k] = $this->revertLiteralPlaceHolder($_, $literalBlock); |
| 1015 | } else { |
| 1016 | if (\substr($_, -1 * \strlen($this->LiteralPlaceHolder)) == $this->LiteralPlaceHolder) { |
| 1017 | $lineArray[$k] = \rtrim($literalBlock, " \r\n"); |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | return $lineArray; |
| 1022 | } |
| 1023 | private static function stripIndent($line, $indent = -1) |
| 1024 | { |
| 1025 | if ($indent == -1) { |
| 1026 | $indent = \strlen($line) - \strlen(\ltrim($line)); |
| 1027 | } |
| 1028 | return \substr($line, $indent); |
| 1029 | } |
| 1030 | private function getParentPathByIndent($indent) |
| 1031 | { |
| 1032 | if ($indent == 0) { |
| 1033 | return array(); |
| 1034 | } |
| 1035 | $linePath = $this->path; |
| 1036 | do { |
| 1037 | \end($linePath); |
| 1038 | $lastIndentInParentPath = \key($linePath); |
| 1039 | if ($indent <= $lastIndentInParentPath) { |
| 1040 | \array_pop($linePath); |
| 1041 | } |
| 1042 | } while ($indent <= $lastIndentInParentPath); |
| 1043 | return $linePath; |
| 1044 | } |
| 1045 | private function clearBiggerPathValues($indent) |
| 1046 | { |
| 1047 | if ($indent == 0) { |
| 1048 | $this->path = array(); |
| 1049 | } |
| 1050 | if (empty($this->path)) { |
| 1051 | return \true; |
| 1052 | } |
| 1053 | foreach ($this->path as $k => $_) { |
| 1054 | if ($k > $indent) { |
| 1055 | unset($this->path[$k]); |
| 1056 | } |
| 1057 | } |
| 1058 | return \true; |
| 1059 | } |
| 1060 | private static function isComment($line) |
| 1061 | { |
| 1062 | if (!$line) { |
| 1063 | return \false; |
| 1064 | } |
| 1065 | if ($line[0] == '#') { |
| 1066 | return \true; |
| 1067 | } |
| 1068 | if (\trim($line, " \r\n\t") == '---') { |
| 1069 | return \true; |
| 1070 | } |
| 1071 | return \false; |
| 1072 | } |
| 1073 | private static function isEmpty($line) |
| 1074 | { |
| 1075 | return \trim($line) === ''; |
| 1076 | } |
| 1077 | private function isArrayElement($line) |
| 1078 | { |
| 1079 | if (!$line || !\is_scalar($line)) { |
| 1080 | return \false; |
| 1081 | } |
| 1082 | if (\substr($line, 0, 2) != '- ') { |
| 1083 | return \false; |
| 1084 | } |
| 1085 | if (\strlen($line) > 3) { |
| 1086 | if (\substr($line, 0, 3) == '---') { |
| 1087 | return \false; |
| 1088 | } |
| 1089 | } |
| 1090 | return \true; |
| 1091 | } |
| 1092 | private function isHashElement($line) |
| 1093 | { |
| 1094 | return \strpos($line, ':'); |
| 1095 | } |
| 1096 | private function isLiteral($line) |
| 1097 | { |
| 1098 | if ($this->isArrayElement($line)) { |
| 1099 | return \false; |
| 1100 | } |
| 1101 | if ($this->isHashElement($line)) { |
| 1102 | return \false; |
| 1103 | } |
| 1104 | return \true; |
| 1105 | } |
| 1106 | private static function unquote($value) |
| 1107 | { |
| 1108 | if (!$value) { |
| 1109 | return $value; |
| 1110 | } |
| 1111 | if (!\is_string($value)) { |
| 1112 | return $value; |
| 1113 | } |
| 1114 | if ($value[0] == '\'') { |
| 1115 | return \trim($value, '\''); |
| 1116 | } |
| 1117 | if ($value[0] == '"') { |
| 1118 | return \trim($value, '"'); |
| 1119 | } |
| 1120 | return $value; |
| 1121 | } |
| 1122 | private function startsMappedSequence($line) |
| 1123 | { |
| 1124 | return \substr($line, 0, 2) == '- ' && \substr($line, -1, 1) == ':'; |
| 1125 | } |
| 1126 | private function returnMappedSequence($line) |
| 1127 | { |
| 1128 | $array = array(); |
| 1129 | $key = self::unquote(\trim(\substr($line, 1, -1))); |
| 1130 | $array[$key] = array(); |
| 1131 | $this->delayedPath = array(\strpos($line, $key) + $this->indent => $key); |
| 1132 | return array($array); |
| 1133 | } |
| 1134 | private function checkKeysInValue($value) |
| 1135 | { |
| 1136 | if (\strchr('[{"\'', $value[0]) === \false) { |
| 1137 | if (\strchr($value, ': ') !== \false) { |
| 1138 | throw new \Exception('Too many keys: ' . $value); |
| 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 | private function returnMappedValue($line) |
| 1143 | { |
| 1144 | $this->checkKeysInValue($line); |
| 1145 | $array = array(); |
| 1146 | $key = self::unquote(\trim(\substr($line, 0, -1))); |
| 1147 | $array[$key] = ''; |
| 1148 | return $array; |
| 1149 | } |
| 1150 | private function startsMappedValue($line) |
| 1151 | { |
| 1152 | return \substr($line, -1, 1) == ':'; |
| 1153 | } |
| 1154 | private function isPlainArray($line) |
| 1155 | { |
| 1156 | return $line[0] == '[' && \substr($line, -1, 1) == ']'; |
| 1157 | } |
| 1158 | private function returnPlainArray($line) |
| 1159 | { |
| 1160 | return $this->_toType($line); |
| 1161 | } |
| 1162 | private function returnKeyValuePair($line) |
| 1163 | { |
| 1164 | $array = array(); |
| 1165 | $key = ''; |
| 1166 | if (\strpos($line, ': ')) { |
| 1167 | // It's a key/value pair most likely |
| 1168 | // If the key is in double quotes pull it out |
| 1169 | if (($line[0] == '"' || $line[0] == "'") && \preg_match('/^(["\'](.*)["\'](\\s)*:)/', $line, $matches)) { |
| 1170 | $value = \trim(\str_replace($matches[1], '', $line)); |
| 1171 | $key = $matches[2]; |
| 1172 | } else { |
| 1173 | // Do some guesswork as to the key and the value |
| 1174 | $explode = \explode(': ', $line); |
| 1175 | $key = \trim(\array_shift($explode)); |
| 1176 | $value = \trim(\implode(': ', $explode)); |
| 1177 | $this->checkKeysInValue($value); |
| 1178 | } |
| 1179 | // Set the type of the value. Int, string, etc |
| 1180 | $value = $this->_toType($value); |
| 1181 | if ($key === '0') { |
| 1182 | $key = '__!YAMLZero'; |
| 1183 | } |
| 1184 | $array[$key] = $value; |
| 1185 | } else { |
| 1186 | $array = array($line); |
| 1187 | } |
| 1188 | return $array; |
| 1189 | } |
| 1190 | private function returnArrayElement($line) |
| 1191 | { |
| 1192 | if (\strlen($line) <= 1) { |
| 1193 | return array(array()); |
| 1194 | } |
| 1195 | // Weird %) |
| 1196 | $array = array(); |
| 1197 | $value = \trim(\substr($line, 1)); |
| 1198 | $value = $this->_toType($value); |
| 1199 | if ($this->isArrayElement($value)) { |
| 1200 | $value = $this->returnArrayElement($value); |
| 1201 | } |
| 1202 | $array[] = $value; |
| 1203 | return $array; |
| 1204 | } |
| 1205 | private function nodeContainsGroup($line) |
| 1206 | { |
| 1207 | $symbolsForReference = 'A-z0-9_\\-'; |
| 1208 | if (\strpos($line, '&') === \false && \strpos($line, '*') === \false) { |
| 1209 | return \false; |
| 1210 | } |
| 1211 | // Please die fast ;-) |
| 1212 | if ($line[0] == '&' && \preg_match('/^(&[' . $symbolsForReference . ']+)/', $line, $matches)) { |
| 1213 | return $matches[1]; |
| 1214 | } |
| 1215 | if ($line[0] == '*' && \preg_match('/^(\\*[' . $symbolsForReference . ']+)/', $line, $matches)) { |
| 1216 | return $matches[1]; |
| 1217 | } |
| 1218 | if (\preg_match('/(&[' . $symbolsForReference . ']+)$/', $line, $matches)) { |
| 1219 | return $matches[1]; |
| 1220 | } |
| 1221 | if (\preg_match('/(\\*[' . $symbolsForReference . ']+$)/', $line, $matches)) { |
| 1222 | return $matches[1]; |
| 1223 | } |
| 1224 | if (\preg_match('#^\\s*<<\\s*:\\s*(\\*[^\\s]+).*$#', $line, $matches)) { |
| 1225 | return $matches[1]; |
| 1226 | } |
| 1227 | return \false; |
| 1228 | } |
| 1229 | private function addGroup($line, $group) |
| 1230 | { |
| 1231 | if ($group[0] == '&') { |
| 1232 | $this->_containsGroupAnchor = \substr($group, 1); |
| 1233 | } |
| 1234 | if ($group[0] == '*') { |
| 1235 | $this->_containsGroupAlias = \substr($group, 1); |
| 1236 | } |
| 1237 | //print_r ($this->path); |
| 1238 | } |
| 1239 | private function stripGroup($line, $group) |
| 1240 | { |
| 1241 | $line = \trim(\str_replace($group, '', $line)); |
| 1242 | return $line; |
| 1243 | } |
| 1244 | } |
| 1245 | } |
| 1246 | // Enable use of Spyc from command line |
| 1247 | // The syntax is the following: php Spyc.php spyc.yaml |
| 1248 | do { |
| 1249 | if (\PHP_SAPI != 'cli') { |
| 1250 | break; |
| 1251 | } |
| 1252 | if (empty($_SERVER['argc']) || $_SERVER['argc'] < 2) { |
| 1253 | break; |
| 1254 | } |
| 1255 | if (empty($_SERVER['PHP_SELF']) || \FALSE === \strpos($_SERVER['PHP_SELF'], 'Spyc.php')) { |
| 1256 | break; |
| 1257 | } |
| 1258 | $file = $argv[1]; |
| 1259 | echo \json_encode(spyc_load_file($file)); |
| 1260 | } while (0); |
| 1261 |