You are here

public function Constraint::__construct in Zircon Profile 8.0

Same name in this branch
  1. 8.0 vendor/symfony/validator/Constraint.php \Symfony\Component\Validator\Constraint::__construct()
  2. 8.0 vendor/composer/semver/src/Constraint/Constraint.php \Composer\Semver\Constraint\Constraint::__construct()
Same name and namespace in other branches
  1. 8 vendor/symfony/validator/Constraint.php \Symfony\Component\Validator\Constraint::__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.

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

11 calls to Constraint::__construct()
AbstractComparison::__construct in vendor/symfony/validator/Constraints/AbstractComparison.php
Initializes the constraint with options.
Callback::__construct in vendor/symfony/validator/Constraints/Callback.php
Initializes the constraint with options.
ComplexDataConstraint::__construct in core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/ComplexDataConstraint.php
Initializes the constraint with options.
Composite::__construct in vendor/symfony/validator/Constraints/Composite.php
The groups of the composite and its nested constraints are made consistent using the following strategy:
Count::__construct in vendor/symfony/validator/Constraints/Count.php
Initializes the constraint with options.

... See full list

11 methods override Constraint::__construct()
AbstractComparison::__construct in vendor/symfony/validator/Constraints/AbstractComparison.php
Initializes the constraint with options.
Callback::__construct in vendor/symfony/validator/Constraints/Callback.php
Initializes the constraint with options.
ComplexDataConstraint::__construct in core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/ComplexDataConstraint.php
Initializes the constraint with options.
Composite::__construct in vendor/symfony/validator/Constraints/Composite.php
The groups of the composite and its nested constraints are made consistent using the following strategy:
Count::__construct in vendor/symfony/validator/Constraints/Count.php
Initializes the constraint with options.

... See full list

File

vendor/symfony/validator/Constraint.php, line 118

Class

Constraint
Contains the properties of a constraint definition.

Namespace

Symfony\Component\Validator

Code

public function __construct($options = null) {
  $invalidOptions = array();
  $missingOptions = array_flip((array) $this
    ->getRequiredOptions());
  $knownOptions = get_object_vars($this);

  // The "groups" option is added to the object lazily
  $knownOptions['groups'] = true;
  if (is_array($options) && count($options) >= 1 && isset($options['value']) && !property_exists($this, 'value')) {
    $options[$this
      ->getDefaultOption()] = $options['value'];
    unset($options['value']);
  }
  if (is_array($options) && count($options) > 0 && is_string(key($options))) {
    foreach ($options as $option => $value) {
      if (array_key_exists($option, $knownOptions)) {
        $this->{$option} = $value;
        unset($missingOptions[$option]);
      }
      else {
        $invalidOptions[] = $option;
      }
    }
  }
  elseif (null !== $options && !(is_array($options) && count($options) === 0)) {
    $option = $this
      ->getDefaultOption();
    if (null === $option) {
      throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint %s', get_class($this)));
    }
    if (array_key_exists($option, $knownOptions)) {
      $this->{$option} = $options;
      unset($missingOptions[$option]);
    }
    else {
      $invalidOptions[] = $option;
    }
  }
  if (count($invalidOptions) > 0) {
    throw new InvalidOptionsException(sprintf('The options "%s" do not exist in constraint %s', implode('", "', $invalidOptions), get_class($this)), $invalidOptions);
  }
  if (count($missingOptions) > 0) {
    throw new MissingOptionsException(sprintf('The options "%s" must be set for constraint %s', implode('", "', array_keys($missingOptions)), get_class($this)), array_keys($missingOptions));
  }
}