|
5 | 5 | use Illuminate\Support\Collection;
|
6 | 6 |
|
7 | 7 | /**
|
8 |
| - * The Post Author Object Model. |
| 8 | + * The Post Author model object. |
9 | 9 | *
|
10 |
| - * @todo #437 Refactor to use same format for create method as constructor |
11 |
| - * @phpstan-consistent-constructor |
| 10 | + * @see \Hyde\Framework\Testing\Feature\AuthorTest |
12 | 11 | */
|
13 | 12 | class Author implements \Stringable
|
14 | 13 | {
|
@@ -46,61 +45,52 @@ class Author implements \Stringable
|
46 | 45 | * @param string $username
|
47 | 46 | * @param array|null $data
|
48 | 47 | */
|
49 |
| - public function __construct(string $username, ?array $data = []) |
| 48 | + final public function __construct(string $username, ?array $data = []) |
50 | 49 | {
|
51 | 50 | $this->username = $username;
|
| 51 | + |
52 | 52 | if (isset($data['name'])) {
|
53 | 53 | $this->name = $data['name'];
|
54 | 54 | }
|
| 55 | + |
55 | 56 | if (isset($data['website'])) {
|
56 | 57 | $this->website = $data['website'];
|
57 | 58 | }
|
58 | 59 | }
|
59 | 60 |
|
60 |
| - public function __toString(): string |
| 61 | + /** Dynamically get or create an author based on a username string or front matter array */ |
| 62 | + final public static function make(string|array $data): static |
61 | 63 | {
|
62 |
| - return $this->getName(); |
| 64 | + if (is_string($data)) { |
| 65 | + return static::get($data); |
| 66 | + } |
| 67 | + |
| 68 | + return static::create(static::findUsername($data), $data['name'] ?? null, $data['website'] ?? null); |
63 | 69 | }
|
64 | 70 |
|
65 |
| - /** |
66 |
| - * Get the author's preferred name. |
67 |
| - * |
68 |
| - * @see \Hyde\Framework\Testing\Unit\AuthorGetNameTest |
69 |
| - * |
70 |
| - * @return string |
71 |
| - */ |
72 |
| - public function getName(): string |
| 71 | + public static function get(string $username): static |
73 | 72 | {
|
74 |
| - return $this->name ?? $this->username; |
| 73 | + return static::all()->firstWhere('username', $username) ?? static::create($username); |
75 | 74 | }
|
76 | 75 |
|
77 |
| - public static function create(string $username, ?string $name = null, ?string $website = null): static |
| 76 | + public static function all(): Collection |
78 | 77 | {
|
79 |
| - return new static($username, [ |
80 |
| - 'name' => $name, |
81 |
| - 'website'=> $website, |
82 |
| - ]); |
| 78 | + return new Collection(config('authors', [])); |
83 | 79 | }
|
84 | 80 |
|
85 |
| - /** Dynamically get or create an author based on string or front matter array */ |
86 |
| - public static function make(string|array $data): static |
| 81 | + public static function create(string $username, ?string $name = null, ?string $website = null): static |
87 | 82 | {
|
88 |
| - if (is_string($data)) { |
89 |
| - return static::get($data); |
90 |
| - } |
91 |
| - |
92 |
| - return static::create(static::findUsername($data), $data['name'] ?? null, $data['website'] ?? null); |
| 83 | + return new static($username, ['name' => $name, 'website' => $website]); |
93 | 84 | }
|
94 | 85 |
|
95 |
| - public static function all(): Collection |
| 86 | + public function __toString(): string |
96 | 87 | {
|
97 |
| - return new Collection(config('authors', [])); |
| 88 | + return $this->getName(); |
98 | 89 | }
|
99 | 90 |
|
100 |
| - public static function get(string $username): static |
| 91 | + public function getName(): string |
101 | 92 | {
|
102 |
| - return static::all()->firstWhere('username', $username) |
103 |
| - ?? static::create($username); |
| 93 | + return $this->name ?? $this->username; |
104 | 94 | }
|
105 | 95 |
|
106 | 96 | protected static function findUsername(array $data): string
|
|
0 commit comments