Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Fix error when saving entry where content is empty array #8813

Merged
merged 5 commits into from
Oct 9, 2023
Merged
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
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
Loading