Skip to content

Commit

Permalink
Merge pull request #396 from hydephp/improve-array-to-front-matter-ac…
Browse files Browse the repository at this point in the history
…tion

Improve ConvertsArrayToFrontMatter action hydephp/develop@f703f5e
  • Loading branch information
github-actions committed Aug 10, 2022
1 parent 5715ee6 commit 41d8f83
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
26 changes: 6 additions & 20 deletions src/Actions/ConvertsArrayToFrontMatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Hyde\Framework\Actions;

use Symfony\Component\Yaml\Yaml;

/**
* Convert an array into YAML Front Matter.
*
* Currently, does not support nested arrays.
* @see \Hyde\Framework\Testing\Feature\ConvertsArrayToFrontMatterTest
*/
class ConvertsArrayToFrontMatter
{
Expand All @@ -17,26 +19,10 @@ class ConvertsArrayToFrontMatter
*/
public function execute(array $array): string
{
// Initialize the array
$yaml = [];

// Set the first line to the opening starting block
$yaml[] = '---';

// For each line, add the key-value pair as YAML
foreach ($array as $key => $value) {
if (trim($value) !== '' && $value !== null) {
$yaml[] = "$key: $value";
}
if (empty($array)) {
return '';
}

// Set the closing block
$yaml[] = '---';

// Add an extra line
$yaml[] = '';

// Return the array imploded into a string with newline characters
return implode("\n", $yaml);
return "---\n".Yaml::dump($array)."---\n";
}
}
4 changes: 2 additions & 2 deletions tests/Feature/Commands/MakePostCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function test_command_has_expected_output_and_creates_valid_file()

$this->assertFileExists($this->getPath());
$this->assertStringContainsString(
'title: Test Post',
"title: 'Test Post'",
file_get_contents($this->getPath())
);
}
Expand Down Expand Up @@ -90,7 +90,7 @@ public function test_that_files_are_overwritten_when_force_flag_is_set()
file_get_contents($this->getPath())
);
$this->assertStringContainsString(
'title: Test Post',
"title: 'Test Post'",
file_get_contents($this->getPath())
);
}
Expand Down
44 changes: 44 additions & 0 deletions tests/Feature/ConvertsArrayToFrontMatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Hyde\Framework\Testing\Feature;

use Hyde\Framework\Actions\ConvertsArrayToFrontMatter;
use Hyde\Testing\TestCase;

/**
* @covers \Hyde\Framework\Actions\ConvertsArrayToFrontMatter
*/
class ConvertsArrayToFrontMatterTest extends TestCase
{
public function test_action_converts_an_array_to_front_matter()
{
$array = [
'key' => 'value',
'string' => 'quoted string',
'boolean' => true,
'integer' => 100,
'array' => ['key' => 'value'],
'list' => ['foo', 'bar'],
];
$expected = <<<'YAML'
---
key: value
string: 'quoted string'
boolean: true
integer: 100
array:
key: value
list:
- foo
- bar
---

YAML;
$this->assertEquals(str_replace("\r", '', $expected), (new ConvertsArrayToFrontMatter)->execute($array));
}

public function test_action_returns_empty_string_if_array_is_empty()
{
$this->assertEquals('', (new ConvertsArrayToFrontMatter)->execute([]));
}
}

0 comments on commit 41d8f83

Please sign in to comment.