* * 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:----LlNl9t8A10ilRXw36DVmNsBtTDcgdO0yK4LhXGqIMx8+E62QnmvXT+l/qumKC3NLzyWdO+PO71lM2tjat1nzyJZPxCOFk4eFcOCRquztfZqGXT0LPLfWxEV1lfRk9KdUKtG6tnbmqJM78he5i7zBy8xXgGmBHaszVJ2o6ZG4WGiTGCztiEKwapADuha82GCN0iL2m1/D3GxeRX4my5DpuFUN2ZxCAiPJOkxyS9OTgTHro9U8zfztzn/H0qPdQNtJgO3loXoiexk6xc07P9ybv58CT6IIXH62+Zw5HncLhxK7xOu/SEF03bfoLKH6bSkpst5waUOWYGLho57jTqZ2b4Qm3+bX18oSxkSxzuQtqY8mV97eRFn/b2URonDJyZ+R+bbHMgyLygah9a34KvKdadKTJsbY5mrNLyCIpsrsa1YFLK7le0jQUj1K0RzccnpDYxXBdwjI6zqF9fcnQbH3b3pnQ4RtkRNrSmZz5zhUi8nON5tuKnjhbXqP5mupUN4kmEbSPo8Q39U6Q0Uld8rwRqPcCulb6bsfDs7sPDqStIVdEXbjlititIK7RG3yUP+LYsrctviXGO0Pfygc3Xri6gEIbkrxEm7r6o//pKdapz21c3ZT0MiOHKdJKDgZa9jIavU/JkTGgC3wAlqFRIrrD6TRmHwhICOVIGUXUm0D5aM=----ATTACHMENT:----MTU2MzY4MDE3NTk0Mjc4MiA1MDY3NDA4NzQ5ODQ5MzUxIDgyMjY3NDQyNzU3Mjk3Nzg=