diff --git a/Changelog.md b/Changelog.md index e5bacf3..b2f40ae 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,6 +2,11 @@ ## 3.x Series +## Unreleased +##### 2023-XX-YY + +- Added the `link_type_exists()` helper function (to be used in blade templates) + ## 3.7.0 ##### 2023-04-04 diff --git a/Support/helpers.php b/Support/helpers.php index b096455..cbbaba7 100644 --- a/Support/helpers.php +++ b/Support/helpers.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use Vanilo\Links\Models\LinkTypeProxy; use Vanilo\Links\Query\Get; if (!function_exists('links')) { @@ -21,3 +22,11 @@ function link_groups(string $type, string $property = null): Get return null !== $property ? $result->basedOn($property) : $result; } } + +if (!function_exists('link_type_exists')) { + function link_type_exists(string $type): bool + { + return LinkTypeProxy::whereSlug($type)->exists(); + } +} + diff --git a/Tests/Unit/LinkTypeTest.php b/Tests/Unit/LinkTypeTest.php index 97f22b6..a0f64c0 100644 --- a/Tests/Unit/LinkTypeTest.php +++ b/Tests/Unit/LinkTypeTest.php @@ -181,4 +181,15 @@ public function the_choices_method_can_return_slugs_as_keys() '2019' => '2019', ], LinkType::choices(false, true)); } + + /** @test */ + public function the_link_type_exists_helper_function_can_tell_whether_a_certain_type_exists() + { + LinkType::create(['name' => 'Popular Companion']); + LinkType::create(['name' => 'Accessories']); + + $this->assertTrue(link_type_exists('popular-companion')); + $this->assertTrue(link_type_exists('accessories')); + $this->assertFalse(link_type_exists('heavy-metal')); + } }