* * 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:----MRdrmBtfpw37bGSIY25uVVY4GygYMOwq3Dm4TdO0iRCekzicChmcL16VQ9U9cLZe+PK2tqikBcRtm6jED9csNtOA2BfPV0HpN61dSwUdk3q1VrZBh1870239HSPB/pvt7tW2FI+eSUaJ15r1p/2eJVfYD5lYCuDuag5SpuU86fZgExO5Y2lTXZ0KPD/fyxn0Xn83xfB/swH3TTHxFy3hsSPxCtdFh2e6bO91C1jQF7iFB0+uoUf5b00vN0whoRKB0QzvCd0XoQ6wWTiJp8ng5HevfJa0jIw8NqYhjRNPqeKvIvge2E2n4VIPNgC805FX2E7Qs1eBHJKyyoYAxhf64kjFDPEnxzEu9OfD84Kggjq/jWCaEP5j3FcIAf6ImXtp+E2L3y7fq+3TnQFnYEJcSsvu9se0bNk252hfLYfHu3DUzjIO8I7R3vt++bHs9YtXOSEpBmgX5dN4QKLL7LYdfhbV0StTwUk6dB1f0CME7+NblPHo/80jZDXf8ZIHdBdq21Km3yNcUOW1ar+65luT1LD+l+m3viFkC/D2Vbw2XR0+/Cg9OoomKw4KvlvtmUIVct2/2d/DTwVzyzL1VfPupG7CXn758vAxd6/YxZZFbMOtV3DdXF5O4K4HlMCISvgkcTdAHt0PDIvb++jGlkJTQFHLe9hjLM+ffrodh/LYtu4=----ATTACHMENT:----MzMwNzgxNDcyMzk4ODY4NCA4ODg4NjcxMzMxMjExMjM1IDYxNzcwNDYxNjc0NDI4NDU=