function SchemaCheckTrait::checkValue

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php \Drupal\Core\Config\Schema\SchemaCheckTrait::checkValue()
  2. 10 core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php \Drupal\Core\Config\Schema\SchemaCheckTrait::checkValue()
  3. 11.x core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php \Drupal\Core\Config\Schema\SchemaCheckTrait::checkValue()

Helper method to check data type.

Parameters

string $key: A string of configuration key.

mixed $value: Value of given key.

Return value

array List of errors found while checking with the corresponding schema.

2 calls to SchemaCheckTrait::checkValue()
CKEditor5PluginDefinition::validateConfiguration in core/modules/ckeditor5/src/Plugin/CKEditor5PluginDefinition.php
Validates the given configuration array.
SchemaCheckTrait::checkConfigSchema in core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php
Checks the TypedConfigManager has a valid schema for the configuration.

File

core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php, line 81

Class

SchemaCheckTrait
Provides a trait for checking configuration schema.

Namespace

Drupal\Core\Config\Schema

Code

protected function checkValue($key, $value) {
    $error_key = $this->configName . ':' . $key;
    
    /** @var \Drupal\Core\TypedData\TypedDataInterface $element */
    $element = $this->schema
        ->get($key);
    // Check if this type has been deprecated.
    $data_definition = $element->getDataDefinition();
    if (!empty($data_definition['deprecated'])) {
        @trigger_error($data_definition['deprecated'], E_USER_DEPRECATED);
    }
    if ($element instanceof Undefined) {
        return [
            $error_key => 'missing schema',
        ];
    }
    // Do not check value if it is defined to be ignored.
    if ($element && $element instanceof Ignore) {
        return [];
    }
    if ($element && is_scalar($value) || $value === NULL) {
        $success = FALSE;
        $type = gettype($value);
        if ($element instanceof PrimitiveInterface) {
            $success = $type == 'integer' && $element instanceof IntegerInterface || ($type == 'double' || $type == 'integer') && $element instanceof FloatInterface || $type == 'boolean' && $element instanceof BooleanInterface || $type == 'string' && $element instanceof StringInterface || $value === NULL;
        }
        elseif ($element instanceof ArrayElement && $element->isNullable() && $value === NULL) {
            $success = TRUE;
        }
        $class = get_class($element);
        if (!$success) {
            return [
                $error_key => "variable type is {$type} but applied schema class is {$class}",
            ];
        }
    }
    else {
        $errors = [];
        if (!$element instanceof TraversableTypedDataInterface) {
            $errors[$error_key] = 'non-scalar value but not defined as an array (such as mapping or sequence)';
        }
        // Go on processing so we can get errors on all levels. Any non-scalar
        // value must be an array so cast to an array.
        if (!is_array($value)) {
            $value = (array) $value;
        }
        $nested_errors = [];
        // Recurse into any nested keys.
        foreach ($value as $nested_value_key => $nested_value) {
            $nested_errors[] = $this->checkValue($key . '.' . $nested_value_key, $nested_value);
        }
        return array_merge($errors, ...$nested_errors);
    }
    // No errors found.
    return [];
}

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