CollectionValidator.php in Zircon Profile 8.0
File
vendor/symfony/validator/Constraints/CollectionValidator.php
View source
<?php
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class CollectionValidator extends ConstraintValidator {
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');
}
$context = $this->context;
foreach ($constraint->fields as $field => $fieldConstraint) {
$existsInArray = is_array($value) && array_key_exists($field, $value);
$existsInArrayAccess = $value instanceof \ArrayAccess && $value
->offsetExists($field);
if ($existsInArray || $existsInArrayAccess) {
if (count($fieldConstraint->constraints) > 0) {
if ($context instanceof ExecutionContextInterface) {
$context
->getValidator()
->inContext($context)
->atPath('[' . $field . ']')
->validate($value[$field], $fieldConstraint->constraints);
}
else {
$context
->validateValue($value[$field], $fieldConstraint->constraints, '[' . $field . ']');
}
}
}
elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
if ($context instanceof ExecutionContextInterface) {
$context
->buildViolation($constraint->missingFieldsMessage)
->atPath('[' . $field . ']')
->setParameter('{{ field }}', $this
->formatValue($field))
->setInvalidValue(null)
->setCode(Collection::MISSING_FIELD_ERROR)
->addViolation();
}
else {
$this
->buildViolationInContext($context, $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])) {
if ($context instanceof ExecutionContextInterface) {
$context
->buildViolation($constraint->extraFieldsMessage)
->atPath('[' . $field . ']')
->setParameter('{{ field }}', $this
->formatValue($field))
->setInvalidValue($fieldValue)
->setCode(Collection::NO_SUCH_FIELD_ERROR)
->addViolation();
}
else {
$this
->buildViolationInContext($context, $constraint->extraFieldsMessage)
->atPath('[' . $field . ']')
->setParameter('{{ field }}', $this
->formatValue($field))
->setInvalidValue($fieldValue)
->setCode(Collection::NO_SUCH_FIELD_ERROR)
->addViolation();
}
}
}
}
}
}