Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added NotReferenced validator.
- Loading branch information
Showing
4 changed files
with
107 additions
and
1 deletion.
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
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,44 @@ | ||
<?php | ||
namespace Vanio\DomainBundle\Validator; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
/** | ||
* @Annotation | ||
*/ | ||
class NotReferenced extends Constraint | ||
{ | ||
const IS_REFERENCED_ERROR = '0d52565f-bc0f-41ee-9261-4dc68d29548c'; | ||
|
||
/** @var array */ | ||
protected static $errorNames = [ | ||
self::IS_REFERENCED_ERROR => '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'; | ||
} | ||
} |
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,58 @@ | ||
<?php | ||
namespace Vanio\DomainBundle\Validator; | ||
|
||
use Doctrine\Common\Persistence\ManagerRegistry; | ||
use Symfony\Component\PropertyAccess\PropertyAccess; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
class NotReferencedValidator extends ConstraintValidator | ||
{ | ||
/** @var ManagerRegistry */ | ||
private $registry; | ||
|
||
public function __construct(ManagerRegistry $registry) | ||
{ | ||
$this->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(); | ||
} | ||
} | ||
} |
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