Skip to content
This repository has been archived by the owner on Oct 10, 2018. It is now read-only.

Commit

Permalink
Merge pull request #14 from kontenta/emptyadminlink
Browse files Browse the repository at this point in the history
Don't print href if url is empty
  • Loading branch information
erik-epineer authored Sep 24, 2018
2 parents 47851f2 + 02976eb commit f354a9a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/AdminLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(string $url, string $name, string $description = nul
$this->description = $description;
}

public function getUrl(): string
public function getUrl(): ?string
{
return $this->url;
}
Expand All @@ -38,11 +38,16 @@ public function getDescription(): ?string

public function toHtml(): string
{
$attributes[] = 'href="' . htmlspecialchars($this->getUrl()) . '"';
$attributes = [];

if ($this->getUrl()) {
$attributes[] = 'href="' . htmlspecialchars($this->getUrl()) . '"';
}

if ($this->getDescription()) {
$attributes[] = 'title="' . htmlspecialchars($this->getDescription()) . '"';
}

return '<a '.implode(' ', $attributes).'>'.$this->getName().'</a>';
return '<a ' . implode(' ', $attributes) . '>' . $this->getName() . '</a>';
}
}
7 changes: 5 additions & 2 deletions src/RouteAdminLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Kontenta\KontourSupport;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;

class RouteAdminLink extends AdminLink
{
protected $routeName;
Expand All @@ -13,8 +16,8 @@ public function __construct(string $routeName, string $name, string $description
$this->description = $description;
}

public function getUrl(): string
public function getUrl(): ?string
{
return route($this->routeName);
return Route::has($this->routeName) ? URL::route($this->routeName) : null;
}
}
17 changes: 17 additions & 0 deletions tests/Feature/RouteAdminLinkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Kontenta\KontourSupport\Tests\Feature;

use Kontenta\KontourSupport\RouteAdminLink;
use Kontenta\KontourSupport\Tests\IntegrationTest;

class RouteAdminLinkTest extends IntegrationTest
{
public function test_url_is_null_when_route_does_not_exist()
{
$link = new RouteAdminLink('not.existing', 'Hej', '"Hejsanhejsan"');

$this->assertNull($link->getUrl());
$this->assertEquals('<a title="&quot;Hejsanhejsan&quot;">Hej</a>', $link->toHtml());
}
}

0 comments on commit f354a9a

Please sign in to comment.