Skip to content

Commit c9886d3

Browse files
author
github-actions
committed
Merge pull request #462 from hydephp/437-refactor-author-model-to-use-same-format-for-create-method-as-constructor
Clean up author model hydephp/develop@e040a51
1 parent 76fb16b commit c9886d3

File tree

2 files changed

+23
-35
lines changed

2 files changed

+23
-35
lines changed

src/Models/Author.php

+22-32
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
use Illuminate\Support\Collection;
66

77
/**
8-
* The Post Author Object Model.
8+
* The Post Author model object.
99
*
10-
* @todo #437 Refactor to use same format for create method as constructor
11-
* @phpstan-consistent-constructor
10+
* @see \Hyde\Framework\Testing\Feature\AuthorTest
1211
*/
1312
class Author implements \Stringable
1413
{
@@ -46,61 +45,52 @@ class Author implements \Stringable
4645
* @param string $username
4746
* @param array|null $data
4847
*/
49-
public function __construct(string $username, ?array $data = [])
48+
final public function __construct(string $username, ?array $data = [])
5049
{
5150
$this->username = $username;
51+
5252
if (isset($data['name'])) {
5353
$this->name = $data['name'];
5454
}
55+
5556
if (isset($data['website'])) {
5657
$this->website = $data['website'];
5758
}
5859
}
5960

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
6163
{
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);
6369
}
6470

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
7372
{
74-
return $this->name ?? $this->username;
73+
return static::all()->firstWhere('username', $username) ?? static::create($username);
7574
}
7675

77-
public static function create(string $username, ?string $name = null, ?string $website = null): static
76+
public static function all(): Collection
7877
{
79-
return new static($username, [
80-
'name' => $name,
81-
'website'=> $website,
82-
]);
78+
return new Collection(config('authors', []));
8379
}
8480

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
8782
{
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]);
9384
}
9485

95-
public static function all(): Collection
86+
public function __toString(): string
9687
{
97-
return new Collection(config('authors', []));
88+
return $this->getName();
9889
}
9990

100-
public static function get(string $username): static
91+
public function getName(): string
10192
{
102-
return static::all()->firstWhere('username', $username)
103-
?? static::create($username);
93+
return $this->name ?? $this->username;
10494
}
10595

10696
protected static function findUsername(array $data): string

tests/Feature/AuthorHelperTest.php tests/Feature/AuthorTest.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
use Illuminate\Support\Facades\Config;
99

1010
/**
11-
* Class AuthorHelperTest.
12-
*
1311
* @covers \Hyde\Framework\Models\Author
1412
*/
15-
class AuthorHelperTest extends TestCase
13+
class AuthorTest extends TestCase
1614
{
1715
public function test_create_method_creates_new_author_model()
1816
{

0 commit comments

Comments
 (0)