Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix @link matcher #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Generator/MarkdownGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
21 changes: 16 additions & 5 deletions src/Value/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 : '');
}
}
2 changes: 1 addition & 1 deletion src/Value/UrlList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion tests/Generator/JekyllPageGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use PHPUnit\Framework\TestCase;

/** @covers \App\Generator\JekyllPage */
class JekyllPageTest extends TestCase
class JekyllPageGeneratorTest extends TestCase
{
private JekyllPageGenerator $generator;

Expand Down
29 changes: 24 additions & 5 deletions tests/Value/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
/** @covers \App\Value\Url */
class UrlTest extends TestCase
{
const URL = 'http://link.com';

/** @test */
public function constructor_WithInvalidUrlString_ThrowException()
{
Expand All @@ -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()
);
}
}