Skip to content

Commit

Permalink
Merge pull request #77 from ezimuel/feature/empty-hash-as-object
Browse files Browse the repository at this point in the history
Added feature to convert {} into empty object using stdClass()
  • Loading branch information
jackmcdade committed Sep 10, 2019
2 parents 576c42e + b066393 commit 4627c83
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
37 changes: 31 additions & 6 deletions Spyc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**#@+
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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);

}
Expand All @@ -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);

Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions tests/ParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'], []);
}
}

0 comments on commit 4627c83

Please sign in to comment.