diff --git a/src/Generator/MarkdownGenerator.php b/src/Generator/MarkdownGenerator.php index c73b8da..e2e9a3e 100644 --- a/src/Generator/MarkdownGenerator.php +++ b/src/Generator/MarkdownGenerator.php @@ -156,7 +156,7 @@ private function getSeeAlso(UrlList $urls): string private function getLinkLines(UrlList $urls): array { return array_map(function (Url $url) { - return "- [$url]($url)"; + return "- [{$url->getName()}]({$url->getUrl()})"; }, $urls->toArray()); } diff --git a/src/Value/Url.php b/src/Value/Url.php index 92ed716..ae101c3 100644 --- a/src/Value/Url.php +++ b/src/Value/Url.php @@ -8,22 +8,33 @@ class Url { private string $url; + private string $name; - public function __construct(string $url) + public function __construct(string $value) { + $parts = explode(' ', $value); + $url = array_shift($parts); + $name = implode(' ', $parts); + Assert::that($url) ->url(); $this->url = $url; + $this->name = $name !== '' ? $name : $this->url; } - public function __toString() + public function getUrl(): string { - return $this->getUrl(); + return $this->url; } - public function getUrl(): string + public function getName(): string { - return $this->url; + return $this->name; + } + + public function toString(): string + { + return $this->url . ($this->name !== $this->url ? ' ' . $this->name : ''); } } diff --git a/src/Value/UrlList.php b/src/Value/UrlList.php index c852847..5f898fe 100644 --- a/src/Value/UrlList.php +++ b/src/Value/UrlList.php @@ -19,7 +19,7 @@ class UrlList public function __construct(array $urls) { $strings = array_map(function (Url $url): string { - return (string)$url; + return $url->toString(); }, $urls); $strings = array_values(array_unique($strings)); diff --git a/tests/Generator/JekyllPageGeneratorTest.php b/tests/Generator/JekyllPageGeneratorTest.php index eae2d38..4741cb1 100644 --- a/tests/Generator/JekyllPageGeneratorTest.php +++ b/tests/Generator/JekyllPageGeneratorTest.php @@ -9,7 +9,7 @@ use PHPUnit\Framework\TestCase; /** @covers \App\Generator\JekyllPage */ -class JekyllPageTest extends TestCase +class JekyllPageGeneratorTest extends TestCase { private JekyllPageGenerator $generator; diff --git a/tests/Value/UrlTest.php b/tests/Value/UrlTest.php index 758b3e8..e32b424 100644 --- a/tests/Value/UrlTest.php +++ b/tests/Value/UrlTest.php @@ -10,8 +10,6 @@ /** @covers \App\Value\Url */ class UrlTest extends TestCase { - const URL = 'http://link.com'; - /** @test */ public function constructor_WithInvalidUrlString_ThrowException() { @@ -20,11 +18,32 @@ public function constructor_WithInvalidUrlString_ThrowException() } /** @test */ - public function getUrl() + public function getName_WithTextAfterUrl_ReturnTextName() + { + $url = new Url('https://link.com Name with spaces'); + self::assertEquals( + 'Name with spaces', + $url->getName() + ); + } + + /** @test */ + public function getUrl_WithUrlOnly_ReturnsUrl() + { + $url = new Url('https://link.com'); + self::assertEquals( + 'https://link.com', + $url->getUrl() + ); + } + + /** @test */ + public function getName_WithUrlOnly_ReturnsUrl() { + $url = new Url('https://link.com'); self::assertEquals( - self::URL, - (string)(new Url(self::URL)) + 'https://link.com', + $url->getName() ); } }