-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from rgdevment/master
feat: add constraint for native enum
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Linio\Component\Input\Constraint; | ||
|
||
class NativeEnum extends Constraint | ||
{ | ||
protected $enumClass; | ||
|
||
public function __construct($enumClass) | ||
{ | ||
$this->enumClass = $enumClass; | ||
|
||
$this->setErrorMessage( | ||
$errorMessage ?? 'Invalid option for a native PHP enum. Allowed options are: ' . json_encode($this->enumClass::cases()) | ||
); | ||
} | ||
|
||
public function validate($content): bool | ||
{ | ||
if (!is_scalar($content)) { | ||
return false; | ||
} | ||
|
||
return !($this->enumClass::tryFrom($content) === null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Linio\Component\Input\Constraint; | ||
|
||
use Linio\Component\Input\TestCase; | ||
|
||
class NativeEnumTest extends TestCase | ||
{ | ||
public function testIsCheckingInvalidData(): void | ||
{ | ||
$constraint = new NativeEnum(FakeEnum::class); | ||
$this->assertFalse($constraint->validate('test')); | ||
$this->assertFalse($constraint->validate('blah')); | ||
|
||
$this->assertFalse($constraint->validate(['foo'])); | ||
$obj = new \stdClass(); | ||
$obj->var1 = 'foo'; | ||
$this->assertFalse($constraint->validate($obj)); | ||
} | ||
|
||
public function testIsCheckingValidData(): void | ||
{ | ||
$constraint = new NativeEnum(FakeEnum::class); | ||
$this->assertTrue($constraint->validate('FOO')); | ||
$this->assertTrue($constraint->validate('BAR')); | ||
} | ||
|
||
public function testIsGettingErrorMessage(): void | ||
{ | ||
$constraint = new NativeEnum(FakeEnum::class); | ||
$this->assertFalse($constraint->validate('test')); | ||
$this->assertEquals('[["FOO","BAR"]] Invalid option for a native PHP enum. Allowed options are: ["FOO","BAR"]', $constraint->getErrorMessage('["FOO","BAR"]')); | ||
} | ||
} | ||
|
||
enum FakeEnum: string | ||
{ | ||
case Foo = 'FOO'; | ||
case Bar = 'BAR'; | ||
} |