-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit a9a466b
Showing
4 changed files
with
147 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,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. |
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,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": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=8.1", | ||
"symfony/validator": "^6.1" | ||
} | ||
} |
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 Muffe\EnumConstraint\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"PROPERTY", "METHOD", "ANNOTATION"}) | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
class Enum extends Constraint | ||
{ | ||
final public const NO_SUCH_CASE_ERROR = '8e6b7234-171a-4389-a4fc-9324586a6869'; | ||
|
||
public function __construct( | ||
public string $enumType, | ||
public bool $multiple = false, | ||
public ?string $message = null, | ||
mixed $options = null, | ||
array $groups = null, | ||
mixed $payload = null | ||
) { | ||
parent::__construct($options, $groups, $payload); | ||
} | ||
} |
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,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Muffe\EnumConstraint\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
class EnumValidator extends ConstraintValidator | ||
{ | ||
public function validate($value, Constraint $constraint): void | ||
{ | ||
if (!$constraint instanceof Enum) { | ||
throw new UnexpectedTypeException($constraint, Enum::class); | ||
} | ||
|
||
$enumType = $constraint->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(); | ||
} | ||
} |