Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
muffe committed Aug 5, 2022
0 parents commit a9a466b
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
22 changes: 22 additions & 0 deletions LICENSE
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.
23 changes: 23 additions & 0 deletions composer.json
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"
}
}
28 changes: 28 additions & 0 deletions src/Constraints/Enum.php
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);
}
}
74 changes: 74 additions & 0 deletions src/Constraints/EnumValidator.php
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();
}
}

0 comments on commit a9a466b

Please sign in to comment.