Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
HasAuthor | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
constructAuthor | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
findAuthor | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createAuthor | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Hyde\Framework\Concerns; |
4 | |
5 | use Hyde\Framework\Helpers\Author as AuthorHelper; |
6 | use Hyde\Framework\Models\Author; |
7 | |
8 | /** |
9 | * Handle logic for Page models that have an Author. |
10 | * |
11 | * @see \Hyde\Framework\Models\Author |
12 | * @see \Tests\Unit\HasAuthorTest |
13 | */ |
14 | trait HasAuthor |
15 | { |
16 | public Author $author; |
17 | |
18 | public function constructAuthor(): void |
19 | { |
20 | if (isset($this->matter['author'])) { |
21 | if (is_string($this->matter['author'])) { |
22 | // If the author is a string, we assume it's a username |
23 | // and we'll try to find the author in the config |
24 | $this->author = $this->findAuthor($this->matter['author']); |
25 | } |
26 | if (is_array($this->matter['author'])) { |
27 | // If the author is an array, we'll assume it's a user |
28 | // with one-off custom data, so we create a new author. |
29 | // In the future we may want to merge config data with custom data |
30 | $this->author = $this->createAuthor($this->matter['author']); |
31 | } |
32 | } |
33 | } |
34 | |
35 | protected function findAuthor(string $author): Author |
36 | { |
37 | return AuthorHelper::get($author); |
38 | } |
39 | |
40 | protected function createAuthor(array $data): Author |
41 | { |
42 | $username = $data['username'] ?? $data['name'] ?? 'Guest'; |
43 | |
44 | return new Author($username, $data); |
45 | } |
46 | } |