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

update to add exists function #6

Merged
merged 2 commits into from
Mar 31, 2022
Merged
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
5 changes: 5 additions & 0 deletions src/HasEnumeration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public static function toSelect(): Collection
]);
}

public static function exists($value): bool
{
return (bool) optional(static::tryFrom($value))->value;
}

public function line(): string
{
return Lang::get(
Expand Down
13 changes: 13 additions & 0 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DavidIanBonner\Enumerated\Stubs\Language;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Lang;
use SebastianBergmann\Environment\Console;

class EnumTest extends TestCase
{
Expand Down Expand Up @@ -86,4 +87,16 @@ function it_can_prefix_the_lang_line()
'nintendo switch' => 'nintendo switch',
], Consoles::toSelect()->toArray());
}

/** @test */
function it_will_return_true_if_enum_is_exists()
{
$this->assertTrue(Consoles::exists('playstation 4'));
}

/** @test */
function it_will_return_false_if_enum_value_doesnot_exist()
{
$this->assertFalse(Consoles::exists('playstation 360'));
}
}