forked from netmeansnet/NmnMultiUserBundle
/
UniqueEntityValidator.php
125 lines (103 loc) · 4.7 KB
/
UniqueEntityValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace PUGX\MultiUserBundle\Validator\Constraints;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator as BaseValidator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class UniqueEntityValidator extends BaseValidator
{
/**
* @var ManagerRegistry
*/
private $registry;
/**
* @param ManagerRegistry $registry
*/
public function __construct(ManagerRegistry $registry)
{
$this->registry = $registry;
}
/**
* @param object $entity
* @param Constraint $constraint
*/
public function validate($entity, Constraint $constraint)
{
$violations = $this->context->getViolations();
foreach ($violations as $violation) {
if ($violation->getPropertyPath() == 'data.'.$constraint->fields) {
return;
}
}
if (!is_array($constraint->fields) && !is_string($constraint->fields)) {
throw new UnexpectedTypeException($constraint->fields, 'array');
}
if (null !== $constraint->errorPath && !is_string($constraint->errorPath)) {
throw new UnexpectedTypeException($constraint->errorPath, 'string or null');
}
$fields = (array) $constraint->fields;
if (0 === count($fields)) {
throw new ConstraintDefinitionException('At least one field has to be specified.');
}
if ($constraint->em) {
$em = $this->registry->getManager($constraint->em);
} else {
$em = $this->registry->getManagerForClass(get_class($entity));
}
$className = (!is_null($constraint->targetClass)) ? $constraint->targetClass : $this->context->getCurrentClass();
$class = $em->getClassMetadata($constraint->targetClass);
/* @var $class \Doctrine\Common\Persistence\Mapping\ClassMetadata */
$criteria = [];
foreach ($fields as $fieldName) {
if (!$class->hasField($fieldName) && !$class->hasAssociation($fieldName)) {
throw new ConstraintDefinitionException('Only field names mapped by Doctrine can be validated for uniqueness.');
}
$criteria[$fieldName] = $class->reflFields[$fieldName]->getValue($entity);
if (null === $criteria[$fieldName]) {
return;
}
if ($class->hasAssociation($fieldName)) {
/* Ensure the Proxy is initialized before using reflection to
* read its identifiers. This is necessary because the wrapped
* getter methods in the Proxy are being bypassed.
*/
$em->initializeObject($criteria[$fieldName]);
$relatedClass = $em->getClassMetadata($class->getAssociationTargetClass($fieldName));
$relatedId = $relatedClass->getIdentifierValues($criteria[$fieldName]);
if (count($relatedId) > 1) {
throw new ConstraintDefinitionException(
'Associated entities are not allowed to have more than one identifier field to be '.
'part of a unique constraint in: '.$class->getName().'#'.$fieldName
);
}
$criteria[$fieldName] = array_pop($relatedId);
}
}
$repository = $em->getRepository($className);
$result = $repository->{$constraint->repositoryMethod}($criteria);
/* If the result is a MongoCursor, it must be advanced to the first
* element. Rewinding should have no ill effect if $result is another
* iterator implementation.
*/
if ($result instanceof \Iterator) {
$result->rewind();
}
/* If no entity matched the query criteria or a single entity matched,
* which is the same as the entity being validated, the criteria is
* unique.
*/
if (0 === count($result) || (1 === count($result) && $entity === ($result instanceof \Iterator ? $result->current() : current($result)))) {
return;
}
$errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0];
if (\Symfony\Component\HttpKernel\Kernel::MAJOR_VERSION >= 3) {
$this->context->buildViolation($constraint->message)
->atPath($errorPath)
->setInvalidValue($criteria[$fields[0]])
->addViolation();
} else {
$this->context->addViolationAt($errorPath, $constraint->message, [], $criteria[$fields[0]]);
}
}
}