Skip to content

Commit

Permalink
fix: return null on invalid icon name exception instead of throwing (
Browse files Browse the repository at this point in the history
…#5)

* feat: return `null` on invalid icon name

* chore: update tests

* chore: update README
  • Loading branch information
stefangalescu authored Jul 9, 2023
1 parent 89e3049 commit 759f850
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Apart from the requirements above, it is required that you use Statamic's new Ru

First, require `statamic-heroicons` as a Composer dependency:

```
```shell
composer require stefangalescu/statamic-heroicons
```

Expand Down
20 changes: 12 additions & 8 deletions src/Tags/Heroicon.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@ class Heroicon extends Tags
{
protected static $handle = 'heroicon';

private function renderBladeToHtml(string $variant, string $icon, Collection $attrs): string
private function renderBladeToHtml(string $variant, string $icon, Collection $attrs): string|null
{
$attrsString = $attrs->map(function ($value, $key) {
$parsedValue = gettype($value) === 'string' ? $value : var_export($value, true);

return $key.'='.'"'.$parsedValue.'"';
})->join(' ');

return Blade::render('<x-heroicon-'.$variant[0].'-'.$icon.' '.$attrsString.' />');
try {
return Blade::render('<x-heroicon-'.$variant[0].'-'.$icon.' '.$attrsString.' />');
} catch (\Throwable $e) {
return null;
}
}

private function render(string $variant = null, string|null $icon = null): string
private function render(string $variant = null, string|null $icon = null): string|null
{
$variant = $variant ?? Str::lower($this->params->get('variant'));
$icon = $icon ?? Str::lower($this->params->get('icon'));
Expand All @@ -37,39 +41,39 @@ private function render(string $variant = null, string|null $icon = null): strin
/**
* The {{ heroicon }} tag.
*/
public function index(): string
public function index(): string|null
{
return $this->render();
}

/**
* The {{ heroicon:mini }} tag.
*/
public function mini(): string
public function mini(): string|null
{
return $this->render('mini');
}

/**
* The {{ heroicon:outline }} tag.
*/
public function outline(): string
public function outline(): string|null
{
return $this->render('outline');
}

/**
* The {{ heroicon:solid }} tag.
*/
public function solid(): string
public function solid(): string|null
{
return $this->render('solid');
}

/**
* The {{ heroicon:{variant}:{icon} }} tag.
*/
public function wildcard(string $tag): string
public function wildcard(string $tag): string|null
{
[$variant, $icon] = Str::of($tag)->split('/:/')->toArray();
$icon = Str::kebab($icon);
Expand Down
9 changes: 9 additions & 0 deletions tests/HeroiconTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace StefanGalescu\Heroicons\Tests;

use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertNull;
use function PHPUnit\Framework\assertStringContainsString;
use Statamic\Statamic;

Expand Down Expand Up @@ -75,4 +76,12 @@ public function can_add_dynamically_binded_attributes_to_svg()

assertStringContainsString('x-bind:class="true ? \'w-6 h-6\' : \'w-5 h-5\'"', $render);
}

/** @test */
public function will_not_throw_when_icon_name_is_invalid()
{
$render = $this->render('outline', 'invalid-icon-name');

assertNull($render);
}
}

0 comments on commit 759f850

Please sign in to comment.