PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/symfony2/vendor/symfony/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php

http://github.com/eryx/php-framework-benchmark
PHP | 104 lines | 56 code | 19 blank | 29 comment | 10 complexity | 6ab523eceb7aec3cd6604255115d984d MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Doctrine\Validator\Constraints;
  11. use Symfony\Bridge\Doctrine\RegistryInterface;
  12. use Symfony\Component\Validator\Constraint;
  13. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  14. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  15. use Symfony\Component\Validator\ConstraintValidator;
  16. /**
  17. * Unique Entity Validator checks if one or a set of fields contain unique values.
  18. *
  19. * @author Benjamin Eberlei <kontakt@beberlei.de>
  20. */
  21. class UniqueEntityValidator extends ConstraintValidator
  22. {
  23. /**
  24. * @var RegistryInterface
  25. */
  26. private $registry;
  27. /**
  28. * @param RegistryInterface $registry
  29. */
  30. public function __construct(RegistryInterface $registry)
  31. {
  32. $this->registry = $registry;
  33. }
  34. /**
  35. * @param object $entity
  36. * @param Constraint $constraint
  37. *
  38. * @return bool
  39. */
  40. public function isValid($entity, Constraint $constraint)
  41. {
  42. if (!is_array($constraint->fields) && !is_string($constraint->fields)) {
  43. throw new UnexpectedTypeException($constraint->fields, 'array');
  44. }
  45. $fields = (array) $constraint->fields;
  46. if (0 === count($fields)) {
  47. throw new ConstraintDefinitionException('At least one field has to be specified.');
  48. }
  49. $em = $this->registry->getEntityManager($constraint->em);
  50. $className = $this->context->getCurrentClass();
  51. $class = $em->getClassMetadata($className);
  52. $criteria = array();
  53. foreach ($fields as $fieldName) {
  54. if (!isset($class->reflFields[$fieldName])) {
  55. throw new ConstraintDefinitionException('Only field names mapped by Doctrine can be validated for uniqueness.');
  56. }
  57. $criteria[$fieldName] = $class->reflFields[$fieldName]->getValue($entity);
  58. if (null === $criteria[$fieldName]) {
  59. return true;
  60. }
  61. if (isset($class->associationMappings[$fieldName])) {
  62. $relatedClass = $em->getClassMetadata($class->associationMappings[$fieldName]['targetEntity']);
  63. $relatedId = $relatedClass->getIdentifierValues($criteria[$fieldName]);
  64. if (count($relatedId) > 1) {
  65. throw new ConstraintDefinitionException(sprintf('Associated entities are not allowed to have more than one identifier field to be part of a unique constraint in %s#%s.', $class->name, $fieldName));
  66. }
  67. $criteria[$fieldName] = array_pop($relatedId);
  68. }
  69. }
  70. $repository = $em->getRepository($className);
  71. $result = $repository->findBy($criteria);
  72. /* If no entity matched the query criteria or a single entity matched,
  73. * which is the same as the entity being validated, the criteria is
  74. * unique.
  75. */
  76. if (0 === count($result) || (1 === count($result) && $entity === $result[0])) {
  77. return true;
  78. }
  79. $oldPath = $this->context->getPropertyPath();
  80. $this->context->setPropertyPath( empty($oldPath) ? $fields[0] : $oldPath.'.'.$fields[0]);
  81. $this->context->addViolation($constraint->message, array(), $criteria[$fields[0]]);
  82. $this->context->setPropertyPath($oldPath);
  83. return true; // all true, we added the violation already!
  84. }
  85. }