-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
255 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ composer.lock | |
docs | ||
vendor | ||
coverage | ||
.phpunit.result.cache |
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
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,8 @@ | ||
<?php | ||
|
||
return [ | ||
'enum' => 'The :attribute field is not a valid :enum.', | ||
'enum_index' => 'The :attribute field is not a valid index of :enum.', | ||
'enum_name' => 'The :attribute field is not a valid name of :enum.', | ||
'enum_value' => 'The :attribute field is not a valid value of :enum.', | ||
]; |
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,55 @@ | ||
<?php | ||
|
||
namespace Spatie\Enum\Laravel\Rules; | ||
|
||
use Exception; | ||
use Spatie\Enum\Enumerable; | ||
use InvalidArgumentException; | ||
use Illuminate\Support\Facades\Lang; | ||
use Illuminate\Contracts\Validation\Rule; | ||
|
||
class Enum implements Rule | ||
{ | ||
protected $rule = 'enum'; | ||
|
||
/** @var Enumerable */ | ||
protected $enum; | ||
|
||
/** @var string */ | ||
protected $attribute; | ||
|
||
/** @var mixed */ | ||
protected $value; | ||
|
||
public function __construct(string $enum) | ||
{ | ||
if (! isset(class_implements($enum)[Enumerable::class])) { | ||
throw new InvalidArgumentException("The given class {$enum} does not implement the Enumerable interface."); | ||
} | ||
|
||
$this->enum = $enum; | ||
} | ||
|
||
public function passes($attribute, $value): bool | ||
{ | ||
$this->attribute = $attribute; | ||
$this->value = $value; | ||
|
||
try { | ||
$this->enum::make($value); | ||
|
||
return true; | ||
} catch (Exception $ex) { | ||
return false; | ||
} | ||
} | ||
|
||
public function message(): string | ||
{ | ||
return Lang::trans('enum::validation.'.$this->rule, [ | ||
'attribute' => $this->attribute, | ||
'value' => $this->value, | ||
'enum' => $this->enum, | ||
]); | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace Spatie\Enum\Laravel\Rules; | ||
|
||
class EnumIndex extends Enum | ||
{ | ||
protected $rule = 'enum_index'; | ||
|
||
public function passes($attribute, $value): bool | ||
{ | ||
$this->attribute = $attribute; | ||
$this->value = $value; | ||
|
||
return $this->enum::isValidIndex($value); | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace Spatie\Enum\Laravel\Rules; | ||
|
||
class EnumName extends Enum | ||
{ | ||
protected $rule = 'enum_name'; | ||
|
||
public function passes($attribute, $value): bool | ||
{ | ||
$this->attribute = $attribute; | ||
$this->value = $value; | ||
|
||
return $this->enum::isValidName($value); | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace Spatie\Enum\Laravel\Rules; | ||
|
||
class EnumValue extends Enum | ||
{ | ||
protected $rule = 'enum_value'; | ||
|
||
public function passes($attribute, $value): bool | ||
{ | ||
$this->attribute = $attribute; | ||
$this->value = $value; | ||
|
||
return $this->enum::isValidValue($value); | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace Spatie\Enum\Laravel\Tests\Rules; | ||
|
||
use Spatie\Enum\Laravel\Tests\TestCase; | ||
use Spatie\Enum\Laravel\Rules\EnumIndex; | ||
use Spatie\Enum\Laravel\Tests\Extra\StatusEnum; | ||
|
||
final class EnumIndexTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_will_validate_an_index() | ||
{ | ||
$rule = new EnumIndex(StatusEnum::class); | ||
|
||
$this->assertTrue($rule->passes('attribute', 1)); | ||
$this->assertFalse($rule->passes('attribute', 5)); | ||
} | ||
|
||
/** @test */ | ||
public function it_will_fail_with_a_name() | ||
{ | ||
$rule = new EnumIndex(StatusEnum::class); | ||
|
||
$this->assertFalse($rule->passes('attribute', 'draft')); | ||
} | ||
|
||
/** @test */ | ||
public function it_will_fail_with_a_value() | ||
{ | ||
$rule = new EnumIndex(StatusEnum::class); | ||
|
||
$this->assertFalse($rule->passes('attribute', 'stored archive')); | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace Spatie\Enum\Laravel\Tests\Rules; | ||
|
||
use Spatie\Enum\Laravel\Tests\TestCase; | ||
use Spatie\Enum\Laravel\Rules\EnumIndex; | ||
use Spatie\Enum\Laravel\Tests\Extra\StatusEnum; | ||
|
||
final class EnumNameTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_will_validate_a_name() | ||
{ | ||
$rule = new EnumIndex(StatusEnum::class); | ||
|
||
$this->assertTrue($rule->passes('attribute', 'draft')); | ||
$this->assertFalse($rule->passes('attribute', 'drafted')); | ||
} | ||
|
||
/** @test */ | ||
public function it_will_fail_with_an_index() | ||
{ | ||
$rule = new EnumIndex(StatusEnum::class); | ||
|
||
$this->assertFalse($rule->passes('attribute', 1)); | ||
} | ||
|
||
/** @test */ | ||
public function it_will_fail_with_a_value() | ||
{ | ||
$rule = new EnumIndex(StatusEnum::class); | ||
|
||
$this->assertFalse($rule->passes('attribute', 'stored archive')); | ||
} | ||
} |
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,37 @@ | ||
<?php | ||
|
||
namespace Spatie\Enum\Laravel\Tests\Rules; | ||
|
||
use Spatie\Enum\Laravel\Rules\Enum; | ||
use Spatie\Enum\Laravel\Tests\TestCase; | ||
use Spatie\Enum\Laravel\Tests\Extra\StatusEnum; | ||
|
||
final class EnumTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_will_validate_an_index() | ||
{ | ||
$rule = new Enum(StatusEnum::class); | ||
|
||
$this->assertTrue($rule->passes('attribute', 1)); | ||
$this->assertFalse($rule->passes('attribute', 5)); | ||
} | ||
|
||
/** @test */ | ||
public function it_will_validate_a_name() | ||
{ | ||
$rule = new Enum(StatusEnum::class); | ||
|
||
$this->assertTrue($rule->passes('attribute', 'draft')); | ||
$this->assertFalse($rule->passes('attribute', 'drafted')); | ||
} | ||
|
||
/** @test */ | ||
public function it_will_validate_a_value() | ||
{ | ||
$rule = new Enum(StatusEnum::class); | ||
|
||
$this->assertTrue($rule->passes('attribute', 'stored archive')); | ||
$this->assertFalse($rule->passes('attribute', 'stored draft')); | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace Spatie\Enum\Laravel\Tests\Rules; | ||
|
||
use Spatie\Enum\Laravel\Tests\TestCase; | ||
use Spatie\Enum\Laravel\Rules\EnumIndex; | ||
use Spatie\Enum\Laravel\Tests\Extra\StatusEnum; | ||
|
||
final class EnumValueTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_will_validate_a_value() | ||
{ | ||
$rule = new EnumIndex(StatusEnum::class); | ||
|
||
$this->assertTrue($rule->passes('attribute', 'stored archive')); | ||
$this->assertFalse($rule->passes('attribute', 'stored draft')); | ||
} | ||
|
||
/** @test */ | ||
public function it_will_fail_with_an_index() | ||
{ | ||
$rule = new EnumIndex(StatusEnum::class); | ||
|
||
$this->assertFalse($rule->passes('attribute', 1)); | ||
} | ||
|
||
/** @test */ | ||
public function it_will_fail_with_a_name() | ||
{ | ||
$rule = new EnumIndex(StatusEnum::class); | ||
|
||
$this->assertFalse($rule->passes('attribute', 'draft')); | ||
} | ||
} |