Skip to content

Commit

Permalink
Adding new Family constraint to validator
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentChalnot committed Aug 4, 2020
1 parent 3ad6358 commit 5ee498d
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
59 changes: 59 additions & 0 deletions Validator/Constraints/Family.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/*
* This file is part of the Sidus/EAVModelBundle package.
*
* Copyright (c) 2015-2019 Vincent Chalnot
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sidus\EAVModelBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
* Check if an object is of the proper family
*
* @author Vincent Chalnot <[email protected]>
*
* @Annotation
*/
class Family extends Constraint
{
const INVALID_EAV_FAMILY_ERROR = 'e67e56ca-ccca-11ea-87d0-0242ac130003';

protected static $errorNames = [
self::INVALID_EAV_FAMILY_ERROR => 'INVALID_EAV_FAMILY_ERROR',
];

/** @var string */
public $message = 'This value should be an EAV data of family {{ family }}.';

/** @var string */
public $family;

/**
* {@inheritdoc}
*/
public function getDefaultOption()
{
return 'family';
}

/**
* {@inheritdoc}
*/
public function getRequiredOptions()
{
return ['family'];
}

/**
* @return array
*/
public function getTargets()
{
return [static::CLASS_CONSTRAINT, static::PROPERTY_CONSTRAINT];
}
}
54 changes: 54 additions & 0 deletions Validator/Constraints/FamilyValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/*
* This file is part of the Sidus/EAVModelBundle package.
*
* Copyright (c) 2015-2019 Vincent Chalnot
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sidus\EAVModelBundle\Validator\Constraints;

use Exception;
use Sidus\EAVModelBundle\Entity\DataInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* Check if an object is of the proper family
*
* @author Vincent Chalnot <[email protected]>
*/
class FamilyValidator extends ConstraintValidator
{
/**
* Checks if the passed value is valid.
*
* @param DataInterface|null $value The value that should be validated
* @param Constraint $constraint The constraint for the validation
*
* @throws Exception
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Family) {
throw new UnexpectedTypeException($constraint, Family::class);
}

if (null === $value) {
return;
}

if ($value instanceof DataInterface && $value->getFamilyCode() === $constraint->family) {
return;
}

$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setParameter('{{ family }}', $constraint->family)
->setCode(Family::INVALID_EAV_FAMILY_ERROR)
->addViolation();
}
}

0 comments on commit 5ee498d

Please sign in to comment.