Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| PostAuthor | |
100.00% |
13 / 13 |
|
100.00% |
7 / 7 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getOrCreate | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| all | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| findUsername | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Hyde\Framework\Features\Blogging\Models; |
| 6 | |
| 7 | use Stringable; |
| 8 | use Hyde\Facades\Author; |
| 9 | use Hyde\Facades\Config; |
| 10 | use Illuminate\Support\Collection; |
| 11 | use JetBrains\PhpStorm\Deprecated; |
| 12 | |
| 13 | use function strtolower; |
| 14 | use function is_string; |
| 15 | |
| 16 | /** |
| 17 | * Object representation of a post author for the site. |
| 18 | */ |
| 19 | class PostAuthor implements Stringable |
| 20 | { |
| 21 | /** |
| 22 | * The username of the author. |
| 23 | * This is the key used to find authors in the config. |
| 24 | */ |
| 25 | public readonly string $username; |
| 26 | |
| 27 | /** |
| 28 | * The display name of the author. |
| 29 | */ |
| 30 | public readonly string $name; |
| 31 | |
| 32 | /** |
| 33 | * The author's website URL. |
| 34 | * |
| 35 | * Could for example, be a Twitter page, website, or a hyperlink to more posts by the author. |
| 36 | * Should be a fully qualified link, meaning it starts with http:// or https://. |
| 37 | */ |
| 38 | public readonly ?string $website; |
| 39 | |
| 40 | /** |
| 41 | * Construct a new Post Author object. |
| 42 | * |
| 43 | * If your input is in the form of an array, you may rather want to use the `getOrCreate` method. |
| 44 | * |
| 45 | * @param string $username |
| 46 | * @param string|null $name |
| 47 | * @param string|null $website |
| 48 | */ |
| 49 | public function __construct(string $username, ?string $name = null, ?string $website = null) |
| 50 | { |
| 51 | $this->username = $username; |
| 52 | $this->name = $name ?? $this->username; |
| 53 | $this->website = $website; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Dynamically get or create an author based on a username string or front matter array. |
| 58 | * |
| 59 | * @param string|array{username?: string, name?: string, website?: string} $data |
| 60 | */ |
| 61 | public static function getOrCreate(string|array $data): static |
| 62 | { |
| 63 | if (is_string($data)) { |
| 64 | return static::get($data); |
| 65 | } |
| 66 | |
| 67 | return Author::create(static::findUsername($data), $data['name'] ?? null, $data['website'] ?? null); |
| 68 | } |
| 69 | |
| 70 | /** Get an Author from the config, or create it. */ |
| 71 | public static function get(string $username): static |
| 72 | { |
| 73 | return static::all()->firstWhere('username', strtolower($username)) ?? Author::create($username); |
| 74 | } |
| 75 | |
| 76 | /** @return \Illuminate\Support\Collection<string, \Hyde\Framework\Features\Blogging\Models\PostAuthor> */ |
| 77 | public static function all(): Collection |
| 78 | { |
| 79 | return (new Collection(Config::getArray('hyde.authors', [])))->mapWithKeys(function (self $author): array { |
| 80 | return [strtolower($author->username) => $author]; |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | public function __toString(): string |
| 85 | { |
| 86 | return $this->name; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @deprecated This is not needed as the name property can be accessed directly. |
| 91 | */ |
| 92 | #[Deprecated(reason: 'Use the name property instead.', replacement: '%class%->name')] |
| 93 | public function getName(): string |
| 94 | { |
| 95 | return $this->name; |
| 96 | } |
| 97 | |
| 98 | /** @param array{username?: string, name?: string, website?: string} $data */ |
| 99 | protected static function findUsername(array $data): string |
| 100 | { |
| 101 | return $data['username'] ?? $data['name'] ?? 'Guest'; |
| 102 | } |
| 103 | } |