Skip to content

Commit

Permalink
Merge pull request #339 from hydephp/add-magic-method-to-markdownpages
Browse files Browse the repository at this point in the history
Add magic __get method to Markdown pages to get data from front matter hydephp/develop@314946a
  • Loading branch information
github-actions committed Aug 4, 2022
1 parent b79827f commit e345381
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Contracts/AbstractMarkdownPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public function __construct(string $identifier = '', ?FrontMatter $matter = null
$this->markdown = $markdown ?? new Markdown();
}

/** @interitDoc */
public function __get(string $name)
{
return $this->matter->get($name);
}

/** @inheritDoc */
public function matter(string $key = null, mixed $default = null): mixed
{
Expand Down
8 changes: 8 additions & 0 deletions src/Contracts/MarkdownPageContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ public static function make(string $identifier = '', array $matter = [], string
* @param \Hyde\Framework\Models\Markdown|null $markdown
*/
public function __construct(string $identifier = '', ?FrontMatter $matter = null, ?Markdown $markdown = null);

/**
* Get the value of the specified key from the front matter.
*
* @param string $name
* @return mixed
*/
public function __get(string $name);
}
19 changes: 19 additions & 0 deletions tests/Feature/AbstractPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,23 @@ public function test_body_helper_returns_markdown_document_body_in_markdown_page
$page = new MarkdownPage(markdown: new Markdown(body: '# Foo'));
$this->assertEquals('# Foo', $page->markdown->body());
}

public function test_markdown_page_magic_get_method_returns_front_matter_properties()
{
$page = MarkdownPage::make(matter: ['foo' => 'bar']);
$this->assertEquals('bar', $page->foo);
}

public function test_markdown_page_magic_get_method_gives_precedence_to_actual_class_properties()
{
$page = MarkdownPage::make(matter: ['foo' => 'bar']);
$page->foo = 'baz';
$this->assertEquals('baz', $page->foo);
}

public function test_markdown_page_magic_get_method_returns_null_if_property_not_found()
{
$page = MarkdownPage::make();
$this->assertNull($page->foo);
}
}

0 comments on commit e345381

Please sign in to comment.