CollectionValidator.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\Context\ExecutionContextInterface;
  14. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  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, __NAMESPACE__.'\Collection');
  27. }
  28. if (null === $value) {
  29. return;
  30. }
  31. if (!is_array($value) && !($value instanceof \Traversable && $value instanceof \ArrayAccess)) {
  32. throw new UnexpectedTypeException($value, 'array or Traversable and 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. if ($context instanceof ExecutionContextInterface) {
  51. $context->getValidator()
  52. ->inContext($context)
  53. ->atPath('['.$field.']')
  54. ->validate($value[$field], $fieldConstraint->constraints);
  55. } else {
  56. // 2.4 API
  57. $context->validateValue($value[$field], $fieldConstraint->constraints, '['.$field.']');
  58. }
  59. }
  60. } elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
  61. if ($context instanceof ExecutionContextInterface) {
  62. $context->buildViolation($constraint->missingFieldsMessage)
  63. ->atPath('['.$field.']')
  64. ->setParameter('{{ field }}', $this->formatValue($field))
  65. ->setInvalidValue(null)
  66. ->setCode(Collection::MISSING_FIELD_ERROR)
  67. ->addViolation();
  68. } else {
  69. $this->buildViolationInContext($context, $constraint->missingFieldsMessage)
  70. ->atPath('['.$field.']')
  71. ->setParameter('{{ field }}', $this->formatValue($field))
  72. ->setInvalidValue(null)
  73. ->setCode(Collection::MISSING_FIELD_ERROR)
  74. ->addViolation();
  75. }
  76. }
  77. }
  78. if (!$constraint->allowExtraFields) {
  79. foreach ($value as $field => $fieldValue) {
  80. if (!isset($constraint->fields[$field])) {
  81. if ($context instanceof ExecutionContextInterface) {
  82. $context->buildViolation($constraint->extraFieldsMessage)
  83. ->atPath('['.$field.']')
  84. ->setParameter('{{ field }}', $this->formatValue($field))
  85. ->setInvalidValue($fieldValue)
  86. ->setCode(Collection::NO_SUCH_FIELD_ERROR)
  87. ->addViolation();
  88. } else {
  89. $this->buildViolationInContext($context, $constraint->extraFieldsMessage)
  90. ->atPath('['.$field.']')
  91. ->setParameter('{{ field }}', $this->formatValue($field))
  92. ->setInvalidValue($fieldValue)
  93. ->setCode(Collection::NO_SUCH_FIELD_ERROR)
  94. ->addViolation();
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }