diff --git a/Spyc.php b/Spyc.php index 49e0cbb..3fe0f62 100644 --- a/Spyc.php +++ b/Spyc.php @@ -88,6 +88,12 @@ class Spyc { */ public $setting_use_syck_is_possible = false; + /** + * Setting this to true will forse YAMLLoad to use syck_load function when + * possible. False by default. + * @var bool + */ + public $setting_empty_hash_as_object = false; /**#@+ @@ -147,9 +153,15 @@ public function loadFile ($file) { * @access public * @return array * @param string $input Path of YAML file or string containing YAML + * @param array set options */ - public static function YAMLLoad($input) { + public static function YAMLLoad($input, $options = []) { $Spyc = new Spyc; + foreach ($options as $key => $value) { + if (property_exists($Spyc, $key)) { + $Spyc->$key = $value; + } + } return $Spyc->_load($input); } @@ -171,9 +183,15 @@ public static function YAMLLoad($input) { * @access public * @return array * @param string $input String containing YAML + * @param array set options */ - public static function YAMLLoadString($input) { + public static function YAMLLoadString($input, $options = []) { $Spyc = new Spyc; + foreach ($options as $key => $value) { + if (property_exists($Spyc, $key)) { + $Spyc->$key = $value; + } + } return $Spyc->_loadString($input); } @@ -574,19 +592,20 @@ private function _parseLine($line) { $line = $this->stripGroup ($line, $group); } - if ($this->startsMappedSequence($line)) + if ($this->startsMappedSequence($line)) { return $this->returnMappedSequence($line); + } - if ($this->startsMappedValue($line)) + if ($this->startsMappedValue($line)) { return $this->returnMappedValue($line); + } if ($this->isArrayElement($line)) - return $this->returnArrayElement($line); + return $this->returnArrayElement($line); if ($this->isPlainArray($line)) return $this->returnPlainArray($line); - return $this->returnKeyValuePair($line); } @@ -599,6 +618,11 @@ private function _parseLine($line) { */ private function _toType($value) { if ($value === '') return ""; + + if ($this->setting_empty_hash_as_object && $value === '{}') { + return new stdClass(); + } + $first_character = $value[0]; $last_character = substr($value, -1, 1); @@ -1101,6 +1125,7 @@ private function returnKeyValuePair ($line) { } // Set the type of the value. Int, string, etc $value = $this->_toType($value); + if ($key === '0') $key = '__!YAMLZero'; $array[$key] = $value; } else { diff --git a/tests/ParseTest.php b/tests/ParseTest.php index e480c36..2ce77ca 100644 --- a/tests/ParseTest.php +++ b/tests/ParseTest.php @@ -396,4 +396,18 @@ public function testMultipleArrays() { $expected = array(array(array('x'))); $this->assertSame($expected, Spyc::YAMLLoad("- - - x")); } + + public function testElementWithEmptyHash() + { + $element = "hash: {}\narray: []"; + $yaml = Spyc::YAMLLoadString($element); + $this->assertEquals($yaml['hash'], []); + $this->assertEquals($yaml['array'], []); + + $yaml = Spyc::YAMLLoadString($element, [ + 'setting_empty_hash_as_object' => true + ]); + $this->assertInstanceOf(stdClass::class, $yaml['hash']); + $this->assertEquals($yaml['array'], []); + } }