function ValidKeysConstraint::getAllowedKeys

Same name in other branches
  1. 10 core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/ValidKeysConstraint.php \Drupal\Core\Validation\Plugin\Validation\Constraint\ValidKeysConstraint::getAllowedKeys()

Returns the list of valid keys.

Parameters

\Symfony\Component\Validator\Context\ExecutionContextInterface $context: The current execution context.

Return value

string[] The keys that will be considered valid.

File

core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/ValidKeysConstraint.php, line 89

Class

ValidKeysConstraint
Checks that all the keys of a mapping are valid and required keys present.

Namespace

Drupal\Core\Validation\Plugin\Validation\Constraint

Code

public function getAllowedKeys(ExecutionContextInterface $context) : array {
    $mapping = $context->getObject();
    assert($mapping instanceof Mapping);
    $resolved_type = $mapping->getDataDefinition()
        ->getDataType();
    $valid_keys = $mapping->getValidKeys();
    // If we were given an explicit array of allowed keys, return that.
    if (is_array($this->allowedKeys)) {
        if (!empty(array_diff($this->allowedKeys, $valid_keys))) {
            throw new InvalidArgumentException(sprintf('The type \'%s\' explicitly specifies the allowed keys (%s), but they are not a subset of the statically defined mapping keys in the schema (%s).', $resolved_type, implode(', ', $this->allowedKeys), implode(', ', $valid_keys)));
        }
        return array_intersect($valid_keys, $this->allowedKeys);
    }
    elseif ($this->allowedKeys === '<infer>') {
        return $mapping->getValidKeys();
    }
    throw new InvalidArgumentException("'{$this->allowedKeys}' is not a valid set of allowed keys.");
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.