Skip to content

Commit

Permalink
Merge pull request #675 from hydephp/prepare-post-author-changes
Browse files Browse the repository at this point in the history
Update the post author class to prepare for the v2 overhaul
  • Loading branch information
caendesilva authored Dec 21, 2024
2 parents a4dca7b + 3946c11 commit 80d23b7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/Framework/Features/Blogging/Models/PostAuthor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Hyde\Facades\Author;
use Hyde\Facades\Config;
use Illuminate\Support\Collection;
use JetBrains\PhpStorm\Deprecated;

use function strtolower;
use function is_string;
Expand All @@ -26,7 +27,7 @@ class PostAuthor implements Stringable
/**
* The display name of the author.
*/
public readonly ?string $name;
public readonly string $name;

/**
* The author's website URL.
Expand All @@ -48,7 +49,7 @@ class PostAuthor implements Stringable
public function __construct(string $username, ?string $name = null, ?string $website = null)
{
$this->username = $username;
$this->name = $name;
$this->name = $name ?? $this->username;
$this->website = $website;
}

Expand Down Expand Up @@ -82,12 +83,16 @@ public static function all(): Collection

public function __toString(): string
{
return $this->getName();
return $this->name;
}

/**
* @deprecated This is not needed as the name property can be accessed directly.
*/
#[Deprecated(reason: 'Use the name property instead.', replacement: '%class%->name')]
public function getName(): string
{
return $this->name ?? $this->username;
return $this->name;
}

/** @param array{username?: string, name?: string, website?: string} $data */
Expand Down
2 changes: 1 addition & 1 deletion src/Framework/Features/XmlGenerators/RssFeedGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected function addDynamicItemData(SimpleXMLElement $item, MarkdownPost $post
}

if (isset($post->author)) {
$item->addChild('dc:creator', $post->author->getName(), 'http://purl.org/dc/elements/1.1/');
$item->addChild('dc:creator', $post->author->name, 'http://purl.org/dc/elements/1.1/');
}

if (isset($post->category)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/MarkdownPostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testConstructorCanCreateANewAuthorInstanceFromUsernameString()

$this->assertInstanceOf(PostAuthor::class, $post->author);
$this->assertSame('John Doe', $post->author->username);
$this->assertNull($post->author->name);
$this->assertSame('John Doe', $post->author->name);
$this->assertNull($post->author->website);
}

Expand Down
7 changes: 7 additions & 0 deletions tests/Unit/PostAuthorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ public function testGetNameHelperReturnsUsernameIfNameIsNotSet()
$this->assertEquals('username', $author->getName());
}

public function testNameIsSetToUsernameIfNameIsNotSet()
{
$author = new PostAuthor('username');

$this->assertEquals('username', $author->name);
}

public function testToStringHelperReturnsTheName()
{
$author = new PostAuthor('username', 'John Doe');
Expand Down

0 comments on commit 80d23b7

Please sign in to comment.