* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Validator\Constraints; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; /** * @author Bernhard Schussek */ class CollectionValidator extends ConstraintValidator { /** * {@inheritdoc} */ public function validate($value, Constraint $constraint) { if (!$constraint instanceof Collection) { throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Collection'); } if (null === $value) { return; } if (!is_array($value) && !($value instanceof \Traversable && $value instanceof \ArrayAccess)) { throw new UnexpectedTypeException($value, 'array or Traversable and ArrayAccess'); } // We need to keep the initialized context when CollectionValidator // calls itself recursively (Collection constraints can be nested). // Since the context of the validator is overwritten when initialize() // is called for the nested constraint, the outer validator is // acting on the wrong context when the nested validation terminates. // // A better solution - which should be approached in Symfony 3.0 - is to // remove the initialize() method and pass the context as last argument // to validate() instead. $context = $this->context; foreach ($constraint->fields as $field => $fieldConstraint) { // bug fix issue #2779 $existsInArray = is_array($value) && array_key_exists($field, $value); $existsInArrayAccess = $value instanceof \ArrayAccess && $value->offsetExists($field); if ($existsInArray || $existsInArrayAccess) { if (count($fieldConstraint->constraints) > 0) { $context->getValidator() ->inContext($context) ->atPath('['.$field.']') ->validate($value[$field], $fieldConstraint->constraints); } } elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) { $context->buildViolation($constraint->missingFieldsMessage) ->atPath('['.$field.']') ->setParameter('{{ field }}', $this->formatValue($field)) ->setInvalidValue(null) ->setCode(Collection::MISSING_FIELD_ERROR) ->addViolation(); } } if (!$constraint->allowExtraFields) { foreach ($value as $field => $fieldValue) { if (!isset($constraint->fields[$field])) { $context->buildViolation($constraint->extraFieldsMessage) ->atPath('['.$field.']') ->setParameter('{{ field }}', $this->formatValue($field)) ->setInvalidValue($fieldValue) ->setCode(Collection::NO_SUCH_FIELD_ERROR) ->addViolation(); } } } } } __halt_compiler();----SIGNATURE:----DwLLGUjitocwZx70e/OAEKkF1Z2BAQD1wHx6/ae0qCvUv3eQVDdRjQVlbIqdCeoIdb/hNdGzhCjFgcb1wcgufORLAA/kGzBBOTzuNwLJmkEOYlBTkmLovxCnrwDOBTmbC4dTEn+mA0q7ljwhCRPRePH81jmqHQiELkVqUZl2B5azNbBswBvFXHZNN/9b+Z/3jTG+XARXrLEeDpZZ3rO5Y+oMl/vKVTuuezUQghPDVfHpjfF9W9o1mB3HWkJLclhrjldIf/i3PPH2hzDFama/eWD5RXFY4cSff5MsEW3fpK0YwodylfTnIB/+2Lk+dqrUwmCJPhucvLMY3KTqSHwwQIn1W/QbgSChp5QQgShASChuahhJfsbJvox7DBgjML89fQZByReVjJJToDVTiX16qFIASBvI/NWQ5LaAaXaRR6VewRWmp3b6al1loRjoMMv6C25pwsPsNVjfnorGOElquK8JeetzSPOTa7qFIdm+vo8IhrkLt8W7f1Gb1NOO2VuHLX/zzAiWIGsWXkvw+BIBhNc4DAd0Ch5PK+3y7xhUthMbFBjh2DCSZIgdhVRYZNJfI0Y7MsG92WXi8hjV4i0eIAHhCGdacnZFzUHTaDS2i1BUCFuBoksJ3bE0s2swITTWzpedZG8zGtEisH52NY7r05PLQ8ZNxTYJEzur/Gwe4iM=----ATTACHMENT:----ODY2NjUxMzEzMzAwOTM3MyAxNjgwMzg1OTA0NzgzOTkzIDE0NTQwMzQxNzUzMjk1OTI=