From a9a466bc384bcda7a9d6b6f3e880cbfc2ba32213 Mon Sep 17 00:00:00 2001 From: Marc Wustrack Date: Fri, 5 Aug 2022 13:16:17 +0200 Subject: [PATCH] Initial commit --- LICENSE | 22 +++++++++ composer.json | 23 ++++++++++ src/Constraints/Enum.php | 28 ++++++++++++ src/Constraints/EnumValidator.php | 74 +++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 LICENSE create mode 100644 composer.json create mode 100644 src/Constraints/Enum.php create mode 100644 src/Constraints/EnumValidator.php diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..147c8dd --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ + +The MIT license + +Copyright (c) 2015-present Kévin Dunglas + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..03a3dd6 --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "muffe/enum-constraint", + "description": "A Symfony Validator constraint that validates of given strings a valid cases in a given PHP 8 Enum", + "type": "library", + "keywords": ["enum", "validator", "constraint"], + "minimum-stability": "dev", + "license": "MIT", + "autoload": { + "psr-4": { + "Muffe\\EnumConstraint\\": "src/" + } + }, + "authors": [ + { + "name": "Marc Wustrack", + "email": "mail@wustrack.dev" + } + ], + "require": { + "php": ">=8.1", + "symfony/validator": "^6.1" + } +} diff --git a/src/Constraints/Enum.php b/src/Constraints/Enum.php new file mode 100644 index 0000000..1505cbc --- /dev/null +++ b/src/Constraints/Enum.php @@ -0,0 +1,28 @@ +enumType; + + if (!\is_a($enumType, \UnitEnum::class, true)) { + throw new \RuntimeException('The "enumType" option of the Enum constraint should be a Enum.'); + } + + if (null === $value) { + return; + } + + $property = 'name'; + if (\is_a($enumType, \BackedEnum::class, true)) { + $property = 'value'; + } + + $enumCases = \array_map(static fn ($item) => $item->{$property}, $enumType::cases()); + + if (!$constraint->message) { + $constraint->message = 'The enum case "{{ value }}" is not valid. Valid cases are: "{{ choices }}"'; + } + + if ($constraint->multiple) { + if (!\is_array($value)) { + $value = [$value]; + } + + foreach ($value as $singleValue) { + if (\in_array($singleValue, $enumCases, true)) { + continue; + } + + $this->context->buildViolation($constraint->message) + ->setParameter('{{ value }}', $this->formatValue($singleValue)) + ->setParameter('{{ choices }}', $this->formatValues($enumCases)) + ->setCode(Enum::NO_SUCH_CASE_ERROR) + ->setInvalidValue($value) + ->addViolation(); + + return; + } + + return; + } + + if (\in_array($value, $enumCases, true)) { + return; + } + + $this->context->buildViolation($constraint->message) + ->setParameter('{{ value }}', $this->formatValue($value)) + ->setParameter('{{ choices }}', $this->formatValues($enumCases)) + ->setCode(Enum::NO_SUCH_CASE_ERROR) + ->setInvalidValue($value) + ->addViolation(); + } +}