Skip to content

Commit

Permalink
[4.x] Fix error when saving entry where content is empty array (#8813)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
mauricewijnia and jasonvarga authored Oct 9, 2023
1 parent af63932 commit afb41c3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Yaml/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function parse($str = null)
*/
public function dump($data, $content = null)
{
if ($content) {
if (! is_null($content)) {
if (is_string($content)) {
return $this->dumpFrontMatter($data, $content);
}
Expand All @@ -103,7 +103,7 @@ public function dump($data, $content = null)
*/
public function dumpFrontMatter($data, $content = null)
{
if ($content && ! is_string($content)) {
if (! is_null($content) && ! is_string($content)) {
$data['content'] = $content;
$content = '';
}
Expand Down
26 changes: 26 additions & 0 deletions tests/Yaml/YamlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ public function it_dumps_without_front_matter_when_content_is_an_array()
$this->assertEquals($expected, YAML::dump(['foo' => 'bar'], ['baz' => 'qux']));
}

/** @test */
public function it_dumps_without_front_matter_when_content_is_an_empty_array()
{
$expected = <<<'EOT'
foo: bar
content: { }

EOT;

$this->assertEquals($expected, YAML::dump(['foo' => 'bar'], []));
}

/** @test */
public function it_dumps_without_front_matter_when_content_is_null()
{
Expand Down Expand Up @@ -112,6 +124,20 @@ public function it_explicitly_dumps_front_matter_including_content_when_its_an_a
$this->assertEquals($expected, YAML::dumpFrontMatter(['foo' => 'bar'], ['baz' => 'qux']));
}

/** @test */
public function it_explicitly_dumps_front_matter_including_content_when_its_an_empty_array()
{
$expected = <<<'EOT'
---
foo: bar
content: { }
---

EOT;

$this->assertEquals($expected, YAML::dumpFrontMatter(['foo' => 'bar'], []));
}

/** @test */
public function it_explicitly_dumps_front_matter_without_content_when_its_null()
{
Expand Down

0 comments on commit afb41c3

Please sign in to comment.