From 7280504931880659ffd9dca84099a72dca32c812 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Wed, 17 Mar 2021 00:52:03 +0100 Subject: [PATCH] Validation rule EnumIsValidRule --- changelog.md | 19 +++++++++-- composer.json | 7 ++++ resources/lang/en.json | 4 +++ resources/lang/fr.json | 4 +++ src/BaseEnum.php | 4 +-- src/EnumServiceProvider.php | 22 +++++++++++++ src/Exceptions/UnknownEnumClass.php | 10 ++++++ src/Rules/BaseEnumRule.php | 42 ++++++++++++++++++++++++ src/Rules/EnumIsValidRule.php | 50 +++++++++++++++++++++++++++++ tests/EnumTest.php | 9 ++++++ 10 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 resources/lang/en.json create mode 100644 resources/lang/fr.json create mode 100644 src/EnumServiceProvider.php create mode 100644 src/Exceptions/UnknownEnumClass.php create mode 100644 src/Rules/BaseEnumRule.php create mode 100644 src/Rules/EnumIsValidRule.php diff --git a/changelog.md b/changelog.md index 5e7e924..366f4dc 100644 --- a/changelog.md +++ b/changelog.md @@ -9,6 +9,7 @@ All notable changes to `Enum` will be documented in this file. ## Version 0.0.2 ### - Add + - Enum cast in Models usage: @@ -23,6 +24,7 @@ All notable changes to `Enum` will be documented in this file. ## Version 0.1.0 ### - Changed + - toArray Method now returns an array of BaseEnums this is because php numeric string array indexes to int, this would break origin enum values @@ -30,9 +32,22 @@ All notable changes to `Enum` will be documented in this file. ## Version 0.1.1 ### Fixed - - fixed performances issue on toValues and toLabels + +- Fixed performances issue on toValues and toLabels ## Version 0.1.2 ### Changed - - changed toJson to output array with label and value + +- Changed toJson to output array with label and value + +## Version 0.1.3 + +### Added + +- Validation rule EnumIsValidRule, the constructor needs a BaseEnum class + eg: new EnumIsValidRule(ArcadiaMailType::class) + +### Fixed + +- Unit tests with 0.1.2 changes diff --git a/composer.json b/composer.json index d82678b..fe1cdb9 100644 --- a/composer.json +++ b/composer.json @@ -32,5 +32,12 @@ "psr-4": { "Kwaadpepper\\Enum\\Tests\\": "tests" } + }, + "extra": { + "laravel": { + "providers": [ + "Kwaadpepper\\Enum\\EnumServiceProvider" + ] + } } } diff --git a/resources/lang/en.json b/resources/lang/en.json new file mode 100644 index 0000000..52e5eab --- /dev/null +++ b/resources/lang/en.json @@ -0,0 +1,4 @@ +{ + "enum::enum.isNotValid": "The field :attribute is not valid, it has to be an integer or a string.", + "enum::enum.notInList": "The field :attribute has to be one of the followings: :values" +} diff --git a/resources/lang/fr.json b/resources/lang/fr.json new file mode 100644 index 0000000..8085205 --- /dev/null +++ b/resources/lang/fr.json @@ -0,0 +1,4 @@ +{ + "enum::enum.isNotValid": "Le champ :attribute n'est pas valide, il doit ĂȘtre un entier ou une chaine.", + "enum::enum.notInList": "Le champ :attribute doit ĂȘtre une des valeurs suivantes: :values" +} diff --git a/src/BaseEnum.php b/src/BaseEnum.php index 769e9d2..40a9902 100644 --- a/src/BaseEnum.php +++ b/src/BaseEnum.php @@ -268,8 +268,8 @@ abstract class BaseEnum implements JsonSerializable, UrlRoutable public function jsonSerialize() { return [ - 'value' => $this->value, - 'label' => $this->label + 'label' => $this->label, + 'value' => $this->value ]; } diff --git a/src/EnumServiceProvider.php b/src/EnumServiceProvider.php new file mode 100644 index 0000000..33602cd --- /dev/null +++ b/src/EnumServiceProvider.php @@ -0,0 +1,22 @@ +loadJsonTranslationsFrom(__DIR__ . '/../resources/lang', 'enum'); + + $this->publishes([ + __DIR__ . '/../resources/lang' => resource_path('lang/vendor/enum'), + ]); + } +} diff --git a/src/Exceptions/UnknownEnumClass.php b/src/Exceptions/UnknownEnumClass.php new file mode 100644 index 0000000..09ce7f9 --- /dev/null +++ b/src/Exceptions/UnknownEnumClass.php @@ -0,0 +1,10 @@ +enumClass = $enumClass; + } + + /** + * Determine if the validation rule passes. + * + * @param string $attribute + * @param mixed $value + * @return bool + */ + abstract public function passes($attribute, $value): bool; + /** + * Get the validation error message. + * + * @return string + */ + abstract public function message(): string; +} diff --git a/src/Rules/EnumIsValidRule.php b/src/Rules/EnumIsValidRule.php new file mode 100644 index 0000000..c4e6675 --- /dev/null +++ b/src/Rules/EnumIsValidRule.php @@ -0,0 +1,50 @@ +enumClass, 'make'], $value); + } catch (TypeError $e) { + $this->messageType = 1; + return false; + } catch (BadMethodCallException $e) { + $this->messageType = 2; + return false; + } + return true; + } + /** + * Get the validation error message. + * + * @return string + */ + public function message(): string + { + switch ($this->messageType) { + case 1: + return trans('enum::enum.isNotValid'); + case 2: + return trans('enum::enum.notInList', [ + 'values' => implode(',', forward_static_call([$this->enumClass, 'toValues'])) + ]); + } + } +} diff --git a/tests/EnumTest.php b/tests/EnumTest.php index 5ada54d..276db49 100644 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -2,6 +2,7 @@ namespace Kwaadpepper\Enum\Tests; +use Kwaadpepper\Enum\Rules\EnumIsValidRule; use Kwaadpepper\Enum\Tests\Enums\ContactFormCivility; use Kwaadpepper\Enum\Tests\Enums\Days; use Kwaadpepper\Enum\Tests\Enums\ForceStringsFromInteger; @@ -108,4 +109,12 @@ class EnumTest extends TestCase $response->assertOk(); $response->assertExactJson([ForceStringsFromInteger::valueB()]); } + + public function testRules() + { + $rule = new EnumIsValidRule(Days::class); + $this->assertTrue($rule->passes('day', Days::mon()->value)); + $this->assertFalse($rule->passes('day', 9999)); + $this->assertFalse($rule->passes('day', Days::mon())); + } }