CollectionValidator.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\ConstraintValidator;
  13. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  14. use Symfony\Component\Validator\Exception\UnexpectedValueException;
  15. /**
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. */
  18. class CollectionValidator extends ConstraintValidator
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function validate($value, Constraint $constraint)
  24. {
  25. if (!$constraint instanceof Collection) {
  26. throw new UnexpectedTypeException($constraint, Collection::class);
  27. }
  28. if (null === $value) {
  29. return;
  30. }
  31. if (!\is_array($value) && !($value instanceof \Traversable && $value instanceof \ArrayAccess)) {
  32. throw new UnexpectedValueException($value, 'array|(Traversable&ArrayAccess)');
  33. }
  34. // We need to keep the initialized context when CollectionValidator
  35. // calls itself recursively (Collection constraints can be nested).
  36. // Since the context of the validator is overwritten when initialize()
  37. // is called for the nested constraint, the outer validator is
  38. // acting on the wrong context when the nested validation terminates.
  39. //
  40. // A better solution - which should be approached in Symfony 3.0 - is to
  41. // remove the initialize() method and pass the context as last argument
  42. // to validate() instead.
  43. $context = $this->context;
  44. foreach ($constraint->fields as $field => $fieldConstraint) {
  45. // bug fix issue #2779
  46. $existsInArray = \is_array($value) && \array_key_exists($field, $value);
  47. $existsInArrayAccess = $value instanceof \ArrayAccess && $value->offsetExists($field);
  48. if ($existsInArray || $existsInArrayAccess) {
  49. if (\count($fieldConstraint->constraints) > 0) {
  50. $context->getValidator()
  51. ->inContext($context)
  52. ->atPath('['.$field.']')
  53. ->validate($value[$field], $fieldConstraint->constraints);
  54. }
  55. } elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
  56. $context->buildViolation($constraint->missingFieldsMessage)
  57. ->atPath('['.$field.']')
  58. ->setParameter('{{ field }}', $this->formatValue($field))
  59. ->setInvalidValue(null)
  60. ->setCode(Collection::MISSING_FIELD_ERROR)
  61. ->addViolation();
  62. }
  63. }
  64. if (!$constraint->allowExtraFields) {
  65. foreach ($value as $field => $fieldValue) {
  66. if (!isset($constraint->fields[$field])) {
  67. $context->buildViolation($constraint->extraFieldsMessage)
  68. ->atPath('['.$field.']')
  69. ->setParameter('{{ field }}', $this->formatValue($field))
  70. ->setInvalidValue($fieldValue)
  71. ->setCode(Collection::NO_SUCH_FIELD_ERROR)
  72. ->addViolation();
  73. }
  74. }
  75. }
  76. }
  77. }