From 22ee66244e2d9b5b826933d5d1a832ba231d2903 Mon Sep 17 00:00:00 2001 From: dvondrak Date: Thu, 3 Aug 2017 19:07:27 +0200 Subject: [PATCH] Added NotReferenced validator. (#2) * Added NotReferenced validator. --- Resources/config.xml | 5 +++ Validator/NotReferenced.php | 44 +++++++++++++++++++++ Validator/NotReferencedValidator.php | 58 ++++++++++++++++++++++++++++ Validator/UniqueValidator.php | 1 - 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 Validator/NotReferenced.php create mode 100644 Validator/NotReferencedValidator.php diff --git a/Resources/config.xml b/Resources/config.xml index 88975f1..b389e55 100644 --- a/Resources/config.xml +++ b/Resources/config.xml @@ -41,5 +41,10 @@ + + + + + diff --git a/Validator/NotReferenced.php b/Validator/NotReferenced.php new file mode 100644 index 0000000..a9bf646 --- /dev/null +++ b/Validator/NotReferenced.php @@ -0,0 +1,44 @@ + 'IS_REFERENCED_ERROR', + ]; + + /** @var string */ + public $field; + + /** @var string */ + public $relatedEntity; + + /** @var string|null */ + public $relatedField = null; + + /** @var string */ + public $message = 'The entity is referenced by another entity.'; + + public function getRequiredOptions(): array + { + return [ 'field', 'relatedEntity' ]; + } + + public function getTargets(): string + { + return self::CLASS_CONSTRAINT; + } + + public function validatedBy(): string + { + return 'vanio_domain.validator.not_referenced'; + } +} diff --git a/Validator/NotReferencedValidator.php b/Validator/NotReferencedValidator.php new file mode 100644 index 0000000..67d8191 --- /dev/null +++ b/Validator/NotReferencedValidator.php @@ -0,0 +1,58 @@ +registry = $registry; + } + + /** + * @param mixed $object + * @param Constraint $constraint + */ + public function validate($object, Constraint $constraint) + { + if (!$constraint instanceof NotReferenced) { + throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\Unique'); + } + + $em = $this->registry->getManagerForClass($constraint->relatedEntity); + if (!$em) { + throw new ConstraintDefinitionException(sprintf('Unable to find the object manager associated with an entity of class "%s".', $constraint->relatedEntity)); + } + $repository = $em->getRepository($constraint->relatedEntity); + + $accessor = PropertyAccess::createPropertyAccessor(); + $relatedField = $constraint->relatedField ? $constraint->relatedField : $constraint->field; + $relatedValue = $accessor->getValue($object, $constraint->field); + + if (is_callable([ $repository, 'existsBy' ])) { + $result = $repository->existsBy([ + $relatedField => $relatedValue, + ]); + } + else { + $result = $repository->findBy([ + $relatedField => $relatedValue, + ]); + } + + if ($result) { + $this->context->buildViolation($constraint->message) + ->setCode(NotReferenced::IS_REFERENCED_ERROR) + ->addViolation(); + } + } +} diff --git a/Validator/UniqueValidator.php b/Validator/UniqueValidator.php index cf9ec90..94723f7 100644 --- a/Validator/UniqueValidator.php +++ b/Validator/UniqueValidator.php @@ -55,7 +55,6 @@ public function validate($object, Constraint $constraint) return; } - $id = $constraint->id; if (!is_null($id) && count($result) === 1 && $em->getReference($constraint->class, $accessor->getValue($object, $id)) === current($result)) {