-
-
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.
Add trait to handle Authors in the data layer
- Loading branch information
1 parent
d463d61
commit 62f3793
Showing
2 changed files
with
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Hyde\Framework\Models; | ||
|
||
use Hyde\Framework\Services\AuthorService; | ||
|
||
/** | ||
* Trait HasAuthor | ||
* | ||
* @see \Tests\Unit\HasAuthorTest | ||
*/ | ||
trait HasAuthor | ||
{ | ||
public Author $author; | ||
|
||
public function constructAuthor(): void | ||
{ | ||
if (isset($this->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); | ||
} | ||
} |
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