Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/Spyc.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@ public function dump($array,$indent = false,$wordwrap = false, $no_opening_dashe
if (!$no_opening_dashes) $string = "---\n";

// Start at the base of the array and move through it.
if ($array) {
// Note: We use explicit checks instead of `if ($array)` to properly handle:
// - Scalar zero values passed directly (e.g., dump(0) or dump("0"))
// - Arrays containing zero values (e.g., dump([0]))
// PHP treats 0 and "0" as falsey, so a simple `if ($array)` would skip processing.
if ($array !== null && $array !== '' && (!is_array($array) || count($array) > 0)) {
$array = (array)$array;
$previous_key = -1;
foreach ($array as $key => $value) {
Expand All @@ -234,7 +238,11 @@ public function dump($array,$indent = false,$wordwrap = false, $no_opening_dashe
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 (empty ($value))
// Note: We use count($value) === 0 instead of empty($value) for stylistic consistency
// with the fix in dump(). While functionally equivalent here (since $value is already
// verified to be an array), this makes the intent explicit and avoids any confusion
// with PHP's empty() behavior on zero values.
if (count($value) === 0)
return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key, $source_array);
// It has children. What to do?
// Make it the right kind of item
Expand Down
39 changes: 39 additions & 0 deletions tests/DumpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,43 @@ public function testPerCentAndDoubleQuote() {
$this->assertEquals ($awaiting, $dump);
}

/**
* Test that integer zero values are correctly dumped.
* This is a regression test for the issue where empty(0) returns true in PHP,
* causing zero values to be incorrectly treated as empty.
*/
public function testDumpIntegerZero() {
$dump = Spyc::YAMLDump(array(0));
$awaiting = "---\n- 0\n";
$this->assertEquals($awaiting, $dump);
}

/**
* Test that string zero values are correctly dumped.
* This is a regression test for the issue where empty("0") returns true in PHP.
*/
public function testDumpStringZero() {
$dump = Spyc::YAMLDump(array('0'));
$awaiting = "---\n- \"0\"\n";
$this->assertEquals($awaiting, $dump);
}

/**
* Test that associative arrays with zero values are correctly dumped.
*/
public function testDumpAssociativeZero() {
$dump = Spyc::YAMLDump(array('key' => 0));
$awaiting = "---\nkey: 0\n";
$this->assertEquals($awaiting, $dump);
}

/**
* Test that mixed arrays containing zero values are correctly dumped.
*/
public function testDumpMixedWithZero() {
$dump = Spyc::YAMLDump(array(1, 0, 2));
$awaiting = "---\n- 1\n- 0\n- 2\n";
$this->assertEquals($awaiting, $dump);
}

}