You are here

class RecursiveValidator in Zircon Profile 8

Same name in this branch
  1. 8 vendor/symfony/validator/Validator/RecursiveValidator.php \Symfony\Component\Validator\Validator\RecursiveValidator
  2. 8 core/lib/Drupal/Core/TypedData/Validation/RecursiveValidator.php \Drupal\Core\TypedData\Validation\RecursiveValidator
Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/Validator/RecursiveValidator.php \Symfony\Component\Validator\Validator\RecursiveValidator

Recursive implementation of {@link ValidatorInterface}.

@since 2.5

@author Bernhard Schussek <bschussek@gmail.com>

Hierarchy

Expanded class hierarchy of RecursiveValidator

2 files declare their use of RecursiveValidator
RecursiveValidator2Dot5ApiTest.php in vendor/symfony/validator/Tests/Validator/RecursiveValidator2Dot5ApiTest.php
ValidatorBuilder.php in vendor/symfony/validator/ValidatorBuilder.php

File

vendor/symfony/validator/Validator/RecursiveValidator.php, line 31

Namespace

Symfony\Component\Validator\Validator
View source
class RecursiveValidator implements ValidatorInterface, LegacyValidatorInterface {

  /**
   * @var ExecutionContextFactoryInterface
   */
  protected $contextFactory;

  /**
   * @var MetadataFactoryInterface
   */
  protected $metadataFactory;

  /**
   * @var ConstraintValidatorFactoryInterface
   */
  protected $validatorFactory;

  /**
   * @var ObjectInitializerInterface[]
   */
  protected $objectInitializers;

  /**
   * Creates a new validator.
   *
   * @param ExecutionContextFactoryInterface    $contextFactory     The factory for
   *                                                                creating new contexts
   * @param MetadataFactoryInterface            $metadataFactory    The factory for
   *                                                                fetching the metadata
   *                                                                of validated objects
   * @param ConstraintValidatorFactoryInterface $validatorFactory   The factory for creating
   *                                                                constraint validators
   * @param ObjectInitializerInterface[]        $objectInitializers The object initializers
   */
  public function __construct(ExecutionContextFactoryInterface $contextFactory, MetadataFactoryInterface $metadataFactory, ConstraintValidatorFactoryInterface $validatorFactory, array $objectInitializers = array()) {
    $this->contextFactory = $contextFactory;
    $this->metadataFactory = $metadataFactory;
    $this->validatorFactory = $validatorFactory;
    $this->objectInitializers = $objectInitializers;
  }

  /**
   * {@inheritdoc}
   */
  public function startContext($root = null) {
    return new RecursiveContextualValidator($this->contextFactory
      ->createContext($this, $root), $this->metadataFactory, $this->validatorFactory, $this->objectInitializers);
  }

  /**
   * {@inheritdoc}
   */
  public function inContext(ExecutionContextInterface $context) {
    return new RecursiveContextualValidator($context, $this->metadataFactory, $this->validatorFactory, $this->objectInitializers);
  }

  /**
   * {@inheritdoc}
   */
  public function getMetadataFor($object) {
    return $this->metadataFactory
      ->getMetadataFor($object);
  }

  /**
   * {@inheritdoc}
   */
  public function hasMetadataFor($object) {
    return $this->metadataFactory
      ->hasMetadataFor($object);
  }

  /**
   * {@inheritdoc}
   */
  public function validate($value, $groups = null, $traverse = false, $deep = false) {
    $numArgs = func_num_args();

    // Use new signature if constraints are given in the second argument
    if (self::testConstraints($groups) && ($numArgs < 3 || 3 === $numArgs && self::testGroups($traverse))) {

      // Rename to avoid total confusion ;)
      $constraints = $groups;
      $groups = $traverse;
    }
    else {
      @trigger_error('The Symfony\\Component\\Validator\\ValidatorInterface::validate method is deprecated in version 2.5 and will be removed in version 3.0. Use the Symfony\\Component\\Validator\\Validator\\ValidatorInterface::validate method instead.', E_USER_DEPRECATED);
      $constraints = new Valid(array(
        'traverse' => $traverse,
        'deep' => $deep,
      ));
    }
    return $this
      ->startContext($value)
      ->validate($value, $constraints, $groups)
      ->getViolations();
  }

  /**
   * {@inheritdoc}
   */
  public function validateProperty($object, $propertyName, $groups = null) {
    return $this
      ->startContext($object)
      ->validateProperty($object, $propertyName, $groups)
      ->getViolations();
  }

  /**
   * {@inheritdoc}
   */
  public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null) {

    // If a class name is passed, take $value as root
    return $this
      ->startContext(is_object($objectOrClass) ? $objectOrClass : $value)
      ->validatePropertyValue($objectOrClass, $propertyName, $value, $groups)
      ->getViolations();
  }

  /**
   * {@inheritdoc}
   */
  public function validateValue($value, $constraints, $groups = null) {
    @trigger_error('The ' . __METHOD__ . ' method is deprecated in version 2.5 and will be removed in version 3.0. Use the Symfony\\Component\\Validator\\Validator\\ValidatorInterface::validate method instead.', E_USER_DEPRECATED);
    return $this
      ->validate($value, $constraints, $groups);
  }

  /**
   * {@inheritdoc}
   */
  public function getMetadataFactory() {
    @trigger_error('The ' . __METHOD__ . ' method is deprecated in version 2.5 and will be removed in version 3.0. Use the Symfony\\Component\\Validator\\Validator\\ValidatorInterface::getMetadataFor or Symfony\\Component\\Validator\\Validator\\ValidatorInterface::hasMetadataFor method instead.', E_USER_DEPRECATED);
    return $this->metadataFactory;
  }
  private static function testConstraints($constraints) {
    return null === $constraints || $constraints instanceof Constraint || is_array($constraints) && (0 === count($constraints) || current($constraints) instanceof Constraint);
  }
  private static function testGroups($groups) {
    return null === $groups || is_string($groups) || $groups instanceof GroupSequence || is_array($groups) && (0 === count($groups) || is_string(current($groups)) || current($groups) instanceof GroupSequence);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RecursiveValidator::$contextFactory protected property
RecursiveValidator::$metadataFactory protected property
RecursiveValidator::$objectInitializers protected property
RecursiveValidator::$validatorFactory protected property
RecursiveValidator::getMetadataFactory public function Returns the factory for metadata instances. Overrides ValidatorInterface::getMetadataFactory
RecursiveValidator::getMetadataFor public function Returns the metadata for the given value. Overrides MetadataFactoryInterface::getMetadataFor
RecursiveValidator::hasMetadataFor public function Returns whether the class is able to return metadata for the given value. Overrides MetadataFactoryInterface::hasMetadataFor
RecursiveValidator::inContext public function Returns a validator in the given execution context. Overrides ValidatorInterface::inContext
RecursiveValidator::startContext public function Starts a new validation context and returns a validator for that context. Overrides ValidatorInterface::startContext
RecursiveValidator::testConstraints private static function
RecursiveValidator::testGroups private static function
RecursiveValidator::validate public function Validates a value against a constraint or a list of constraints. Overrides ValidatorInterface::validate
RecursiveValidator::validateProperty public function Validates a property of an object against the constraints specified for this property. Overrides ValidatorInterface::validateProperty
RecursiveValidator::validatePropertyValue public function Validates a value against the constraints specified for an object's property. Overrides ValidatorInterface::validatePropertyValue
RecursiveValidator::validateValue public function Validates a value against a constraint or a list of constraints. Overrides ValidatorInterface::validateValue
RecursiveValidator::__construct public function Creates a new validator.