Skip to content

Commit

Permalink
Added NotReferenced validator. (#2)
Browse files Browse the repository at this point in the history
* Added NotReferenced validator.
  • Loading branch information
dvondrak authored and maryo committed Aug 3, 2017
1 parent dae3cf6 commit 22ee662
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Resources/config.xml
Expand Up @@ -41,5 +41,10 @@
<argument type="service" id="doctrine" />
<tag name="validator.constraint_validator" alias="vanio_domain.validator.unique" />
</service>

<service id="vanio_domain.validator.not_referenced" class="Vanio\DomainBundle\Validator\NotReferencedValidator">
<argument type="service" id="doctrine" />
<tag name="validator.constraint_validator" alias="vanio_domain.validator.not_referenced" />
</service>
</services>
</container>
44 changes: 44 additions & 0 deletions Validator/NotReferenced.php
@@ -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';
}
}
58 changes: 58 additions & 0 deletions Validator/NotReferencedValidator.php
@@ -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();
}
}
}
1 change: 0 additions & 1 deletion Validator/UniqueValidator.php
Expand Up @@ -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)) {
Expand Down

0 comments on commit 22ee662

Please sign in to comment.