Index: trunk/extensions/Translate/spyc/spyc.php |
— | — | @@ -10,25 +10,25 @@ |
11 | 11 | * @package Spyc |
12 | 12 | */ |
13 | 13 | |
14 | | -if ( !function_exists( 'spyc_load' ) ) { |
| 14 | +if (!function_exists('spyc_load')) { |
15 | 15 | /** |
16 | 16 | * Parses YAML to array. |
17 | 17 | * @param string $string YAML string. |
18 | 18 | * @return array |
19 | 19 | */ |
20 | | - function spyc_load ( $string ) { |
21 | | - return Spyc::YAMLLoadString( $string ); |
| 20 | + function spyc_load ($string) { |
| 21 | + return Spyc::YAMLLoadString($string); |
22 | 22 | } |
23 | 23 | } |
24 | 24 | |
25 | | -if ( !function_exists( 'spyc_load_file' ) ) { |
| 25 | +if (!function_exists('spyc_load_file')) { |
26 | 26 | /** |
27 | 27 | * Parses YAML to array. |
28 | 28 | * @param string $file Path to YAML file. |
29 | 29 | * @return array |
30 | 30 | */ |
31 | | - function spyc_load_file ( $file ) { |
32 | | - return Spyc::YAMLLoad( $file ); |
| 31 | + function spyc_load_file ($file) { |
| 32 | + return Spyc::YAMLLoad($file); |
33 | 33 | } |
34 | 34 | } |
35 | 35 | |
— | — | @@ -105,8 +105,8 @@ |
106 | 106 | * @param string $input |
107 | 107 | * @return array |
108 | 108 | */ |
109 | | - public function load ( $input ) { |
110 | | - return $this->__loadString( $input ); |
| 109 | + public function load ($input) { |
| 110 | + return $this->__loadString($input); |
111 | 111 | } |
112 | 112 | |
113 | 113 | /** |
— | — | @@ -114,8 +114,8 @@ |
115 | 115 | * @param string $file |
116 | 116 | * @return array |
117 | 117 | */ |
118 | | - public function loadFile ( $file ) { |
119 | | - return $this->__load( $file ); |
| 118 | + public function loadFile ($file) { |
| 119 | + return $this->__load($file); |
120 | 120 | } |
121 | 121 | |
122 | 122 | /** |
— | — | @@ -133,9 +133,9 @@ |
134 | 134 | * @return array |
135 | 135 | * @param string $input Path of YAML file or string containing YAML |
136 | 136 | */ |
137 | | - public static function YAMLLoad( $input ) { |
| 137 | + public static function YAMLLoad($input) { |
138 | 138 | $Spyc = new Spyc; |
139 | | - return $Spyc->__load( $input ); |
| 139 | + return $Spyc->__load($input); |
140 | 140 | } |
141 | 141 | |
142 | 142 | /** |
— | — | @@ -157,9 +157,9 @@ |
158 | 158 | * @return array |
159 | 159 | * @param string $input String containing YAML |
160 | 160 | */ |
161 | | - public static function YAMLLoadString( $input ) { |
| 161 | + public static function YAMLLoadString($input) { |
162 | 162 | $Spyc = new Spyc; |
163 | | - return $Spyc->__loadString( $input ); |
| 163 | + return $Spyc->__loadString($input); |
164 | 164 | } |
165 | 165 | |
166 | 166 | /** |
— | — | @@ -182,9 +182,9 @@ |
183 | 183 | * @param int $indent Pass in false to use the default, which is 2 |
184 | 184 | * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40) |
185 | 185 | */ |
186 | | - public static function YAMLDump( $array, $indent = false, $wordwrap = false ) { |
| 186 | + public static function YAMLDump($array,$indent = false,$wordwrap = false) { |
187 | 187 | $spyc = new Spyc; |
188 | | - return $spyc->dump( $array, $indent, $wordwrap ); |
| 188 | + return $spyc->dump($array,$indent,$wordwrap); |
189 | 189 | } |
190 | 190 | |
191 | 191 | |
— | — | @@ -208,18 +208,18 @@ |
209 | 209 | * @param int $indent Pass in false to use the default, which is 2 |
210 | 210 | * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40) |
211 | 211 | */ |
212 | | - public function dump( $array, $indent = false, $wordwrap = false ) { |
| 212 | + public function dump($array,$indent = false,$wordwrap = false) { |
213 | 213 | // Dumps to some very clean YAML. We'll have to add some more features |
214 | 214 | // and options soon. And better support for folding. |
215 | 215 | |
216 | 216 | // New features and options. |
217 | | - if ( $indent === false or !is_numeric( $indent ) ) { |
| 217 | + if ($indent === false or !is_numeric($indent)) { |
218 | 218 | $this->_dumpIndent = 2; |
219 | 219 | } else { |
220 | 220 | $this->_dumpIndent = $indent; |
221 | 221 | } |
222 | 222 | |
223 | | - if ( $wordwrap === false or !is_numeric( $wordwrap ) ) { |
| 223 | + if ($wordwrap === false or !is_numeric($wordwrap)) { |
224 | 224 | $this->_dumpWordWrap = 40; |
225 | 225 | } else { |
226 | 226 | $this->_dumpWordWrap = $wordwrap; |
— | — | @@ -229,13 +229,13 @@ |
230 | 230 | $string = "---\n"; |
231 | 231 | |
232 | 232 | // Start at the base of the array and move through it. |
233 | | - if ( $array ) { |
| 233 | + if ($array) { |
234 | 234 | $array = (array)$array; |
235 | | - $first_key = key( $array ); |
| 235 | + $first_key = key($array); |
236 | 236 | |
237 | 237 | $previous_key = -1; |
238 | | - foreach ( $array as $key => $value ) { |
239 | | - $string .= $this->_yamlize( $key, $value, 0, $previous_key, $first_key ); |
| 238 | + foreach ($array as $key => $value) { |
| 239 | + $string .= $this->_yamlize($key,$value,0,$previous_key, $first_key, $array); |
240 | 240 | $previous_key = $key; |
241 | 241 | } |
242 | 242 | } |
— | — | @@ -250,20 +250,20 @@ |
251 | 251 | * @param $value The value of the item |
252 | 252 | * @param $indent The indent of the current node |
253 | 253 | */ |
254 | | - private function _yamlize( $key, $value, $indent, $previous_key = -1, $first_key = 0 ) { |
255 | | - if ( is_array( $value ) ) { |
256 | | - if ( empty ( $value ) ) |
257 | | - return $this->_dumpNode( $key, array(), $indent, $previous_key, $first_key ); |
| 254 | + private function _yamlize($key,$value,$indent, $previous_key = -1, $first_key = 0, $source_array = null) { |
| 255 | + if (is_array($value)) { |
| 256 | + if (empty ($value)) |
| 257 | + return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key, $source_array); |
258 | 258 | // It has children. What to do? |
259 | 259 | // Make it the right kind of item |
260 | | - $string = $this->_dumpNode( $key, null, $indent, $previous_key, $first_key ); |
| 260 | + $string = $this->_dumpNode($key, NULL, $indent, $previous_key, $first_key, $source_array); |
261 | 261 | // Add the indent |
262 | 262 | $indent += $this->_dumpIndent; |
263 | 263 | // Yamlize the array |
264 | | - $string .= $this->_yamlizeArray( $value, $indent ); |
265 | | - } elseif ( !is_array( $value ) ) { |
| 264 | + $string .= $this->_yamlizeArray($value,$indent); |
| 265 | + } elseif (!is_array($value)) { |
266 | 266 | // It doesn't have children. Yip. |
267 | | - $string = $this->_dumpNode( $key, $value, $indent, $previous_key, $first_key ); |
| 267 | + $string = $this->_dumpNode($key, $value, $indent, $previous_key, $first_key, $source_array); |
268 | 268 | } |
269 | 269 | return $string; |
270 | 270 | } |
— | — | @@ -275,13 +275,14 @@ |
276 | 276 | * @param $array The array you want to convert |
277 | 277 | * @param $indent The indent of the current level |
278 | 278 | */ |
279 | | - private function _yamlizeArray( $array, $indent ) { |
280 | | - if ( is_array( $array ) ) { |
| 279 | + private function _yamlizeArray($array,$indent) { |
| 280 | + if (is_array($array)) { |
281 | 281 | $string = ''; |
282 | 282 | $previous_key = -1; |
283 | | - $first_key = key( $array ); |
284 | | - foreach ( $array as $key => $value ) { |
285 | | - $string .= $this->_yamlize( $key, $value, $indent, $previous_key, $first_key ); |
| 283 | + $first_key = key($array); |
| 284 | + |
| 285 | + foreach ($array as $key => $value) { |
| 286 | + $string .= $this->_yamlize($key, $value, $indent, $previous_key, $first_key, $array); |
286 | 287 | $previous_key = $key; |
287 | 288 | } |
288 | 289 | return $string; |
— | — | @@ -298,38 +299,40 @@ |
299 | 300 | * @param $value The value of the item |
300 | 301 | * @param $indent The indent of the current node |
301 | 302 | */ |
302 | | - private function _dumpNode( $key, $value, $indent, $previous_key = -1, $first_key = 0 ) { |
| 303 | + private function _dumpNode($key, $value, $indent, $previous_key = -1, $first_key = 0, $source_array = null) { |
303 | 304 | // do some folding here, for blocks |
304 | | - if ( is_string ( $value ) && ( ( strpos( $value, "\n" ) !== false || strpos( $value, ": " ) !== false || strpos( $value, "- " ) !== false || |
305 | | - strpos( $value, "*" ) !== false || strpos( $value, "#" ) !== false || strpos( $value, "<" ) !== false || strpos( $value, ">" ) !== false || strpos ( $value, ' ' ) !== false || |
306 | | - strpos( $value, "[" ) !== false || strpos( $value, "]" ) !== false || strpos( $value, "{" ) !== false || strpos( $value, "}" ) !== false ) || substr ( $value, -1, 1 ) == ':' ) |
| 305 | + if (is_string ($value) && ((strpos($value,"\n") !== false || strpos($value,": ") !== false || strpos($value,"- ") !== false || |
| 306 | + strpos($value,"*") !== false || strpos($value,"#") !== false || strpos($value,"<") !== false || strpos($value,">") !== false || strpos ($value, ' ') !== false || |
| 307 | + strpos($value,"[") !== false || strpos($value,"]") !== false || strpos($value,"{") !== false || strpos($value,"}") !== false) || strpos($value,"&") !== false || |
| 308 | + substr ($value, -1, 1) == ':') |
307 | 309 | ) { |
308 | | - $value = $this->_doLiteralBlock( $value, $indent ); |
| 310 | + $value = $this->_doLiteralBlock($value,$indent); |
309 | 311 | } else { |
310 | | - $value = $this->_doFolding( $value, $indent ); |
| 312 | + $value = $this->_doFolding($value,$indent); |
311 | 313 | } |
312 | 314 | |
313 | | - if ( $value === array() ) $value = '[ ]'; |
314 | | - if ( in_array ( $value, array ( 'true', 'TRUE', 'false', 'FALSE', 'y', 'Y', 'n', 'N', 'null', 'NULL' ), true ) ) { |
315 | | - $value = $this->_doLiteralBlock( $value, $indent ); |
| 315 | + if ($value === array()) $value = '[ ]'; |
| 316 | + if (in_array ($value, array ('true', 'TRUE', 'false', 'FALSE', 'y', 'Y', 'n', 'N', 'null', 'NULL'), true)) { |
| 317 | + $value = $this->_doLiteralBlock($value,$indent); |
316 | 318 | } |
317 | | - if ( trim ( $value ) != $value ) |
318 | | - $value = $this->_doLiteralBlock( $value, $indent ); |
| 319 | + if (trim ($value) != $value) |
| 320 | + $value = $this->_doLiteralBlock($value,$indent); |
319 | 321 | |
320 | | - if ( is_bool( $value ) ) { |
321 | | - $value = ( $value ) ? "true" : "false"; |
| 322 | + if (is_bool($value)) { |
| 323 | + $value = ($value) ? "true" : "false"; |
322 | 324 | } |
323 | 325 | |
324 | | - $spaces = str_repeat( ' ', $indent ); |
| 326 | + $spaces = str_repeat(' ',$indent); |
325 | 327 | |
326 | | - if ( is_int( $key ) && $key - 1 == $previous_key && $first_key === 0 ) { |
| 328 | + //if (is_int($key) && $key - 1 == $previous_key && $first_key===0) { |
| 329 | + if (is_array ($source_array) && array_keys($source_array) === range(0, count($source_array) - 1)) { |
327 | 330 | // It's a sequence |
328 | | - $string = $spaces . '- ' . $value . "\n"; |
| 331 | + $string = $spaces.'- '.$value."\n"; |
329 | 332 | } else { |
330 | | - if ( $first_key === 0 ) throw new Exception( 'Keys are all screwy. The first one was zero, now it\'s "' . $key . '"' ); |
| 333 | + // if ($first_key===0) throw new Exception('Keys are all screwy. The first one was zero, now it\'s "'. $key .'"'); |
331 | 334 | // It's mapped |
332 | | - if ( strpos( $key, ":" ) !== false ) { $key = '"' . $key . '"'; } |
333 | | - $string = $spaces . $key . ': ' . $value . "\n"; |
| 335 | + if (strpos($key, ":") !== false || strpos($key, "#") !== false) { $key = '"' . $key . '"'; } |
| 336 | + $string = rtrim ($spaces.$key.': '.$value)."\n"; |
334 | 337 | } |
335 | 338 | return $string; |
336 | 339 | } |
— | — | @@ -341,20 +344,20 @@ |
342 | 345 | * @param $value |
343 | 346 | * @param $indent int The value of the indent |
344 | 347 | */ |
345 | | - private function _doLiteralBlock( $value, $indent ) { |
346 | | - if ( $value === "\n" ) return '\n'; |
347 | | - if ( strpos( $value, "\n" ) === false && strpos( $value, "'" ) === false ) { |
348 | | - return sprintf ( "'%s'", $value ); |
| 348 | + private function _doLiteralBlock($value,$indent) { |
| 349 | + if ($value === "\n") return '\n'; |
| 350 | + if (strpos($value, "\n") === false && strpos($value, "'") === false) { |
| 351 | + return sprintf ("'%s'", $value); |
349 | 352 | } |
350 | | - if ( strpos( $value, "\n" ) === false && strpos( $value, '"' ) === false ) { |
351 | | - return sprintf ( '"%s"', $value ); |
| 353 | + if (strpos($value, "\n") === false && strpos($value, '"') === false) { |
| 354 | + return sprintf ('"%s"', $value); |
352 | 355 | } |
353 | | - $exploded = explode( "\n", $value ); |
| 356 | + $exploded = explode("\n",$value); |
354 | 357 | $newValue = '|'; |
355 | 358 | $indent += $this->_dumpIndent; |
356 | | - $spaces = str_repeat( ' ', $indent ); |
357 | | - foreach ( $exploded as $line ) { |
358 | | - $newValue .= "\n" . $spaces . ( $line ); |
| 359 | + $spaces = str_repeat(' ',$indent); |
| 360 | + foreach ($exploded as $line) { |
| 361 | + $newValue .= "\n" . $spaces . ($line); |
359 | 362 | } |
360 | 363 | return $newValue; |
361 | 364 | } |
— | — | @@ -365,16 +368,16 @@ |
366 | 369 | * @return string |
367 | 370 | * @param $value The string you wish to fold |
368 | 371 | */ |
369 | | - private function _doFolding( $value, $indent ) { |
| 372 | + private function _doFolding($value,$indent) { |
370 | 373 | // Don't do anything if wordwrap is set to 0 |
371 | 374 | |
372 | | - if ( $this->_dumpWordWrap !== 0 && is_string ( $value ) && strlen( $value ) > $this->_dumpWordWrap ) { |
| 375 | + if ($this->_dumpWordWrap !== 0 && is_string ($value) && strlen($value) > $this->_dumpWordWrap) { |
373 | 376 | $indent += $this->_dumpIndent; |
374 | | - $indent = str_repeat( ' ', $indent ); |
375 | | - $wrapped = wordwrap( $value, $this->_dumpWordWrap, "\n$indent" ); |
376 | | - $value = ">\n" . $indent . $wrapped; |
| 377 | + $indent = str_repeat(' ',$indent); |
| 378 | + $wrapped = wordwrap($value,$this->_dumpWordWrap,"\n$indent"); |
| 379 | + $value = ">\n".$indent.$wrapped; |
377 | 380 | } else { |
378 | | - if ( $this->setting_dump_force_quotes && is_string ( $value ) ) |
| 381 | + if ($this->setting_dump_force_quotes && is_string ($value)) |
379 | 382 | $value = '"' . $value . '"'; |
380 | 383 | } |
381 | 384 | |
— | — | @@ -384,69 +387,69 @@ |
385 | 388 | |
386 | 389 | // LOADING FUNCTIONS |
387 | 390 | |
388 | | - private function __load( $input ) { |
389 | | - $Source = $this->loadFromSource( $input ); |
390 | | - return $this->loadWithSource( $Source ); |
| 391 | + private function __load($input) { |
| 392 | + $Source = $this->loadFromSource($input); |
| 393 | + return $this->loadWithSource($Source); |
391 | 394 | } |
392 | 395 | |
393 | | - private function __loadString( $input ) { |
394 | | - $Source = $this->loadFromString( $input ); |
395 | | - return $this->loadWithSource( $Source ); |
| 396 | + private function __loadString($input) { |
| 397 | + $Source = $this->loadFromString($input); |
| 398 | + return $this->loadWithSource($Source); |
396 | 399 | } |
397 | 400 | |
398 | | - private function loadWithSource( $Source ) { |
399 | | - if ( empty ( $Source ) ) return array(); |
400 | | - if ( $this->setting_use_syck_is_possible && function_exists ( 'syck_load' ) ) { |
401 | | - $array = syck_load ( implode ( '', $Source ) ); |
402 | | - return is_array( $array ) ? $array : array(); |
| 401 | + private function loadWithSource($Source) { |
| 402 | + if (empty ($Source)) return array(); |
| 403 | + if ($this->setting_use_syck_is_possible && function_exists ('syck_load')) { |
| 404 | + $array = syck_load (implode ('', $Source)); |
| 405 | + return is_array($array) ? $array : array(); |
403 | 406 | } |
404 | 407 | |
405 | 408 | $this->path = array(); |
406 | 409 | $this->result = array(); |
407 | 410 | |
408 | | - $cnt = count( $Source ); |
409 | | - for ( $i = 0; $i < $cnt; $i++ ) { |
| 411 | + $cnt = count($Source); |
| 412 | + for ($i = 0; $i < $cnt; $i++) { |
410 | 413 | $line = $Source[$i]; |
411 | 414 | |
412 | | - $this->indent = strlen( $line ) - strlen( ltrim( $line ) ); |
413 | | - $tempPath = $this->getParentPathByIndent( $this->indent ); |
414 | | - $line = self::stripIndent( $line, $this->indent ); |
415 | | - if ( self::isComment( $line ) ) continue; |
416 | | - if ( self::isEmpty( $line ) ) continue; |
| 415 | + $this->indent = strlen($line) - strlen(ltrim($line)); |
| 416 | + $tempPath = $this->getParentPathByIndent($this->indent); |
| 417 | + $line = self::stripIndent($line, $this->indent); |
| 418 | + if (self::isComment($line)) continue; |
| 419 | + if (self::isEmpty($line)) continue; |
417 | 420 | $this->path = $tempPath; |
418 | 421 | |
419 | | - $literalBlockStyle = self::startsLiteralBlock( $line ); |
420 | | - if ( $literalBlockStyle ) { |
421 | | - $line = rtrim ( $line, $literalBlockStyle . " \n" ); |
| 422 | + $literalBlockStyle = self::startsLiteralBlock($line); |
| 423 | + if ($literalBlockStyle) { |
| 424 | + $line = rtrim ($line, $literalBlockStyle . " \n"); |
422 | 425 | $literalBlock = ''; |
423 | 426 | $line .= $this->LiteralPlaceHolder; |
424 | 427 | |
425 | | - while ( ++$i < $cnt && $this->literalBlockContinues( $Source[$i], $this->indent ) ) { |
426 | | - $literalBlock = $this->addLiteralLine( $literalBlock, $Source[$i], $literalBlockStyle ); |
| 428 | + while (++$i < $cnt && $this->literalBlockContinues($Source[$i], $this->indent)) { |
| 429 | + $literalBlock = $this->addLiteralLine($literalBlock, $Source[$i], $literalBlockStyle); |
427 | 430 | } |
428 | 431 | $i--; |
429 | 432 | } |
430 | 433 | |
431 | | - while ( ++$i < $cnt && self::greedilyNeedNextLine( $line ) ) { |
432 | | - $line = rtrim ( $line, " \n\t\r" ) . ' ' . ltrim ( $Source[$i], " \t" ); |
| 434 | + while (++$i < $cnt && self::greedilyNeedNextLine($line)) { |
| 435 | + $line = rtrim ($line, " \n\t\r") . ' ' . ltrim ($Source[$i], " \t"); |
433 | 436 | } |
434 | 437 | $i--; |
435 | 438 | |
436 | 439 | |
437 | 440 | |
438 | | - if ( strpos ( $line, '#' ) ) { |
439 | | - if ( strpos ( $line, '"' ) === false && strpos ( $line, "'" ) === false ) |
440 | | - $line = preg_replace( '/\s+#(.+)$/', '', $line ); |
| 441 | + if (strpos ($line, '#')) { |
| 442 | + if (strpos ($line, '"') === false && strpos ($line, "'") === false) |
| 443 | + $line = preg_replace('/\s+#(.+)$/','',$line); |
441 | 444 | } |
442 | 445 | |
443 | | - $lineArray = $this->_parseLine( $line ); |
| 446 | + $lineArray = $this->_parseLine($line); |
444 | 447 | |
445 | | - if ( $literalBlockStyle ) |
446 | | - $lineArray = $this->revertLiteralPlaceHolder ( $lineArray, $literalBlock ); |
| 448 | + if ($literalBlockStyle) |
| 449 | + $lineArray = $this->revertLiteralPlaceHolder ($lineArray, $literalBlock); |
447 | 450 | |
448 | | - $this->addArray( $lineArray, $this->indent ); |
| 451 | + $this->addArray($lineArray, $this->indent); |
449 | 452 | |
450 | | - foreach ( $this->delayedPath as $indent => $delayedPath ) |
| 453 | + foreach ($this->delayedPath as $indent => $delayedPath) |
451 | 454 | $this->path[$indent] = $delayedPath; |
452 | 455 | |
453 | 456 | $this->delayedPath = array(); |
— | — | @@ -455,17 +458,17 @@ |
456 | 459 | return $this->result; |
457 | 460 | } |
458 | 461 | |
459 | | - private function loadFromSource ( $input ) { |
460 | | - if ( !empty( $input ) && strpos( $input, "\n" ) === false && file_exists( $input ) ) |
461 | | - return file( $input ); |
| 462 | + private function loadFromSource ($input) { |
| 463 | + if (!empty($input) && strpos($input, "\n") === false && file_exists($input)) |
| 464 | + return file($input); |
462 | 465 | |
463 | | - return $this->loadFromString( $input ); |
| 466 | + return $this->loadFromString($input); |
464 | 467 | } |
465 | 468 | |
466 | | - private function loadFromString ( $input ) { |
467 | | - $lines = explode( "\n", $input ); |
468 | | - foreach ( $lines as $k => $_ ) { |
469 | | - $lines[$k] = rtrim ( $_, "\r" ); |
| 469 | + private function loadFromString ($input) { |
| 470 | + $lines = explode("\n",$input); |
| 471 | + foreach ($lines as $k => $_) { |
| 472 | + $lines[$k] = rtrim ($_, "\r"); |
470 | 473 | } |
471 | 474 | return $lines; |
472 | 475 | } |
— | — | @@ -476,31 +479,33 @@ |
477 | 480 | * @return array |
478 | 481 | * @param string $line A line from the YAML file |
479 | 482 | */ |
480 | | - private function _parseLine( $line ) { |
481 | | - if ( !$line ) return array(); |
482 | | - $line = trim( $line ); |
483 | | - if ( !$line ) return array(); |
| 483 | + private function _parseLine($line) { |
| 484 | + if (!$line) return array(); |
| 485 | + $line = trim($line); |
| 486 | + if (!$line) return array(); |
484 | 487 | |
485 | | - $group = $this->nodeContainsGroup( $line ); |
486 | | - if ( $group ) { |
487 | | - $this->addGroup( $line, $group ); |
488 | | - $line = $this->stripGroup ( $line, $group ); |
| 488 | + $array = array(); |
| 489 | + |
| 490 | + $group = $this->nodeContainsGroup($line); |
| 491 | + if ($group) { |
| 492 | + $this->addGroup($line, $group); |
| 493 | + $line = $this->stripGroup ($line, $group); |
489 | 494 | } |
490 | 495 | |
491 | | - if ( $this->startsMappedSequence( $line ) ) |
492 | | - return $this->returnMappedSequence( $line ); |
| 496 | + if ($this->startsMappedSequence($line)) |
| 497 | + return $this->returnMappedSequence($line); |
493 | 498 | |
494 | | - if ( $this->startsMappedValue( $line ) ) |
495 | | - return $this->returnMappedValue( $line ); |
| 499 | + if ($this->startsMappedValue($line)) |
| 500 | + return $this->returnMappedValue($line); |
496 | 501 | |
497 | | - if ( $this->isArrayElement( $line ) ) |
498 | | - return $this->returnArrayElement( $line ); |
| 502 | + if ($this->isArrayElement($line)) |
| 503 | + return $this->returnArrayElement($line); |
499 | 504 | |
500 | | - if ( $this->isPlainArray( $line ) ) |
501 | | - return $this->returnPlainArray( $line ); |
| 505 | + if ($this->isPlainArray($line)) |
| 506 | + return $this->returnPlainArray($line); |
502 | 507 | |
503 | 508 | |
504 | | - return $this->returnKeyValuePair( $line ); |
| 509 | + return $this->returnKeyValuePair($line); |
505 | 510 | |
506 | 511 | } |
507 | 512 | |
— | — | @@ -510,92 +515,92 @@ |
511 | 516 | * @param string $value |
512 | 517 | * @return mixed |
513 | 518 | */ |
514 | | - private function _toType( $value ) { |
515 | | - if ( $value === '' ) return null; |
| 519 | + private function _toType($value) { |
| 520 | + if ($value === '') return null; |
516 | 521 | $first_character = $value[0]; |
517 | | - $last_character = substr( $value, -1, 1 ); |
| 522 | + $last_character = substr($value, -1, 1); |
518 | 523 | |
519 | 524 | $is_quoted = false; |
520 | 525 | do { |
521 | | - if ( !$value ) break; |
522 | | - if ( $first_character != '"' && $first_character != "'" ) break; |
523 | | - if ( $last_character != '"' && $last_character != "'" ) break; |
| 526 | + if (!$value) break; |
| 527 | + if ($first_character != '"' && $first_character != "'") break; |
| 528 | + if ($last_character != '"' && $last_character != "'") break; |
524 | 529 | $is_quoted = true; |
525 | | - } while ( 0 ); |
| 530 | + } while (0); |
526 | 531 | |
527 | | - if ( $is_quoted ) |
528 | | - return strtr( substr ( $value, 1, -1 ), array ( '\\"' => '"', '\'\'' => '\'', '\\\'' => '\'' ) ); |
| 532 | + if ($is_quoted) |
| 533 | + return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\'\'' => '\'', '\\\'' => '\'')); |
529 | 534 | |
530 | | - if ( strpos( $value, ' #' ) !== false && !$is_quoted ) |
531 | | - $value = preg_replace( '/\s+#(.+)$/', '', $value ); |
| 535 | + if (strpos($value, ' #') !== false && !$is_quoted) |
| 536 | + $value = preg_replace('/\s+#(.+)$/','',$value); |
532 | 537 | |
533 | | - if ( !$is_quoted ) $value = str_replace( '\n', "\n", $value ); |
| 538 | + if (!$is_quoted) $value = str_replace('\n', "\n", $value); |
534 | 539 | |
535 | | - if ( $first_character == '[' && $last_character == ']' ) { |
| 540 | + if ($first_character == '[' && $last_character == ']') { |
536 | 541 | // Take out strings sequences and mappings |
537 | | - $innerValue = trim( substr ( $value, 1, -1 ) ); |
538 | | - if ( $innerValue === '' ) return array(); |
539 | | - $explode = $this->_inlineEscape( $innerValue ); |
| 542 | + $innerValue = trim(substr ($value, 1, -1)); |
| 543 | + if ($innerValue === '') return array(); |
| 544 | + $explode = $this->_inlineEscape($innerValue); |
540 | 545 | // Propagate value array |
541 | 546 | $value = array(); |
542 | | - foreach ( $explode as $v ) { |
543 | | - $value[] = $this->_toType( $v ); |
| 547 | + foreach ($explode as $v) { |
| 548 | + $value[] = $this->_toType($v); |
544 | 549 | } |
545 | 550 | return $value; |
546 | 551 | } |
547 | 552 | |
548 | | - if ( strpos( $value, ': ' ) !== false && $first_character != '{' ) { |
549 | | - $array = explode( ': ', $value ); |
550 | | - $key = trim( $array[0] ); |
551 | | - array_shift( $array ); |
552 | | - $value = trim( implode( ': ', $array ) ); |
553 | | - $value = $this->_toType( $value ); |
554 | | - return array( $key => $value ); |
| 553 | + if (strpos($value,': ')!==false && $first_character != '{') { |
| 554 | + $array = explode(': ',$value); |
| 555 | + $key = trim($array[0]); |
| 556 | + array_shift($array); |
| 557 | + $value = trim(implode(': ',$array)); |
| 558 | + $value = $this->_toType($value); |
| 559 | + return array($key => $value); |
555 | 560 | } |
556 | 561 | |
557 | | - if ( $first_character == '{' && $last_character == '}' ) { |
558 | | - $innerValue = trim( substr ( $value, 1, -1 ) ); |
559 | | - if ( $innerValue === '' ) return array(); |
| 562 | + if ($first_character == '{' && $last_character == '}') { |
| 563 | + $innerValue = trim(substr ($value, 1, -1)); |
| 564 | + if ($innerValue === '') return array(); |
560 | 565 | // Inline Mapping |
561 | 566 | // Take out strings sequences and mappings |
562 | | - $explode = $this->_inlineEscape( $innerValue ); |
| 567 | + $explode = $this->_inlineEscape($innerValue); |
563 | 568 | // Propagate value array |
564 | 569 | $array = array(); |
565 | | - foreach ( $explode as $v ) { |
566 | | - $SubArr = $this->_toType( $v ); |
567 | | - if ( empty( $SubArr ) ) continue; |
568 | | - if ( is_array ( $SubArr ) ) { |
569 | | - $array[key( $SubArr )] = $SubArr[key( $SubArr )]; continue; |
| 570 | + foreach ($explode as $v) { |
| 571 | + $SubArr = $this->_toType($v); |
| 572 | + if (empty($SubArr)) continue; |
| 573 | + if (is_array ($SubArr)) { |
| 574 | + $array[key($SubArr)] = $SubArr[key($SubArr)]; continue; |
570 | 575 | } |
571 | 576 | $array[] = $SubArr; |
572 | 577 | } |
573 | 578 | return $array; |
574 | 579 | } |
575 | 580 | |
576 | | - if ( $value == 'null' || $value == 'NULL' || $value == 'Null' || $value == '' || $value == '~' ) { |
| 581 | + if ($value == 'null' || $value == 'NULL' || $value == 'Null' || $value == '' || $value == '~') { |
577 | 582 | return null; |
578 | 583 | } |
579 | 584 | |
580 | | - if ( intval( $first_character ) > 0 && preg_match ( '/^[1-9]+[0-9]*$/', $value ) ) { |
| 585 | + if (intval($first_character) > 0 && preg_match ('/^[1-9]+[0-9]*$/', $value)) { |
581 | 586 | $intvalue = (int)$value; |
582 | | - if ( $intvalue != PHP_INT_MAX ) |
| 587 | + if ($intvalue != PHP_INT_MAX) |
583 | 588 | $value = $intvalue; |
584 | 589 | return $value; |
585 | 590 | } |
586 | 591 | |
587 | | - if ( in_array( $value, |
588 | | - array( 'true', 'on', '+', 'yes', 'y', 'True', 'TRUE', 'On', 'ON', 'YES', 'Yes', 'Y' ) ) ) { |
| 592 | + if (in_array($value, |
| 593 | + array('true', 'on', '+', 'yes', 'y', 'True', 'TRUE', 'On', 'ON', 'YES', 'Yes', 'Y'))) { |
589 | 594 | return true; |
590 | 595 | } |
591 | 596 | |
592 | | - if ( in_array( strtolower( $value ), |
593 | | - array( 'false', 'off', '-', 'no', 'n' ) ) ) { |
| 597 | + if (in_array(strtolower($value), |
| 598 | + array('false', 'off', '-', 'no', 'n'))) { |
594 | 599 | return false; |
595 | 600 | } |
596 | 601 | |
597 | | - if ( is_numeric( $value ) ) { |
598 | | - if ( $value === '0' ) return 0; |
599 | | - if ( trim ( $value, 0 ) === $value ) |
| 602 | + if (is_numeric($value)) { |
| 603 | + if ($value === '0') return 0; |
| 604 | + if (trim ($value, 0) === $value) |
600 | 605 | $value = (float)$value; |
601 | 606 | return $value; |
602 | 607 | } |
— | — | @@ -608,7 +613,7 @@ |
609 | 614 | * @access private |
610 | 615 | * @return array |
611 | 616 | */ |
612 | | - private function _inlineEscape( $inline ) { |
| 617 | + private function _inlineEscape($inline) { |
613 | 618 | // There's gotta be a cleaner way to do this... |
614 | 619 | // While pure sequences seem to be nesting just fine, |
615 | 620 | // pure mappings and mappings with sequences inside can't go very |
— | — | @@ -620,42 +625,42 @@ |
621 | 626 | |
622 | 627 | // Check for strings |
623 | 628 | $regex = '/(?:(")|(?:\'))((?(1)[^"]+|[^\']+))(?(1)"|\')/'; |
624 | | - if ( preg_match_all( $regex, $inline, $strings ) ) { |
| 629 | + if (preg_match_all($regex,$inline,$strings)) { |
625 | 630 | $saved_strings = $strings[0]; |
626 | | - $inline = preg_replace( $regex, 'YAMLString', $inline ); |
| 631 | + $inline = preg_replace($regex,'YAMLString',$inline); |
627 | 632 | } |
628 | | - unset( $regex ); |
| 633 | + unset($regex); |
629 | 634 | |
630 | 635 | $i = 0; |
631 | 636 | do { |
632 | 637 | |
633 | 638 | // Check for sequences |
634 | | - while ( preg_match( '/\[([^{}\[\]]+)\]/U', $inline, $matchseqs ) ) { |
| 639 | + while (preg_match('/\[([^{}\[\]]+)\]/U',$inline,$matchseqs)) { |
635 | 640 | $seqs[] = $matchseqs[0]; |
636 | | - $inline = preg_replace( '/\[([^{}\[\]]+)\]/U', ( 'YAMLSeq' . ( count( $seqs ) - 1 ) . 's' ), $inline, 1 ); |
| 641 | + $inline = preg_replace('/\[([^{}\[\]]+)\]/U', ('YAMLSeq' . (count($seqs) - 1) . 's'), $inline, 1); |
637 | 642 | } |
638 | 643 | |
639 | 644 | // Check for mappings |
640 | | - while ( preg_match( '/{([^\[\]{}]+)}/U', $inline, $matchmaps ) ) { |
| 645 | + while (preg_match('/{([^\[\]{}]+)}/U',$inline,$matchmaps)) { |
641 | 646 | $maps[] = $matchmaps[0]; |
642 | | - $inline = preg_replace( '/{([^\[\]{}]+)}/U', ( 'YAMLMap' . ( count( $maps ) - 1 ) . 's' ), $inline, 1 ); |
| 647 | + $inline = preg_replace('/{([^\[\]{}]+)}/U', ('YAMLMap' . (count($maps) - 1) . 's'), $inline, 1); |
643 | 648 | } |
644 | 649 | |
645 | | - if ( $i++ >= 10 ) break; |
| 650 | + if ($i++ >= 10) break; |
646 | 651 | |
647 | | - } while ( strpos ( $inline, '[' ) !== false || strpos ( $inline, '{' ) !== false ); |
| 652 | + } while (strpos ($inline, '[') !== false || strpos ($inline, '{') !== false); |
648 | 653 | |
649 | | - $explode = explode( ', ', $inline ); |
| 654 | + $explode = explode(', ',$inline); |
650 | 655 | $stringi = 0; $i = 0; |
651 | 656 | |
652 | | - while ( 1 ) { |
| 657 | + while (1) { |
653 | 658 | |
654 | 659 | // Re-add the sequences |
655 | | - if ( !empty( $seqs ) ) { |
656 | | - foreach ( $explode as $key => $value ) { |
657 | | - if ( strpos( $value, 'YAMLSeq' ) !== false ) { |
658 | | - foreach ( $seqs as $seqk => $seq ) { |
659 | | - $explode[$key] = str_replace( ( 'YAMLSeq' . $seqk . 's' ), $seq, $value ); |
| 660 | + if (!empty($seqs)) { |
| 661 | + foreach ($explode as $key => $value) { |
| 662 | + if (strpos($value,'YAMLSeq') !== false) { |
| 663 | + foreach ($seqs as $seqk => $seq) { |
| 664 | + $explode[$key] = str_replace(('YAMLSeq'.$seqk.'s'),$seq,$value); |
660 | 665 | $value = $explode[$key]; |
661 | 666 | } |
662 | 667 | } |
— | — | @@ -663,11 +668,11 @@ |
664 | 669 | } |
665 | 670 | |
666 | 671 | // Re-add the mappings |
667 | | - if ( !empty( $maps ) ) { |
668 | | - foreach ( $explode as $key => $value ) { |
669 | | - if ( strpos( $value, 'YAMLMap' ) !== false ) { |
670 | | - foreach ( $maps as $mapk => $map ) { |
671 | | - $explode[$key] = str_replace( ( 'YAMLMap' . $mapk . 's' ), $map, $value ); |
| 672 | + if (!empty($maps)) { |
| 673 | + foreach ($explode as $key => $value) { |
| 674 | + if (strpos($value,'YAMLMap') !== false) { |
| 675 | + foreach ($maps as $mapk => $map) { |
| 676 | + $explode[$key] = str_replace(('YAMLMap'.$mapk.'s'), $map, $value); |
672 | 677 | $value = $explode[$key]; |
673 | 678 | } |
674 | 679 | } |
— | — | @@ -676,11 +681,11 @@ |
677 | 682 | |
678 | 683 | |
679 | 684 | // Re-add the strings |
680 | | - if ( !empty( $saved_strings ) ) { |
681 | | - foreach ( $explode as $key => $value ) { |
682 | | - while ( strpos( $value, 'YAMLString' ) !== false ) { |
683 | | - $explode[$key] = preg_replace( '/YAMLString/', $saved_strings[$stringi], $value, 1 ); |
684 | | - unset( $saved_strings[$stringi] ); |
| 685 | + if (!empty($saved_strings)) { |
| 686 | + foreach ($explode as $key => $value) { |
| 687 | + while (strpos($value,'YAMLString') !== false) { |
| 688 | + $explode[$key] = preg_replace('/YAMLString/',$saved_strings[$stringi],$value, 1); |
| 689 | + unset($saved_strings[$stringi]); |
685 | 690 | ++$stringi; |
686 | 691 | $value = $explode[$key]; |
687 | 692 | } |
— | — | @@ -688,72 +693,72 @@ |
689 | 694 | } |
690 | 695 | |
691 | 696 | $finished = true; |
692 | | - foreach ( $explode as $key => $value ) { |
693 | | - if ( strpos( $value, 'YAMLSeq' ) !== false ) { |
| 697 | + foreach ($explode as $key => $value) { |
| 698 | + if (strpos($value,'YAMLSeq') !== false) { |
694 | 699 | $finished = false; break; |
695 | 700 | } |
696 | | - if ( strpos( $value, 'YAMLMap' ) !== false ) { |
| 701 | + if (strpos($value,'YAMLMap') !== false) { |
697 | 702 | $finished = false; break; |
698 | 703 | } |
699 | | - if ( strpos( $value, 'YAMLString' ) !== false ) { |
| 704 | + if (strpos($value,'YAMLString') !== false) { |
700 | 705 | $finished = false; break; |
701 | 706 | } |
702 | 707 | } |
703 | | - if ( $finished ) break; |
| 708 | + if ($finished) break; |
704 | 709 | |
705 | 710 | $i++; |
706 | | - if ( $i > 10 ) |
| 711 | + if ($i > 10) |
707 | 712 | break; // Prevent infinite loops. |
708 | 713 | } |
709 | 714 | |
710 | 715 | return $explode; |
711 | 716 | } |
712 | 717 | |
713 | | - private function literalBlockContinues ( $line, $lineIndent ) { |
714 | | - if ( !trim( $line ) ) return true; |
715 | | - if ( strlen( $line ) - strlen( ltrim( $line ) ) > $lineIndent ) return true; |
| 718 | + private function literalBlockContinues ($line, $lineIndent) { |
| 719 | + if (!trim($line)) return true; |
| 720 | + if (strlen($line) - strlen(ltrim($line)) > $lineIndent) return true; |
716 | 721 | return false; |
717 | 722 | } |
718 | 723 | |
719 | | - private function referenceContentsByAlias ( $alias ) { |
| 724 | + private function referenceContentsByAlias ($alias) { |
720 | 725 | do { |
721 | | - if ( !isset( $this->SavedGroups[$alias] ) ) { echo "Bad group name: $alias."; break; } |
| 726 | + if (!isset($this->SavedGroups[$alias])) { echo "Bad group name: $alias."; break; } |
722 | 727 | $groupPath = $this->SavedGroups[$alias]; |
723 | 728 | $value = $this->result; |
724 | | - foreach ( $groupPath as $k ) { |
| 729 | + foreach ($groupPath as $k) { |
725 | 730 | $value = $value[$k]; |
726 | 731 | } |
727 | | - } while ( false ); |
| 732 | + } while (false); |
728 | 733 | return $value; |
729 | 734 | } |
730 | 735 | |
731 | | - private function addArrayInline ( $array, $indent ) { |
| 736 | + private function addArrayInline ($array, $indent) { |
732 | 737 | $CommonGroupPath = $this->path; |
733 | | - if ( empty ( $array ) ) return false; |
| 738 | + if (empty ($array)) return false; |
734 | 739 | |
735 | | - foreach ( $array as $k => $_ ) { |
736 | | - $this->addArray( array( $k => $_ ), $indent ); |
| 740 | + foreach ($array as $k => $_) { |
| 741 | + $this->addArray(array($k => $_), $indent); |
737 | 742 | $this->path = $CommonGroupPath; |
738 | 743 | } |
739 | 744 | return true; |
740 | 745 | } |
741 | 746 | |
742 | | - private function addArray ( $incoming_data, $incoming_indent ) { |
| 747 | + private function addArray ($incoming_data, $incoming_indent) { |
743 | 748 | |
744 | 749 | // print_r ($incoming_data); |
745 | 750 | |
746 | | - if ( count ( $incoming_data ) > 1 ) |
747 | | - return $this->addArrayInline ( $incoming_data, $incoming_indent ); |
| 751 | + if (count ($incoming_data) > 1) |
| 752 | + return $this->addArrayInline ($incoming_data, $incoming_indent); |
748 | 753 | |
749 | | - $key = key ( $incoming_data ); |
750 | | - $value = isset( $incoming_data[$key] ) ? $incoming_data[$key] : null; |
751 | | - if ( $key === '__!YAMLZero' ) $key = '0'; |
| 754 | + $key = key ($incoming_data); |
| 755 | + $value = isset($incoming_data[$key]) ? $incoming_data[$key] : null; |
| 756 | + if ($key === '__!YAMLZero') $key = '0'; |
752 | 757 | |
753 | | - if ( $incoming_indent == 0 && !$this->_containsGroupAlias && !$this->_containsGroupAnchor ) { // Shortcut for root-level values. |
754 | | - if ( $key || $key === '' || $key === '0' ) { |
| 758 | + if ($incoming_indent == 0 && !$this->_containsGroupAlias && !$this->_containsGroupAnchor) { // Shortcut for root-level values. |
| 759 | + if ($key || $key === '' || $key === '0') { |
755 | 760 | $this->result[$key] = $value; |
756 | 761 | } else { |
757 | | - $this->result[] = $value; end ( $this->result ); $key = key ( $this->result ); |
| 762 | + $this->result[] = $value; end ($this->result); $key = key ($this->result); |
758 | 763 | } |
759 | 764 | $this->path[$incoming_indent] = $key; |
760 | 765 | return; |
— | — | @@ -764,44 +769,47 @@ |
765 | 770 | $history = array(); |
766 | 771 | // Unfolding inner array tree. |
767 | 772 | $history[] = $_arr = $this->result; |
768 | | - foreach ( $this->path as $k ) { |
| 773 | + foreach ($this->path as $k) { |
769 | 774 | $history[] = $_arr = $_arr[$k]; |
770 | 775 | } |
771 | 776 | |
772 | | - if ( $this->_containsGroupAlias ) { |
773 | | - $value = $this->referenceContentsByAlias( $this->_containsGroupAlias ); |
| 777 | + if ($this->_containsGroupAlias) { |
| 778 | + $value = $this->referenceContentsByAlias($this->_containsGroupAlias); |
774 | 779 | $this->_containsGroupAlias = false; |
775 | 780 | } |
776 | 781 | |
777 | 782 | |
778 | 783 | // Adding string or numeric key to the innermost level or $this->arr. |
779 | | - if ( is_string( $key ) && $key == '<<' ) { |
780 | | - if ( !is_array ( $_arr ) ) { $_arr = array (); } |
| 784 | + if (is_string($key) && $key == '<<') { |
| 785 | + if (!is_array ($_arr)) { $_arr = array (); } |
781 | 786 | |
782 | | - $_arr = array_merge ( $_arr, $value ); |
783 | | - } else if ( $key || $key === '' || $key === '0' ) { |
784 | | - $_arr[$key] = $value; |
| 787 | + $_arr = array_merge ($_arr, $value); |
| 788 | + } else if ($key || $key === '' || $key === '0') { |
| 789 | + if (!is_array ($_arr)) |
| 790 | + $_arr = array ($key=>$value); |
| 791 | + else |
| 792 | + $_arr[$key] = $value; |
785 | 793 | } else { |
786 | | - if ( !is_array ( $_arr ) ) { $_arr = array ( $value ); $key = 0; } |
787 | | - else { $_arr[] = $value; end ( $_arr ); $key = key ( $_arr ); } |
| 794 | + if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; } |
| 795 | + else { $_arr[] = $value; end ($_arr); $key = key ($_arr); } |
788 | 796 | } |
789 | 797 | |
790 | | - $reverse_path = array_reverse( $this->path ); |
791 | | - $reverse_history = array_reverse ( $history ); |
| 798 | + $reverse_path = array_reverse($this->path); |
| 799 | + $reverse_history = array_reverse ($history); |
792 | 800 | $reverse_history[0] = $_arr; |
793 | | - $cnt = count( $reverse_history ) - 1; |
794 | | - for ( $i = 0; $i < $cnt; $i++ ) { |
795 | | - $reverse_history[$i + 1][$reverse_path[$i]] = $reverse_history[$i]; |
| 801 | + $cnt = count($reverse_history) - 1; |
| 802 | + for ($i = 0; $i < $cnt; $i++) { |
| 803 | + $reverse_history[$i+1][$reverse_path[$i]] = $reverse_history[$i]; |
796 | 804 | } |
797 | 805 | $this->result = $reverse_history[$cnt]; |
798 | 806 | |
799 | 807 | $this->path[$incoming_indent] = $key; |
800 | 808 | |
801 | | - if ( $this->_containsGroupAnchor ) { |
| 809 | + if ($this->_containsGroupAnchor) { |
802 | 810 | $this->SavedGroups[$this->_containsGroupAnchor] = $this->path; |
803 | | - if ( is_array ( $value ) ) { |
804 | | - $k = key ( $value ); |
805 | | - if ( !is_int ( $k ) ) { |
| 811 | + if (is_array ($value)) { |
| 812 | + $k = key ($value); |
| 813 | + if (!is_int ($k)) { |
806 | 814 | $this->SavedGroups[$this->_containsGroupAnchor][$incoming_indent + 2] = $k; |
807 | 815 | } |
808 | 816 | } |
— | — | @@ -810,209 +818,209 @@ |
811 | 819 | |
812 | 820 | } |
813 | 821 | |
814 | | - private static function startsLiteralBlock ( $line ) { |
815 | | - $lastChar = substr ( trim( $line ), -1 ); |
816 | | - if ( $lastChar != '>' && $lastChar != '|' ) return false; |
817 | | - if ( $lastChar == '|' ) return $lastChar; |
| 822 | + private static function startsLiteralBlock ($line) { |
| 823 | + $lastChar = substr (trim($line), -1); |
| 824 | + if ($lastChar != '>' && $lastChar != '|') return false; |
| 825 | + if ($lastChar == '|') return $lastChar; |
818 | 826 | // HTML tags should not be counted as literal blocks. |
819 | | - if ( preg_match ( '#<.*?>$#', $line ) ) return false; |
| 827 | + if (preg_match ('#<.*?>$#', $line)) return false; |
820 | 828 | return $lastChar; |
821 | 829 | } |
822 | 830 | |
823 | | - private static function greedilyNeedNextLine( $line ) { |
824 | | - $line = trim ( $line ); |
825 | | - if ( !strlen( $line ) ) return false; |
826 | | - if ( substr ( $line, -1, 1 ) == ']' ) return false; |
827 | | - if ( $line[0] == '[' ) return true; |
828 | | - if ( preg_match ( '#^[^:]+?:\s*\[#', $line ) ) return true; |
| 831 | + private static function greedilyNeedNextLine($line) { |
| 832 | + $line = trim ($line); |
| 833 | + if (!strlen($line)) return false; |
| 834 | + if (substr ($line, -1, 1) == ']') return false; |
| 835 | + if ($line[0] == '[') return true; |
| 836 | + if (preg_match ('#^[^:]+?:\s*\[#', $line)) return true; |
829 | 837 | return false; |
830 | 838 | } |
831 | 839 | |
832 | | - private function addLiteralLine ( $literalBlock, $line, $literalBlockStyle ) { |
833 | | - $line = self::stripIndent( $line ); |
834 | | - $line = rtrim ( $line, "\r\n\t " ) . "\n"; |
835 | | - if ( $literalBlockStyle == '|' ) { |
| 840 | + private function addLiteralLine ($literalBlock, $line, $literalBlockStyle) { |
| 841 | + $line = self::stripIndent($line); |
| 842 | + $line = rtrim ($line, "\r\n\t ") . "\n"; |
| 843 | + if ($literalBlockStyle == '|') { |
836 | 844 | return $literalBlock . $line; |
837 | 845 | } |
838 | | - if ( strlen( $line ) == 0 ) |
839 | | - return rtrim( $literalBlock, ' ' ) . "\n"; |
840 | | - if ( $line == "\n" && $literalBlockStyle == '>' ) { |
841 | | - return rtrim ( $literalBlock, " \t" ) . "\n"; |
| 846 | + if (strlen($line) == 0) |
| 847 | + return rtrim($literalBlock, ' ') . "\n"; |
| 848 | + if ($line == "\n" && $literalBlockStyle == '>') { |
| 849 | + return rtrim ($literalBlock, " \t") . "\n"; |
842 | 850 | } |
843 | | - if ( $line != "\n" ) |
844 | | - $line = trim ( $line, "\r\n " ) . " "; |
| 851 | + if ($line != "\n") |
| 852 | + $line = trim ($line, "\r\n ") . " "; |
845 | 853 | return $literalBlock . $line; |
846 | 854 | } |
847 | 855 | |
848 | | - function revertLiteralPlaceHolder ( $lineArray, $literalBlock ) { |
849 | | - foreach ( $lineArray as $k => $_ ) { |
850 | | - if ( is_array( $_ ) ) |
851 | | - $lineArray[$k] = $this->revertLiteralPlaceHolder ( $_, $literalBlock ); |
852 | | - else if ( substr( $_, -1 * strlen ( $this->LiteralPlaceHolder ) ) == $this->LiteralPlaceHolder ) |
853 | | - $lineArray[$k] = rtrim ( $literalBlock, " \r\n" ); |
| 856 | + function revertLiteralPlaceHolder ($lineArray, $literalBlock) { |
| 857 | + foreach ($lineArray as $k => $_) { |
| 858 | + if (is_array($_)) |
| 859 | + $lineArray[$k] = $this->revertLiteralPlaceHolder ($_, $literalBlock); |
| 860 | + else if (substr($_, -1 * strlen ($this->LiteralPlaceHolder)) == $this->LiteralPlaceHolder) |
| 861 | + $lineArray[$k] = rtrim ($literalBlock, " \r\n"); |
854 | 862 | } |
855 | 863 | return $lineArray; |
856 | 864 | } |
857 | 865 | |
858 | | - private static function stripIndent ( $line, $indent = -1 ) { |
859 | | - if ( $indent == -1 ) $indent = strlen( $line ) - strlen( ltrim( $line ) ); |
860 | | - return substr ( $line, $indent ); |
| 866 | + private static function stripIndent ($line, $indent = -1) { |
| 867 | + if ($indent == -1) $indent = strlen($line) - strlen(ltrim($line)); |
| 868 | + return substr ($line, $indent); |
861 | 869 | } |
862 | 870 | |
863 | | - private function getParentPathByIndent ( $indent ) { |
864 | | - if ( $indent == 0 ) return array(); |
| 871 | + private function getParentPathByIndent ($indent) { |
| 872 | + if ($indent == 0) return array(); |
865 | 873 | $linePath = $this->path; |
866 | 874 | do { |
867 | | - end( $linePath ); $lastIndentInParentPath = key( $linePath ); |
868 | | - if ( $indent <= $lastIndentInParentPath ) array_pop ( $linePath ); |
869 | | - } while ( $indent <= $lastIndentInParentPath ); |
| 875 | + end($linePath); $lastIndentInParentPath = key($linePath); |
| 876 | + if ($indent <= $lastIndentInParentPath) array_pop ($linePath); |
| 877 | + } while ($indent <= $lastIndentInParentPath); |
870 | 878 | return $linePath; |
871 | 879 | } |
872 | 880 | |
873 | 881 | |
874 | | - private function clearBiggerPathValues ( $indent ) { |
| 882 | + private function clearBiggerPathValues ($indent) { |
875 | 883 | |
876 | 884 | |
877 | | - if ( $indent == 0 ) $this->path = array(); |
878 | | - if ( empty ( $this->path ) ) return true; |
| 885 | + if ($indent == 0) $this->path = array(); |
| 886 | + if (empty ($this->path)) return true; |
879 | 887 | |
880 | | - foreach ( $this->path as $k => $_ ) { |
881 | | - if ( $k > $indent ) unset ( $this->path[$k] ); |
| 888 | + foreach ($this->path as $k => $_) { |
| 889 | + if ($k > $indent) unset ($this->path[$k]); |
882 | 890 | } |
883 | 891 | |
884 | 892 | return true; |
885 | 893 | } |
886 | 894 | |
887 | 895 | |
888 | | - private static function isComment ( $line ) { |
889 | | - if ( !$line ) return false; |
890 | | - if ( $line[0] == '#' ) return true; |
891 | | - if ( trim( $line, " \r\n\t" ) == '---' ) return true; |
| 896 | + private static function isComment ($line) { |
| 897 | + if (!$line) return false; |
| 898 | + if ($line[0] == '#') return true; |
| 899 | + if (trim($line, " \r\n\t") == '---') return true; |
892 | 900 | return false; |
893 | 901 | } |
894 | 902 | |
895 | | - private static function isEmpty ( $line ) { |
896 | | - return ( trim ( $line ) === '' ); |
| 903 | + private static function isEmpty ($line) { |
| 904 | + return (trim ($line) === ''); |
897 | 905 | } |
898 | 906 | |
899 | 907 | |
900 | | - private function isArrayElement ( $line ) { |
901 | | - if ( !$line ) return false; |
902 | | - if ( $line[0] != '-' ) return false; |
903 | | - if ( strlen ( $line ) > 3 ) |
904 | | - if ( substr( $line, 0, 3 ) == '---' ) return false; |
| 908 | + private function isArrayElement ($line) { |
| 909 | + if (!$line) return false; |
| 910 | + if ($line[0] != '-') return false; |
| 911 | + if (strlen ($line) > 3) |
| 912 | + if (substr($line,0,3) == '---') return false; |
905 | 913 | |
906 | 914 | return true; |
907 | 915 | } |
908 | 916 | |
909 | | - private function isHashElement ( $line ) { |
910 | | - return strpos( $line, ':' ); |
| 917 | + private function isHashElement ($line) { |
| 918 | + return strpos($line, ':'); |
911 | 919 | } |
912 | 920 | |
913 | | - private function isLiteral ( $line ) { |
914 | | - if ( $this->isArrayElement( $line ) ) return false; |
915 | | - if ( $this->isHashElement( $line ) ) return false; |
| 921 | + private function isLiteral ($line) { |
| 922 | + if ($this->isArrayElement($line)) return false; |
| 923 | + if ($this->isHashElement($line)) return false; |
916 | 924 | return true; |
917 | 925 | } |
918 | 926 | |
919 | 927 | |
920 | | - private static function unquote ( $value ) { |
921 | | - if ( !$value ) return $value; |
922 | | - if ( !is_string( $value ) ) return $value; |
923 | | - if ( $value[0] == '\'' ) return trim ( $value, '\'' ); |
924 | | - if ( $value[0] == '"' ) return trim ( $value, '"' ); |
| 928 | + private static function unquote ($value) { |
| 929 | + if (!$value) return $value; |
| 930 | + if (!is_string($value)) return $value; |
| 931 | + if ($value[0] == '\'') return trim ($value, '\''); |
| 932 | + if ($value[0] == '"') return trim ($value, '"'); |
925 | 933 | return $value; |
926 | 934 | } |
927 | 935 | |
928 | | - private function startsMappedSequence ( $line ) { |
929 | | - return ( $line[0] == '-' && substr ( $line, -1, 1 ) == ':' ); |
| 936 | + private function startsMappedSequence ($line) { |
| 937 | + return ($line[0] == '-' && substr ($line, -1, 1) == ':'); |
930 | 938 | } |
931 | 939 | |
932 | | - private function returnMappedSequence ( $line ) { |
| 940 | + private function returnMappedSequence ($line) { |
933 | 941 | $array = array(); |
934 | | - $key = self::unquote( trim( substr( $line, 1, -1 ) ) ); |
| 942 | + $key = self::unquote(trim(substr($line,1,-1))); |
935 | 943 | $array[$key] = array(); |
936 | | - $this->delayedPath = array( strpos ( $line, $key ) + $this->indent => $key ); |
937 | | - return array( $array ); |
| 944 | + $this->delayedPath = array(strpos ($line, $key) + $this->indent => $key); |
| 945 | + return array($array); |
938 | 946 | } |
939 | 947 | |
940 | | - private function returnMappedValue ( $line ) { |
| 948 | + private function returnMappedValue ($line) { |
941 | 949 | $array = array(); |
942 | | - $key = self::unquote ( trim( substr( $line, 0, -1 ) ) ); |
| 950 | + $key = self::unquote (trim(substr($line,0,-1))); |
943 | 951 | $array[$key] = ''; |
944 | 952 | return $array; |
945 | 953 | } |
946 | 954 | |
947 | | - private function startsMappedValue ( $line ) { |
948 | | - return ( substr ( $line, -1, 1 ) == ':' ); |
| 955 | + private function startsMappedValue ($line) { |
| 956 | + return (substr ($line, -1, 1) == ':'); |
949 | 957 | } |
950 | 958 | |
951 | | - private function isPlainArray ( $line ) { |
952 | | - return ( $line[0] == '[' && substr ( $line, -1, 1 ) == ']' ); |
| 959 | + private function isPlainArray ($line) { |
| 960 | + return ($line[0] == '[' && substr ($line, -1, 1) == ']'); |
953 | 961 | } |
954 | 962 | |
955 | | - private function returnPlainArray ( $line ) { |
956 | | - return $this->_toType( $line ); |
957 | | - } |
| 963 | + private function returnPlainArray ($line) { |
| 964 | + return $this->_toType($line); |
| 965 | + } |
958 | 966 | |
959 | | - private function returnKeyValuePair ( $line ) { |
| 967 | + private function returnKeyValuePair ($line) { |
960 | 968 | $array = array(); |
961 | 969 | $key = ''; |
962 | | - if ( strpos ( $line, ':' ) ) { |
| 970 | + if (strpos ($line, ':')) { |
963 | 971 | // It's a key/value pair most likely |
964 | 972 | // If the key is in double quotes pull it out |
965 | | - if ( ( $line[0] == '"' || $line[0] == "'" ) && preg_match( '/^(["\'](.*)["\'](\s)*:)/', $line, $matches ) ) { |
966 | | - $value = trim( str_replace( $matches[1], '', $line ) ); |
| 973 | + if (($line[0] == '"' || $line[0] == "'") && preg_match('/^(["\'](.*)["\'](\s)*:)/',$line,$matches)) { |
| 974 | + $value = trim(str_replace($matches[1],'',$line)); |
967 | 975 | $key = $matches[2]; |
968 | 976 | } else { |
969 | 977 | // Do some guesswork as to the key and the value |
970 | | - $explode = explode( ':', $line ); |
971 | | - $key = trim( $explode[0] ); |
972 | | - array_shift( $explode ); |
973 | | - $value = trim( implode( ':', $explode ) ); |
| 978 | + $explode = explode(':',$line); |
| 979 | + $key = trim($explode[0]); |
| 980 | + array_shift($explode); |
| 981 | + $value = trim(implode(':',$explode)); |
974 | 982 | } |
975 | 983 | // Set the type of the value. Int, string, etc |
976 | | - $value = $this->_toType( $value ); |
977 | | - if ( $key === '0' ) $key = '__!YAMLZero'; |
| 984 | + $value = $this->_toType($value); |
| 985 | + if ($key === '0') $key = '__!YAMLZero'; |
978 | 986 | $array[$key] = $value; |
979 | 987 | } else { |
980 | | - $array = array ( $line ); |
| 988 | + $array = array ($line); |
981 | 989 | } |
982 | 990 | return $array; |
983 | 991 | |
984 | 992 | } |
985 | 993 | |
986 | 994 | |
987 | | - private function returnArrayElement ( $line ) { |
988 | | - if ( strlen( $line ) <= 1 ) return array( array() ); // Weird %) |
| 995 | + private function returnArrayElement ($line) { |
| 996 | + if (strlen($line) <= 1) return array(array()); // Weird %) |
989 | 997 | $array = array(); |
990 | | - $value = trim( substr( $line, 1 ) ); |
991 | | - $value = $this->_toType( $value ); |
| 998 | + $value = trim(substr($line,1)); |
| 999 | + $value = $this->_toType($value); |
992 | 1000 | $array[] = $value; |
993 | 1001 | return $array; |
994 | 1002 | } |
995 | 1003 | |
996 | 1004 | |
997 | | - private function nodeContainsGroup ( $line ) { |
| 1005 | + private function nodeContainsGroup ($line) { |
998 | 1006 | $symbolsForReference = 'A-z0-9_\-'; |
999 | | - if ( strpos( $line, '&' ) === false && strpos( $line, '*' ) === false ) return false; // Please die fast ;-) |
1000 | | - if ( $line[0] == '&' && preg_match( '/^(&[' . $symbolsForReference . ']+)/', $line, $matches ) ) return $matches[1]; |
1001 | | - if ( $line[0] == '*' && preg_match( '/^(\*[' . $symbolsForReference . ']+)/', $line, $matches ) ) return $matches[1]; |
1002 | | - if ( preg_match( '/(&[' . $symbolsForReference . ']+)$/', $line, $matches ) ) return $matches[1]; |
1003 | | - if ( preg_match( '/(\*[' . $symbolsForReference . ']+$)/', $line, $matches ) ) return $matches[1]; |
1004 | | - if ( preg_match ( '#^\s*<<\s*:\s*(\*[^\s]+).*$#', $line, $matches ) ) return $matches[1]; |
| 1007 | + if (strpos($line, '&') === false && strpos($line, '*') === false) return false; // Please die fast ;-) |
| 1008 | + if ($line[0] == '&' && preg_match('/^(&['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1]; |
| 1009 | + if ($line[0] == '*' && preg_match('/^(\*['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1]; |
| 1010 | + if (preg_match('/(&['.$symbolsForReference.']+)$/', $line, $matches)) return $matches[1]; |
| 1011 | + if (preg_match('/(\*['.$symbolsForReference.']+$)/', $line, $matches)) return $matches[1]; |
| 1012 | + if (preg_match ('#^\s*<<\s*:\s*(\*[^\s]+).*$#', $line, $matches)) return $matches[1]; |
1005 | 1013 | return false; |
1006 | 1014 | |
1007 | 1015 | } |
1008 | 1016 | |
1009 | | - private function addGroup ( $line, $group ) { |
1010 | | - if ( $group[0] == '&' ) $this->_containsGroupAnchor = substr ( $group, 1 ); |
1011 | | - if ( $group[0] == '*' ) $this->_containsGroupAlias = substr ( $group, 1 ); |
1012 | | - // print_r ($this->path); |
| 1017 | + private function addGroup ($line, $group) { |
| 1018 | + if ($group[0] == '&') $this->_containsGroupAnchor = substr ($group, 1); |
| 1019 | + if ($group[0] == '*') $this->_containsGroupAlias = substr ($group, 1); |
| 1020 | + //print_r ($this->path); |
1013 | 1021 | } |
1014 | 1022 | |
1015 | | - private function stripGroup ( $line, $group ) { |
1016 | | - $line = trim( str_replace( $group, '', $line ) ); |
| 1023 | + private function stripGroup ($line, $group) { |
| 1024 | + $line = trim(str_replace($group, '', $line)); |
1017 | 1025 | return $line; |
1018 | 1026 | } |
1019 | 1027 | } |
— | — | @@ -1020,13 +1028,13 @@ |
1021 | 1029 | // Enable use of Spyc from command line |
1022 | 1030 | // The syntax is the following: php spyc.php spyc.yaml |
1023 | 1031 | |
1024 | | -define ( 'SPYC_FROM_COMMAND_LINE', false ); |
| 1032 | +define ('SPYC_FROM_COMMAND_LINE', false); |
1025 | 1033 | |
1026 | 1034 | do { |
1027 | | - if ( !SPYC_FROM_COMMAND_LINE ) break; |
1028 | | - if ( empty ( $_SERVER['argc'] ) || $_SERVER['argc'] < 2 ) break; |
1029 | | - if ( empty ( $_SERVER['PHP_SELF'] ) || $_SERVER['PHP_SELF'] != 'spyc.php' ) break; |
| 1035 | + if (!SPYC_FROM_COMMAND_LINE) break; |
| 1036 | + if (empty ($_SERVER['argc']) || $_SERVER['argc'] < 2) break; |
| 1037 | + if (empty ($_SERVER['PHP_SELF']) || $_SERVER['PHP_SELF'] != 'spyc.php') break; |
1030 | 1038 | $file = $argv[1]; |
1031 | | - printf ( "Spyc loading file: %s\n", $file ); |
1032 | | - print_r ( spyc_load_file ( $file ) ); |
1033 | | -} while ( 0 ); |
\ No newline at end of file |
| 1039 | + printf ("Spyc loading file: %s\n", $file); |
| 1040 | + print_r (spyc_load_file ($file)); |
| 1041 | +} while (0); |
\ No newline at end of file |