diff --git a/src/Models/HasAuthor.php b/src/Models/HasAuthor.php new file mode 100644 index 00000000..add18213 --- /dev/null +++ b/src/Models/HasAuthor.php @@ -0,0 +1,38 @@ +matter['author'])) { + if (is_string($this->matter['author'])) { + $this->author = $this->findAuthor($this->matter['author']); + } + if (is_array($this->matter['author'])) { + $this->author = $this->createAuthor($this->matter['author']); + } + } + } + + protected function findAuthor(string $author): Author + { + return AuthorService::find($author) ?: new Author($author); + } + + protected function createAuthor(array $data): Author + { + $username = $data['username'] ?? $data['name'] ?? 'Guest'; + return new Author($username, $data); + } +} \ No newline at end of file diff --git a/src/Models/MarkdownPost.php b/src/Models/MarkdownPost.php index 5ef3a630..566cfce8 100644 --- a/src/Models/MarkdownPost.php +++ b/src/Models/MarkdownPost.php @@ -6,6 +6,7 @@ class MarkdownPost extends MarkdownDocument { + use HasAuthor; use HasMetadata; use HasDateString; use HasFeaturedImage; @@ -17,6 +18,7 @@ public function __construct(array $matter, string $body, string $title = '', str { parent::__construct($matter, $body, $title, $slug); + $this->constructAuthor(); $this->constructMetadata(); $this->constructDateString(); $this->constructFeaturedImage();