-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding new Family constraint to validator
- Loading branch information
1 parent
3ad6358
commit 5ee498d
Showing
2 changed files
with
113 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,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]; | ||
} | ||
} |
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,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(); | ||
} | ||
} |