-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #320 from hydephp/create-front-matter-model
Create front matter model hydephp/develop@039f878
- Loading branch information
github-actions
committed
Aug 2, 2022
1 parent
6301ae8
commit 498a394
Showing
3 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |