Skip to content

Commit

Permalink
Merge pull request #320 from hydephp/create-front-matter-model
Browse files Browse the repository at this point in the history
Create front matter model hydephp/develop@039f878
  • Loading branch information
github-actions committed Aug 2, 2022
1 parent 6301ae8 commit 498a394
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Actions/ConvertsArrayToFrontMatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

/**
* Convert an array into YAML Front Matter.
*
* Currently does not support nested arrays.
*/
class ConvertsArrayToFrontMatter
{
Expand Down
49 changes: 49 additions & 0 deletions src/Models/FrontMatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Hyde\Framework\Models;

use Hyde\Framework\Actions\ConvertsArrayToFrontMatter;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;

/**
* @see \Hyde\Framework\Testing\Unit\FrontMatterModelTest
*/
class FrontMatter implements Arrayable
{
public array $matter;

public function __construct(array $matter = [])
{
$this->matter = $matter;
}

public function __toString(): string
{
return (new ConvertsArrayToFrontMatter())->execute($this->matter);
}

public function __get(string $key): mixed
{
return $this->get($key);
}

public function get(string $key = null, mixed $default = null): mixed
{
if ($key) {
return Arr::get($this->matter, $key, $default);
}

return $this->matter;
}

public function toArray(): array
{
return $this->matter;
}

public static function fromArray(array $matter): static
{
return new static($matter);
}
}
91 changes: 91 additions & 0 deletions tests/Unit/FrontMatterModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Hyde\Framework\Testing\Unit;

use Hyde\Framework\Models\FrontMatter;
use Hyde\Testing\TestCase;

/**
* @covers \Hyde\Framework\Models\FrontMatter
*/
class FrontMatterModelTest extends TestCase
{
public function test_constructor_creates_new_front_matter_model()
{
$matter = new FrontMatter([]);
$this->assertInstanceOf(FrontMatter::class, $matter);
}

public function test_constructor_arguments_are_optional()
{
$matter = new FrontMatter();
$this->assertInstanceOf(FrontMatter::class, $matter);
}

public function test_constructor_arguments_are_assigned()
{
$matter = new FrontMatter(['foo' => 'bar']);
$this->assertEquals(['foo' => 'bar'], $matter->matter);
}

public function test_static_from_array_method_creates_new_front_matter_model()
{
$matter = FrontMatter::fromArray(['foo' => 'bar']);
$this->assertInstanceOf(FrontMatter::class, $matter);
$this->assertEquals(['foo' => 'bar'], $matter->matter);
}

public function test_to_string_magic_method_converts_model_array_into_yaml_front_matter()
{
$matter = new FrontMatter(['foo' => 'bar']);
$this->assertEquals("---\nfoo: bar\n---\n", (string) $matter);
}

public function test_magic_get_method_returns_front_matter_property()
{
$matter = new FrontMatter(['foo' => 'bar']);
$this->assertEquals('bar', $matter->foo);
}

public function test_magic_get_method_returns_null_if_property_does_not_exist()
{
$matter = new FrontMatter();
$this->assertNull($matter->foo);
}

public function test_get_method_returns_empty_array_if_document_has_no_matter()
{
$matter = new FrontMatter();
$this->assertEquals([], $matter->get());
}

public function test_get_method_returns_document_front_matter_array_if_no_arguments_are_specified()
{
$matter = new FrontMatter(['foo' => 'bar']);
$this->assertEquals(['foo' => 'bar'], $matter->get());
}

public function test_get_method_returns_null_if_specified_front_matter_key_does_not_exist()
{
$matter = new FrontMatter();
$this->assertNull($matter->get('bar'));
}

public function test_get_method_returns_specified_default_value_if_property_does_not_exist()
{
$matter = new FrontMatter();
$this->assertEquals('default', $matter->get('bar', 'default'));
}

public function test_get_method_returns_specified_front_matter_value_if_key_is_specified()
{
$matter = new FrontMatter(['foo' => 'bar']);
$this->assertEquals('bar', $matter->get('foo'));
}

public function test_to_array_returns_front_matter_array()
{
$matter = new FrontMatter(['foo' => 'bar']);
$this->assertEquals(['foo' => 'bar'], $matter->toArray());
}
}

0 comments on commit 498a394

Please sign in to comment.