public function Collection::__construct

Same name in this branch
  1. 8.x drupal/core/vendor/guzzle/common/Guzzle/Common/Collection.php \Guzzle\Common\Collection::__construct()
  2. 8.x drupal/core/vendor/symfony/validator/Symfony/Component/Validator/Constraints/Collection.php \Symfony\Component\Validator\Constraints\Collection::__construct()

Initializes the constraint with options.

You should pass an associative array. The keys should be the names of existing properties in this class. The values should be the value for these properties.

Alternatively you can override the method getDefaultOption() to return the name of an existing property. If no associative array is passed, this property is set instead.

You can force that certain options are set by overriding getRequiredOptions() to return the names of these options. If any option is not set here, an exception is thrown.

@api

Parameters

mixed $options The options (as associative array): or the value for the default option (any other type)

Throws

InvalidOptionsException When you pass the names of non-existing options

MissingOptionsException When you don't pass any of the options returned by getRequiredOptions()

ConstraintDefinitionException When you don't pass an associative array, but getDefaultOption() returns NULL

Overrides Constraint::__construct

File

drupal/core/vendor/symfony/validator/Symfony/Component/Validator/Constraints/Collection.php, line 37

Class

Collection
@author Bernhard Schussek <bschussek@gmail.com>

Namespace

Symfony\Component\Validator\Constraints

Code

public function __construct($options = null) {

  // no known options set? $options is the fields array
  if (is_array($options) && !array_intersect(array_keys($options), array(
    'groups',
    'fields',
    'allowExtraFields',
    'allowMissingFields',
    'extraFieldsMessage',
    'missingFieldsMessage',
  ))) {
    $options = array(
      'fields' => $options,
    );
  }
  parent::__construct($options);
  if (!is_array($this->fields)) {
    throw new ConstraintDefinitionException(sprintf('The option "fields" is expected to be an array in constraint %s', __CLASS__));
  }
  foreach ($this->fields as $fieldName => $field) {
    if (!$field instanceof Optional && !$field instanceof Required) {
      $this->fields[$fieldName] = $field = new Required($field);
    }
    if (!is_array($field->constraints)) {
      $field->constraints = array(
        $field->constraints,
      );
    }
    foreach ($field->constraints as $constraint) {
      if (!$constraint instanceof Constraint) {
        throw new ConstraintDefinitionException(sprintf('The value %s of the field %s is not an instance of Constraint in constraint %s', $constraint, $fieldName, __CLASS__));
      }
      if ($constraint instanceof Valid) {
        throw new ConstraintDefinitionException(sprintf('The constraint Valid cannot be nested inside constraint %s. You can only declare the Valid constraint directly on a field or method.', __CLASS__));
      }
    }
  }
}