Skip to content

Commit

Permalink
Fixes hashes with numeric names (issue 19)
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad.andersen committed Sep 20, 2009
1 parent 754c248 commit 5a09bf6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
18 changes: 10 additions & 8 deletions spyc.php
Expand Up @@ -732,14 +732,17 @@ private function addArrayInline ($array, $indent) {

private function addArray ($incoming_data, $incoming_indent) {

// print_r ($incoming_data);

if (count ($incoming_data) > 1)
return $this->addArrayInline ($incoming_data, $incoming_indent);

$key = key ($incoming_data);
$value = isset($incoming_data[$key]) ? $incoming_data[$key] : null;
if ($key === '__!YAMLZero') $key = '0';

if ($incoming_indent == 0 && !$this->_containsGroupAlias && !$this->_containsGroupAnchor) { // Shortcut for root-level values.
if ($key || $key === '') {
if ($key || $key === '' || $key === '0') {
$this->result[$key] = $value;
} else {
$this->result[] = $value; end ($this->result); $key = key ($this->result);
Expand Down Expand Up @@ -768,7 +771,7 @@ private function addArray ($incoming_data, $incoming_indent) {
if (!is_array ($_arr)) { $_arr = array (); }

$_arr = array_merge ($_arr, $value);
} else if ($key || $key === '') {
} else if ($key || $key === '' || $key === '0') {
$_arr[$key] = $value;
} else {
if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; }
Expand Down Expand Up @@ -938,6 +941,7 @@ private function returnPlainArray ($line) {

private function returnKeyValuePair ($line) {
$array = array();
$key = '';
if (strpos ($line, ':')) {
// It's a key/value pair most likely
// If the key is in double quotes pull it out
Expand All @@ -953,13 +957,11 @@ private function returnKeyValuePair ($line) {
}
// Set the type of the value. Int, string, etc
$value = $this->_toType($value);
if (empty($key)) {
$array[] = $value;
} else {
$array[$key] = $value;
}
if ($key === '0') $key = '__!YAMLZero';
$array[$key] = $value;
} else {
$array = array ($line);
}

return $array;

}
Expand Down
5 changes: 5 additions & 0 deletions spyc.yaml
Expand Up @@ -178,6 +178,11 @@ many_lines: |
dog
werte:
1: nummer 1
0: Stunde 0

# [Endloop]
endloop: |
Does this line in the end indeed make Spyc go to an infinite loop?
5 changes: 5 additions & 0 deletions tests/ParseTest.php
Expand Up @@ -266,6 +266,11 @@ public function testManyNewlines() {
dog', $this->yaml['many_lines']);
}

public function testWerte() {
$this->assertSame (array ('1' => 'nummer 1', '0' => 'Stunde 0'), $this->yaml['werte']);
}


public function testSpecialCharacters() {
$this->assertSame ('[{]]{{]]', $this->yaml['special_characters']);
}
Expand Down

0 comments on commit 5a09bf6

Please sign in to comment.