Commit 1632bf3a authored by Luke√'s avatar Luke√

Update spyc.php

parent 1e69dbad
<?php <?php
/** /**
* Spyc -- A Simple PHP YAML Class * Spyc -- A Simple PHP YAML Class
* @version 0.4.5 * @version 0.6.2
* @author Vlad Andersen <vlad.andersen@gmail.com> * @author Vlad Andersen <vlad.andersen@gmail.com>
* @author Chris Wanstrath <chris@ozmm.org> * @author Chris Wanstrath <chris@ozmm.org>
* @link http://code.google.com/p/spyc/ * @link https://github.com/mustangostang/spyc/
* @copyright Copyright 2005-2006 Chris Wanstrath, 2006-2009 Vlad Andersen * @copyright Copyright 2005-2006 Chris Wanstrath, 2006-2011 Vlad Andersen
* @license http://www.opensource.org/licenses/mit-license.php MIT License * @license http://www.opensource.org/licenses/mit-license.php MIT License
* @package Spyc * @package Spyc
*/ */
...@@ -32,7 +32,20 @@ if (!function_exists('spyc_load_file')) { ...@@ -32,7 +32,20 @@ if (!function_exists('spyc_load_file')) {
} }
} }
/** if (!function_exists('spyc_dump')) {
/**
* Dumps array to YAML.
* @param array $data Array.
* @return string
*/
function spyc_dump ($data) {
return Spyc::YAMLDump($data, false, false, true);
}
}
if (!class_exists('Spyc')) {
/**
* The Simple PHP YAML Class. * The Simple PHP YAML Class.
* *
* This class can be used to read a YAML file and convert its contents * This class can be used to read a YAML file and convert its contents
...@@ -54,10 +67,12 @@ if (!function_exists('spyc_load_file')) { ...@@ -54,10 +67,12 @@ if (!function_exists('spyc_load_file')) {
* </code> * </code>
* @package Spyc * @package Spyc
*/ */
class Spyc { class Spyc {
// SETTINGS // SETTINGS
const REMPTY = "\0\0\0\0\0";
/** /**
* Setting this to true will force YAMLDump to enclose any string value in * Setting this to true will force YAMLDump to enclose any string value in
* quotes. False by default. * quotes. False by default.
...@@ -100,13 +115,13 @@ class Spyc { ...@@ -100,13 +115,13 @@ class Spyc {
*/ */
public $_nodeId; public $_nodeId;
/** /**
* Load a valid YAML string to Spyc. * Load a valid YAML string to Spyc.
* @param string $input * @param string $input
* @return array * @return array
*/ */
public function load ($input) { public function load ($input) {
return $this->__loadString($input); return $this->_loadString($input);
} }
/** /**
...@@ -115,7 +130,7 @@ class Spyc { ...@@ -115,7 +130,7 @@ class Spyc {
* @return array * @return array
*/ */
public function loadFile ($file) { public function loadFile ($file) {
return $this->__load($file); return $this->_load($file);
} }
/** /**
...@@ -135,7 +150,7 @@ class Spyc { ...@@ -135,7 +150,7 @@ class Spyc {
*/ */
public static function YAMLLoad($input) { public static function YAMLLoad($input) {
$Spyc = new Spyc; $Spyc = new Spyc;
return $Spyc->__load($input); return $Spyc->_load($input);
} }
/** /**
...@@ -159,7 +174,7 @@ class Spyc { ...@@ -159,7 +174,7 @@ class Spyc {
*/ */
public static function YAMLLoadString($input) { public static function YAMLLoadString($input) {
$Spyc = new Spyc; $Spyc = new Spyc;
return $Spyc->__loadString($input); return $Spyc->_loadString($input);
} }
/** /**
...@@ -178,13 +193,14 @@ class Spyc { ...@@ -178,13 +193,14 @@ class Spyc {
* *
* @access public * @access public
* @return string * @return string
* @param array $array PHP array * @param array|\stdClass $array PHP array
* @param int $indent Pass in false to use the default, which is 2 * @param int $indent Pass in false to use the default, which is 2
* @param int $wordwrap Pass in 0 for no wordwrap, false for default (40) * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40)
* @param bool $no_opening_dashes Do not start YAML file with "---\n"
*/ */
public static function YAMLDump($array,$indent = false,$wordwrap = false) { public static function YAMLDump($array, $indent = false, $wordwrap = false, $no_opening_dashes = false) {
$spyc = new Spyc; $spyc = new Spyc;
return $spyc->dump($array,$indent,$wordwrap); return $spyc->dump($array, $indent, $wordwrap, $no_opening_dashes);
} }
...@@ -208,7 +224,7 @@ class Spyc { ...@@ -208,7 +224,7 @@ class Spyc {
* @param int $indent Pass in false to use the default, which is 2 * @param int $indent Pass in false to use the default, which is 2
* @param int $wordwrap Pass in 0 for no wordwrap, false for default (40) * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40)
*/ */
public function dump($array,$indent = false,$wordwrap = false) { public function dump($array,$indent = false,$wordwrap = false, $no_opening_dashes = false) {
// Dumps to some very clean YAML. We'll have to add some more features // Dumps to some very clean YAML. We'll have to add some more features
// and options soon. And better support for folding. // and options soon. And better support for folding.
...@@ -226,16 +242,16 @@ class Spyc { ...@@ -226,16 +242,16 @@ class Spyc {
} }
// New YAML document // New YAML document
$string = "---\n"; $string = "";
if (!$no_opening_dashes) $string = "---\n";
// Start at the base of the array and move through it. // Start at the base of the array and move through it.
if ($array) { if ($array) {
$array = (array)$array; $array = (array)$array;
$first_key = key($array);
$previous_key = -1; $previous_key = -1;
foreach ($array as $key => $value) { foreach ($array as $key => $value) {
$string .= $this->_yamlize($key,$value,0,$previous_key, $first_key); if (!isset($first_key)) $first_key = $key;
$string .= $this->_yamlize($key,$value,0,$previous_key, $first_key, $array);
$previous_key = $key; $previous_key = $key;
} }
} }
...@@ -250,20 +266,21 @@ class Spyc { ...@@ -250,20 +266,21 @@ class Spyc {
* @param $value The value of the item * @param $value The value of the item
* @param $indent The indent of the current node * @param $indent The indent of the current node
*/ */
private function _yamlize($key,$value,$indent, $previous_key = -1, $first_key = 0) { private function _yamlize($key,$value,$indent, $previous_key = -1, $first_key = 0, $source_array = null) {
if(is_object($value)) $value = (array)$value;
if (is_array($value)) { if (is_array($value)) {
if (empty ($value)) if (empty ($value))
return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key); return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key, $source_array);
// It has children. What to do? // It has children. What to do?
// Make it the right kind of item // Make it the right kind of item
$string = $this->_dumpNode($key, NULL, $indent, $previous_key, $first_key); $string = $this->_dumpNode($key, self::REMPTY, $indent, $previous_key, $first_key, $source_array);
// Add the indent // Add the indent
$indent += $this->_dumpIndent; $indent += $this->_dumpIndent;
// Yamlize the array // Yamlize the array
$string .= $this->_yamlizeArray($value,$indent); $string .= $this->_yamlizeArray($value,$indent);
} elseif (!is_array($value)) { } elseif (!is_array($value)) {
// It doesn't have children. Yip. // It doesn't have children. Yip.
$string = $this->_dumpNode($key, $value, $indent, $previous_key, $first_key); $string = $this->_dumpNode($key, $value, $indent, $previous_key, $first_key, $source_array);
} }
return $string; return $string;
} }
...@@ -279,9 +296,9 @@ class Spyc { ...@@ -279,9 +296,9 @@ class Spyc {
if (is_array($array)) { if (is_array($array)) {
$string = ''; $string = '';
$previous_key = -1; $previous_key = -1;
$first_key = key($array);
foreach ($array as $key => $value) { foreach ($array as $key => $value) {
$string .= $this->_yamlize($key, $value, $indent, $previous_key, $first_key); if (!isset($first_key)) $first_key = $key;
$string .= $this->_yamlize($key, $value, $indent, $previous_key, $first_key, $array);
$previous_key = $key; $previous_key = $key;
} }
return $string; return $string;
...@@ -298,31 +315,44 @@ class Spyc { ...@@ -298,31 +315,44 @@ class Spyc {
* @param $value The value of the item * @param $value The value of the item
* @param $indent The indent of the current node * @param $indent The indent of the current node
*/ */
private function _dumpNode($key, $value, $indent, $previous_key = -1, $first_key = 0) { private function _dumpNode($key, $value, $indent, $previous_key = -1, $first_key = 0, $source_array = null) {
// do some folding here, for blocks // do some folding here, for blocks
if (is_string ($value) && ((strpos($value,"\n") !== false || strpos($value,": ") !== false || strpos($value,"- ") !== false || 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,"{") !== false || strpos($value,"}") !== false) || substr ($value, -1, 1) == ':')) { 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) == ':')
) {
$value = $this->_doLiteralBlock($value,$indent); $value = $this->_doLiteralBlock($value,$indent);
} else { } else {
$value = $this->_doFolding($value,$indent); $value = $this->_doFolding($value,$indent);
if (is_bool($value)) {
$value = ($value) ? "true" : "false";
}
} }
if ($value === array()) $value = '[ ]'; if ($value === array()) $value = '[ ]';
if ($value === "") $value = '""';
if (self::isTranslationWord($value)) {
$value = $this->_doLiteralBlock($value, $indent);
}
if (trim ($value) != $value)
$value = $this->_doLiteralBlock($value,$indent);
if (is_bool($value)) {
$value = $value ? "true" : "false";
}
if ($value === null) $value = 'null';
if ($value === "'" . self::REMPTY . "'") $value = null;
$spaces = str_repeat(' ',$indent); $spaces = str_repeat(' ',$indent);
if (is_int($key) && $key - 1 == $previous_key && $first_key===0) { //if (is_int($key) && $key - 1 == $previous_key && $first_key===0) {
if (is_array ($source_array) && array_keys($source_array) === range(0, count($source_array) - 1)) {
// It's a sequence // It's a sequence
$string = $spaces.'- '.$value."\n"; $string = $spaces.'- '.$value."\n";
} else { } else {
if ($first_key===0) throw new Exception('Keys are all screwy. The first one was zero, now it\'s "'. $key .'"'); // if ($first_key===0) throw new Exception('Keys are all screwy. The first one was zero, now it\'s "'. $key .'"');
// It's mapped // It's mapped
if (strpos($key, ":") !== false) { $key = '"' . $key . '"'; } if (strpos($key, ":") !== false || strpos($key, "#") !== false) { $key = '"' . $key . '"'; }
$string = $spaces.$key.': '.$value."\n"; $string = rtrim ($spaces.$key.': '.$value)."\n";
} }
return $string; return $string;
} }
...@@ -335,6 +365,7 @@ class Spyc { ...@@ -335,6 +365,7 @@ class Spyc {
* @param $indent int The value of the indent * @param $indent int The value of the indent
*/ */
private function _doLiteralBlock($value,$indent) { private function _doLiteralBlock($value,$indent) {
if ($value === "\n") return '\n';
if (strpos($value, "\n") === false && strpos($value, "'") === false) { if (strpos($value, "\n") === false && strpos($value, "'") === false) {
return sprintf ("'%s'", $value); return sprintf ("'%s'", $value);
} }
...@@ -343,10 +374,18 @@ class Spyc { ...@@ -343,10 +374,18 @@ class Spyc {
} }
$exploded = explode("\n",$value); $exploded = explode("\n",$value);
$newValue = '|'; $newValue = '|';
if (isset($exploded[0]) && ($exploded[0] == "|" || $exploded[0] == "|-" || $exploded[0] == ">")) {
$newValue = $exploded[0];
unset($exploded[0]);
}
$indent += $this->_dumpIndent; $indent += $this->_dumpIndent;
$spaces = str_repeat(' ',$indent); $spaces = str_repeat(' ',$indent);
foreach ($exploded as $line) { foreach ($exploded as $line) {
$newValue .= "\n" . $spaces . trim($line); $line = trim($line);
if (strpos($line, '"') === 0 && strrpos($line, '"') == (strlen($line)-1) || strpos($line, "'") === 0 && strrpos($line, "'") == (strlen($line)-1)) {
$line = substr($line, 1, -1);
}
$newValue .= "\n" . $spaces . ($line);
} }
return $newValue; return $newValue;
} }
...@@ -366,7 +405,9 @@ class Spyc { ...@@ -366,7 +405,9 @@ class Spyc {
$wrapped = wordwrap($value,$this->_dumpWordWrap,"\n$indent"); $wrapped = wordwrap($value,$this->_dumpWordWrap,"\n$indent");
$value = ">\n".$indent.$wrapped; $value = ">\n".$indent.$wrapped;
} else { } else {
if ($this->setting_dump_force_quotes && is_string ($value)) if ($this->setting_dump_force_quotes && is_string ($value) && $value !== self::REMPTY)
$value = '"' . $value . '"';
if (is_numeric($value) && is_string($value))
$value = '"' . $value . '"'; $value = '"' . $value . '"';
} }
...@@ -374,14 +415,68 @@ class Spyc { ...@@ -374,14 +415,68 @@ class Spyc {
return $value; return $value;
} }
private function isTrueWord($value) {
$words = self::getTranslations(array('true', 'on', 'yes', 'y'));
return in_array($value, $words, true);
}
private function isFalseWord($value) {
$words = self::getTranslations(array('false', 'off', 'no', 'n'));
return in_array($value, $words, true);
}
private function isNullWord($value) {
$words = self::getTranslations(array('null', '~'));
return in_array($value, $words, true);
}
private function isTranslationWord($value) {
return (
self::isTrueWord($value) ||
self::isFalseWord($value) ||
self::isNullWord($value)
);
}
/**
* Coerce a string into a native type
* Reference: http://yaml.org/type/bool.html
* TODO: Use only words from the YAML spec.
* @access private
* @param $value The value to coerce
*/
private function coerceValue(&$value) {
if (self::isTrueWord($value)) {
$value = true;
} else if (self::isFalseWord($value)) {
$value = false;
} else if (self::isNullWord($value)) {
$value = null;
}
}
/**
* Given a set of words, perform the appropriate translations on them to
* match the YAML 1.1 specification for type coercing.
* @param $words The words to translate
* @access private
*/
private static function getTranslations(array $words) {
$result = array();
foreach ($words as $i) {
$result = array_merge($result, array(ucfirst($i), strtoupper($i), strtolower($i)));
}
return $result;
}
// LOADING FUNCTIONS // LOADING FUNCTIONS
private function __load($input) { private function _load($input) {
$Source = $this->loadFromSource($input); $Source = $this->loadFromSource($input);
return $this->loadWithSource($Source); return $this->loadWithSource($Source);
} }
private function __loadString($input) { private function _loadString($input) {
$Source = $this->loadFromString($input); $Source = $this->loadFromString($input);
return $this->loadWithSource($Source); return $this->loadWithSource($Source);
} }
...@@ -389,7 +484,7 @@ class Spyc { ...@@ -389,7 +484,7 @@ class Spyc {
private function loadWithSource($Source) { private function loadWithSource($Source) {
if (empty ($Source)) return array(); if (empty ($Source)) return array();
if ($this->setting_use_syck_is_possible && function_exists ('syck_load')) { if ($this->setting_use_syck_is_possible && function_exists ('syck_load')) {
$array = syck_load (implode ('', $Source)); $array = syck_load (implode ("\n", $Source));
return is_array($array) ? $array : array(); return is_array($array) ? $array : array();
} }
...@@ -411,26 +506,24 @@ class Spyc { ...@@ -411,26 +506,24 @@ class Spyc {
if ($literalBlockStyle) { if ($literalBlockStyle) {
$line = rtrim ($line, $literalBlockStyle . " \n"); $line = rtrim ($line, $literalBlockStyle . " \n");
$literalBlock = ''; $literalBlock = '';
$line .= $this->LiteralPlaceHolder; $line .= ' '.$this->LiteralPlaceHolder;
$literal_block_indent = strlen($Source[$i+1]) - strlen(ltrim($Source[$i+1]));
while (++$i < $cnt && $this->literalBlockContinues($Source[$i], $this->indent)) { while (++$i < $cnt && $this->literalBlockContinues($Source[$i], $this->indent)) {
$literalBlock = $this->addLiteralLine($literalBlock, $Source[$i], $literalBlockStyle); $literalBlock = $this->addLiteralLine($literalBlock, $Source[$i], $literalBlockStyle, $literal_block_indent);
} }
$i--; $i--;
} }
// Strip out comments
if (strpos ($line, '#')) {
$line = preg_replace('/\s*#([^"\']+)$/','',$line);
}
while (++$i < $cnt && self::greedilyNeedNextLine($line)) { while (++$i < $cnt && self::greedilyNeedNextLine($line)) {
$line = rtrim ($line, " \n\t\r") . ' ' . ltrim ($Source[$i], " \t"); $line = rtrim ($line, " \n\t\r") . ' ' . ltrim ($Source[$i], " \t");
} }
$i--; $i--;
if (strpos ($line, '#')) {
if (strpos ($line, '"') === false && strpos ($line, "'") === false)
$line = preg_replace('/\s+#(.+)$/','',$line);
}
$lineArray = $this->_parseLine($line); $lineArray = $this->_parseLine($line);
if ($literalBlockStyle) if ($literalBlockStyle)
...@@ -449,7 +542,7 @@ class Spyc { ...@@ -449,7 +542,7 @@ class Spyc {
private function loadFromSource ($input) { private function loadFromSource ($input) {
if (!empty($input) && strpos($input, "\n") === false && file_exists($input)) if (!empty($input) && strpos($input, "\n") === false && file_exists($input))
return file($input); $input = file_get_contents($input);
return $this->loadFromString($input); return $this->loadFromString($input);
} }
...@@ -471,8 +564,8 @@ class Spyc { ...@@ -471,8 +564,8 @@ class Spyc {
private function _parseLine($line) { private function _parseLine($line) {
if (!$line) return array(); if (!$line) return array();
$line = trim($line); $line = trim($line);
if (!$line) return array(); if (!$line) return array();
$array = array(); $array = array();
$group = $this->nodeContainsGroup($line); $group = $this->nodeContainsGroup($line);
...@@ -505,7 +598,7 @@ class Spyc { ...@@ -505,7 +598,7 @@ class Spyc {
* @return mixed * @return mixed
*/ */
private function _toType($value) { private function _toType($value) {
if ($value === '') return null; if ($value === '') return "";
$first_character = $value[0]; $first_character = $value[0];
$last_character = substr($value, -1, 1); $last_character = substr($value, -1, 1);
...@@ -517,10 +610,14 @@ class Spyc { ...@@ -517,10 +610,14 @@ class Spyc {
$is_quoted = true; $is_quoted = true;
} while (0); } while (0);
if ($is_quoted) if ($is_quoted) {
return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\'\'' => '\'', '\\\'' => '\'')); $value = str_replace('\n', "\n", $value);
if ($first_character == "'")
return strtr(substr ($value, 1, -1), array ('\'\'' => '\'', '\\\''=> '\''));
return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\\\''=> '\''));
}
if (strpos($value, ' #') !== false) if (strpos($value, ' #') !== false && !$is_quoted)
$value = preg_replace('/\s+#(.+)$/','',$value); $value = preg_replace('/\s+#(.+)$/','',$value);
if ($first_character == '[' && $last_character == ']') { if ($first_character == '[' && $last_character == ']') {
...@@ -568,26 +665,23 @@ class Spyc { ...@@ -568,26 +665,23 @@ class Spyc {
return null; return null;
} }
if (intval($first_character) > 0 && preg_match ('/^[1-9]+[0-9]*$/', $value)) { if ( is_numeric($value) && preg_match ('/^(-|)[1-9]+[0-9]*$/', $value) ){
$intvalue = (int)$value; $intvalue = (int)$value;
if ($intvalue != PHP_INT_MAX) if ($intvalue != PHP_INT_MAX && $intvalue != ~PHP_INT_MAX)
$value = $intvalue; $value = $intvalue;
return $value; return $value;
} }
if (in_array($value, if ( is_string($value) && preg_match('/^0[xX][0-9a-fA-F]+$/', $value)) {
array('true', 'on', '+', 'yes', 'y', 'True', 'TRUE', 'On', 'ON', 'YES', 'Yes', 'Y'))) { // Hexadecimal value.
return true; return hexdec($value);
} }
if (in_array(strtolower($value), $this->coerceValue($value);
array('false', 'off', '-', 'no', 'n'))) {
return false;
}
if (is_numeric($value)) { if (is_numeric($value)) {
if ($value === '0') return 0; if ($value === '0') return 0;
if (trim ($value, 0) === $value) if (rtrim ($value, 0) === $value)
$value = (float)$value; $value = (float)$value;
return $value; return $value;
} }
...@@ -609,6 +703,15 @@ class Spyc { ...@@ -609,6 +703,15 @@ class Spyc {
$seqs = array(); $seqs = array();
$maps = array(); $maps = array();
$saved_strings = array(); $saved_strings = array();
$saved_empties = array();
// Check for empty strings
$regex = '/("")|(\'\')/';
if (preg_match_all($regex,$inline,$strings)) {
$saved_empties = $strings[0];
$inline = preg_replace($regex,'YAMLEmpty',$inline);
}
unset($regex);
// Check for strings // Check for strings
$regex = '/(?:(")|(?:\'))((?(1)[^"]+|[^\']+))(?(1)"|\')/'; $regex = '/(?:(")|(?:\'))((?(1)[^"]+|[^\']+))(?(1)"|\')/';
...@@ -618,6 +721,8 @@ class Spyc { ...@@ -618,6 +721,8 @@ class Spyc {
} }
unset($regex); unset($regex);
// echo $inline;
$i = 0; $i = 0;
do { do {
...@@ -637,7 +742,8 @@ class Spyc { ...@@ -637,7 +742,8 @@ class Spyc {
} while (strpos ($inline, '[') !== false || strpos ($inline, '{') !== false); } while (strpos ($inline, '[') !== false || strpos ($inline, '{') !== false);
$explode = explode(', ',$inline); $explode = explode(',',$inline);
$explode = array_map('trim', $explode);
$stringi = 0; $i = 0; $stringi = 0; $i = 0;
while (1) { while (1) {
...@@ -679,6 +785,17 @@ class Spyc { ...@@ -679,6 +785,17 @@ class Spyc {
} }
} }
// Re-add the empties
if (!empty($saved_empties)) {
foreach ($explode as $key => $value) {
while (strpos($value,'YAMLEmpty') !== false) {
$explode[$key] = preg_replace('/YAMLEmpty/', '', $value, 1);
$value = $explode[$key];
}
}
}
$finished = true; $finished = true;
foreach ($explode as $key => $value) { foreach ($explode as $key => $value) {
if (strpos($value,'YAMLSeq') !== false) { if (strpos($value,'YAMLSeq') !== false) {
...@@ -690,6 +807,9 @@ class Spyc { ...@@ -690,6 +807,9 @@ class Spyc {
if (strpos($value,'YAMLString') !== false) { if (strpos($value,'YAMLString') !== false) {
$finished = false; break; $finished = false; break;
} }
if (strpos($value,'YAMLEmpty') !== false) {
$finished = false; break;
}
} }
if ($finished) break; if ($finished) break;
...@@ -698,6 +818,7 @@ class Spyc { ...@@ -698,6 +818,7 @@ class Spyc {
break; // Prevent infinite loops. break; // Prevent infinite loops.
} }
return $explode; return $explode;
} }
...@@ -772,6 +893,9 @@ class Spyc { ...@@ -772,6 +893,9 @@ class Spyc {
$_arr = array_merge ($_arr, $value); $_arr = array_merge ($_arr, $value);
} else if ($key || $key === '' || $key === '0') { } else if ($key || $key === '' || $key === '0') {
if (!is_array ($_arr))
$_arr = array ($key=>$value);
else
$_arr[$key] = $value; $_arr[$key] = $value;
} else { } else {
if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; } if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; }
...@@ -820,8 +944,11 @@ class Spyc { ...@@ -820,8 +944,11 @@ class Spyc {
return false; return false;
} }
private function addLiteralLine ($literalBlock, $line, $literalBlockStyle) { private function addLiteralLine ($literalBlock, $line, $literalBlockStyle, $indent = -1) {
$line = self::stripIndent($line, $indent);
if ($literalBlockStyle !== '|') {
$line = self::stripIndent($line); $line = self::stripIndent($line);
}
$line = rtrim ($line, "\r\n\t ") . "\n"; $line = rtrim ($line, "\r\n\t ") . "\n";
if ($literalBlockStyle == '|') { if ($literalBlockStyle == '|') {
return $literalBlock . $line; return $literalBlock . $line;
...@@ -889,8 +1016,8 @@ class Spyc { ...@@ -889,8 +1016,8 @@ class Spyc {
private function isArrayElement ($line) { private function isArrayElement ($line) {
if (!$line) return false; if (!$line || !is_scalar($line)) return false;
if ($line[0] != '-') return false; if (substr($line, 0, 2) != '- ') return false;
if (strlen ($line) > 3) if (strlen ($line) > 3)
if (substr($line,0,3) == '---') return false; if (substr($line,0,3) == '---') return false;
...@@ -917,7 +1044,7 @@ class Spyc { ...@@ -917,7 +1044,7 @@ class Spyc {
} }
private function startsMappedSequence ($line) { private function startsMappedSequence ($line) {
return ($line[0] == '-' && substr ($line, -1, 1) == ':'); return (substr($line, 0, 2) == '- ' && substr ($line, -1, 1) == ':');
} }
private function returnMappedSequence ($line) { private function returnMappedSequence ($line) {
...@@ -928,7 +1055,16 @@ class Spyc { ...@@ -928,7 +1055,16 @@ class Spyc {
return array($array); return array($array);
} }
private function checkKeysInValue($value) {
if (strchr('[{"\'', $value[0]) === false) {
if (strchr($value, ': ') !== false) {
throw new Exception('Too many keys: '.$value);
}
}
}
private function returnMappedValue ($line) { private function returnMappedValue ($line) {
$this->checkKeysInValue($line);
$array = array(); $array = array();
$key = self::unquote (trim(substr($line,0,-1))); $key = self::unquote (trim(substr($line,0,-1)));
$array[$key] = ''; $array[$key] = '';
...@@ -950,7 +1086,7 @@ class Spyc { ...@@ -950,7 +1086,7 @@ class Spyc {
private function returnKeyValuePair ($line) { private function returnKeyValuePair ($line) {
$array = array(); $array = array();
$key = ''; $key = '';
if (strpos ($line, ':')) { if (strpos ($line, ': ')) {
// It's a key/value pair most likely // It's a key/value pair most likely
// If the key is in double quotes pull it out // If the key is in double quotes pull it out
if (($line[0] == '"' || $line[0] == "'") && preg_match('/^(["\'](.*)["\'](\s)*:)/',$line,$matches)) { if (($line[0] == '"' || $line[0] == "'") && preg_match('/^(["\'](.*)["\'](\s)*:)/',$line,$matches)) {
...@@ -958,10 +1094,10 @@ class Spyc { ...@@ -958,10 +1094,10 @@ class Spyc {
$key = $matches[2]; $key = $matches[2];
} else { } else {
// Do some guesswork as to the key and the value // Do some guesswork as to the key and the value
$explode = explode(':',$line); $explode = explode(': ', $line);
$key = trim($explode[0]); $key = trim(array_shift($explode));
array_shift($explode); $value = trim(implode(': ', $explode));
$value = trim(implode(':',$explode)); $this->checkKeysInValue($value);
} }
// Set the type of the value. Int, string, etc // Set the type of the value. Int, string, etc
$value = $this->_toType($value); $value = $this->_toType($value);
...@@ -980,6 +1116,9 @@ class Spyc { ...@@ -980,6 +1116,9 @@ class Spyc {
$array = array(); $array = array();
$value = trim(substr($line,1)); $value = trim(substr($line,1));
$value = $this->_toType($value); $value = $this->_toType($value);
if ($this->isArrayElement($value)) {
$value = $this->returnArrayElement($value);
}
$array[] = $value; $array[] = $value;
return $array; return $array;
} }
...@@ -1007,18 +1146,16 @@ class Spyc { ...@@ -1007,18 +1146,16 @@ class Spyc {
$line = trim(str_replace($group, '', $line)); $line = trim(str_replace($group, '', $line));
return $line; return $line;
} }
}
} }
// Enable use of Spyc from command line // Enable use of Spyc from command line
// The syntax is the following: php spyc.php spyc.yaml // The syntax is the following: php Spyc.php spyc.yaml
define ('SPYC_FROM_COMMAND_LINE', false);
do { do {
if (!SPYC_FROM_COMMAND_LINE) break; if (PHP_SAPI != 'cli') break;
if (empty ($_SERVER['argc']) || $_SERVER['argc'] < 2) break; if (empty ($_SERVER['argc']) || $_SERVER['argc'] < 2) break;
if (empty ($_SERVER['PHP_SELF']) || $_SERVER['PHP_SELF'] != 'spyc.php') break; if (empty ($_SERVER['PHP_SELF']) || FALSE === strpos ($_SERVER['PHP_SELF'], 'Spyc.php') ) break;
$file = $argv[1]; $file = $argv[1];
printf ("Spyc loading file: %s\n", $file); echo json_encode (spyc_load_file ($file));
print_r (spyc_load_file ($file));
} while (0); } while (0);
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment