Skip to content

Commit

Permalink
Merge pull request #1312 from hydephp/clean-up-page-data-factories
Browse files Browse the repository at this point in the history
Clean up page data factories
  • Loading branch information
caendesilva committed Mar 16, 2023
2 parents 4b1ce1e + 0a2eb6c commit 274e82c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
3 changes: 2 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ This serves two purposes:

### Changed
- Updated discovery exception message to include the causing exception message in https://github.com/hydephp/develop/pull/1305
- Cleaned up PageDataFactory, NavigationDataFactory, and BlogPostDataFactory internals for better type safety in https://github.com/hydephp/develop/pull/1312

### Deprecated
- for soon-to-be removed features.

### Removed
- for now removed features.
- Classes PageDataFactory, NavigationDataFactory, and BlogPostDataFactory no longer use the InteractsWithFrontMatter trait

### Fixed
- Fixed https://github.com/hydephp/develop/issues/1301 in https://github.com/hydephp/develop/pull/1302
Expand Down
22 changes: 12 additions & 10 deletions packages/framework/src/Framework/Factories/BlogPostDataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Hyde\Framework\Factories;

use Hyde\Framework\Concerns\InteractsWithFrontMatter;
use Hyde\Framework\Factories\Concerns\CoreDataObject;
use Hyde\Framework\Features\Blogging\Models\FeaturedImage;
use Hyde\Framework\Features\Blogging\Models\PostAuthor;
Expand All @@ -26,8 +25,6 @@
*/
class BlogPostDataFactory extends Concerns\PageDataFactory implements BlogPostSchema
{
use InteractsWithFrontMatter;

/**
* The front matter properties supported by this factory.
*
Expand Down Expand Up @@ -72,35 +69,35 @@ public function toArray(): array

protected function makeDescription(): string
{
return $this->matter('description') ?? $this->getTruncatedMarkdown($this->markdown->body());
return $this->getMatter('description') ?? $this->getTruncatedMarkdown($this->markdown->body());
}

protected function makeCategory(): ?string
{
return $this->matter('category');
return $this->getMatter('category');
}

protected function makeDate(): ?DateString
{
if ($this->matter('date')) {
return new DateString($this->matter('date'));
if ($this->getMatter('date')) {
return new DateString($this->getMatter('date'));
}

return null;
}

protected function makeAuthor(): ?PostAuthor
{
if ($this->matter('author')) {
return PostAuthor::getOrCreate($this->matter('author'));
if ($this->getMatter('author')) {
return PostAuthor::getOrCreate($this->getMatter('author'));
}

return null;
}

protected function makeImage(): ?FeaturedImage
{
if ($this->matter('image')) {
if ($this->getMatter('image')) {
return FeaturedImageFactory::make($this->matter);
}

Expand All @@ -115,4 +112,9 @@ private function getTruncatedMarkdown(string $markdown): string

return $markdown;
}

protected function getMatter(string $key): string|null|array
{
return $this->matter->get($key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Hyde\Markdown\Models\Markdown;
use Hyde\Markdown\Models\FrontMatter;
use Hyde\Markdown\Contracts\FrontMatter\PageSchema;
use Hyde\Framework\Concerns\InteractsWithFrontMatter;
use Hyde\Framework\Factories\Concerns\CoreDataObject;
use Hyde\Framework\Features\Navigation\NavigationData;

Expand All @@ -22,8 +21,6 @@

class HydePageDataFactory extends Concerns\PageDataFactory implements PageSchema
{
use InteractsWithFrontMatter;

/**
* The front matter properties supported by this factory.
*/
Expand Down Expand Up @@ -74,7 +71,7 @@ protected function makeNavigation(): NavigationData

private function findTitleForPage(): string
{
return $this->matter('title')
return $this->getMatter('title')
?? $this->findTitleFromMarkdownHeadings()
?? $this->findTitleFromParentIdentifier()
?? Hyde::makeTitle(basename($this->identifier));
Expand All @@ -101,4 +98,9 @@ private function findTitleFromParentIdentifier(): ?string

return null;
}

protected function getMatter(string $key): string|null
{
return $this->matter->get($key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Hyde\Pages\MarkdownPost;
use Hyde\Pages\DocumentationPage;
use Hyde\Markdown\Models\FrontMatter;
use Hyde\Framework\Concerns\InteractsWithFrontMatter;
use Hyde\Framework\Factories\Concerns\CoreDataObject;
use Hyde\Markdown\Contracts\FrontMatter\SubSchemas\NavigationSchema;

Expand All @@ -23,8 +22,6 @@
*/
class NavigationDataFactory extends Concerns\PageDataFactory implements NavigationSchema
{
use InteractsWithFrontMatter;

/**
* The front matter properties supported by this factory.
*
Expand Down Expand Up @@ -76,7 +73,7 @@ protected function makeLabel(): ?string
{
return $this->searchForLabelInFrontMatter()
?? $this->searchForLabelInConfig()
?? $this->matter('title')
?? $this->getMatter('title')
?? $this->title;
}

Expand Down Expand Up @@ -106,26 +103,26 @@ protected function makePriority(): int

private function searchForLabelInFrontMatter(): ?string
{
return $this->matter('navigation.label')
?? $this->matter('navigation.title');
return $this->getMatter('navigation.label')
?? $this->getMatter('navigation.title');
}

private function searchForGroupInFrontMatter(): ?string
{
return $this->matter('navigation.group')
?? $this->matter('navigation.category');
return $this->getMatter('navigation.group')
?? $this->getMatter('navigation.category');
}

private function searchForHiddenInFrontMatter(): ?bool
{
return $this->matter('navigation.hidden')
?? $this->invert($this->matter('navigation.visible'));
return $this->getMatter('navigation.hidden')
?? $this->invert($this->getMatter('navigation.visible'));
}

private function searchForPriorityInFrontMatter(): ?int
{
return $this->matter('navigation.priority')
?? $this->matter('navigation.order');
return $this->getMatter('navigation.priority')
?? $this->getMatter('navigation.order');
}

private function searchForLabelInConfig(): ?string
Expand Down Expand Up @@ -206,4 +203,9 @@ protected function offset(?int $value, int $offset): ?int
{
return $value === null ? null : $value + $offset;
}

protected function getMatter(string $key): string|null|int|bool
{
return $this->matter->get($key);
}
}

0 comments on commit 274e82c

Please sign in to comment.