Skip to content

Commit

Permalink
Merge 0a94736 into bf3f884
Browse files Browse the repository at this point in the history
  • Loading branch information
Gummibeer committed Aug 5, 2019
2 parents bf3f884 + 0a94736 commit a2ad790
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ composer.lock
docs
vendor
coverage
.phpunit.result.cache
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -26,7 +26,7 @@
"php": "^7.2",
"illuminate/console": "^5.8",
"illuminate/support": "^5.8",
"spatie/enum": "^2.1"
"spatie/enum": "^2.3"
},
"require-dev": {
"orchestra/testbench": "^3.8",
Expand Down
8 changes: 8 additions & 0 deletions resources/lang/en/validation.php
@@ -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.',
];
55 changes: 55 additions & 0 deletions src/Rules/Enum.php
@@ -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,
]);
}
}
16 changes: 16 additions & 0 deletions src/Rules/EnumIndex.php
@@ -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);
}
}
16 changes: 16 additions & 0 deletions src/Rules/EnumName.php
@@ -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);
}
}
16 changes: 16 additions & 0 deletions src/Rules/EnumValue.php
@@ -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);
}
}
35 changes: 35 additions & 0 deletions tests/Rules/EnumIndexTest.php
@@ -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'));
}
}
35 changes: 35 additions & 0 deletions tests/Rules/EnumNameTest.php
@@ -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'));
}
}
37 changes: 37 additions & 0 deletions tests/Rules/EnumTest.php
@@ -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'));
}
}
35 changes: 35 additions & 0 deletions tests/Rules/EnumValueTest.php
@@ -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'));
}
}

0 comments on commit a2ad790

Please sign in to comment.