class ConfigValidation

Same name in other branches
  1. 9 core/modules/config/tests/config_test/src/ConfigValidation.php \Drupal\config_test\ConfigValidation
  2. 8.9.x core/modules/config/tests/config_test/src/ConfigValidation.php \Drupal\config_test\ConfigValidation
  3. 10 core/modules/config/tests/config_test/src/ConfigValidation.php \Drupal\config_test\ConfigValidation

Provides a collection of validation callbacks for testing purposes.

Hierarchy

Expanded class hierarchy of ConfigValidation

1 string reference to 'ConfigValidation'
config_test.schema.yml in core/modules/config/tests/config_test/config/schema/config_test.schema.yml
core/modules/config/tests/config_test/config/schema/config_test.schema.yml

File

core/modules/config/tests/config_test/src/ConfigValidation.php, line 14

Namespace

Drupal\config_test
View source
class ConfigValidation {
    
    /**
     * Validates a llama.
     *
     * @param string $string
     *   The string to validate.
     * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
     *   The validation execution context.
     */
    public static function validateLlama($string, ExecutionContextInterface $context) {
        if (!in_array($string, [
            'llama',
            'alpaca',
            'guanaco',
            'vicuña',
        ], TRUE)) {
            $context->addViolation('no valid llama');
        }
    }
    
    /**
     * Validates cats.
     *
     * @param string $string
     *   The string to validate.
     * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
     *   The validation execution context.
     */
    public static function validateCats($string, ExecutionContextInterface $context) {
        if (!in_array($string, [
            'kitten',
            'cats',
            'nyans',
        ])) {
            $context->addViolation('no valid cat');
        }
    }
    
    /**
     * Validates a number.
     *
     * @param int $count
     *   The integer to validate.
     * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
     *   The validation execution context.
     */
    public static function validateCatCount($count, ExecutionContextInterface $context) {
        if ($count <= 1) {
            $context->addViolation('no enough cats');
        }
    }
    
    /**
     * Validates giraffes.
     *
     * @param string $string
     *   The string to validate.
     * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
     *   The validation execution context.
     */
    public static function validateGiraffes($string, ExecutionContextInterface $context) {
        if (!str_starts_with($string, 'hum')) {
            $context->addViolation('Giraffes just hum');
        }
    }
    
    /**
     * Validates a mapping.
     *
     * @param array $mapping
     *   The data to validate.
     * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
     *   The validation execution context.
     */
    public static function validateMapping($mapping, ExecutionContextInterface $context) {
        // Ensure we are validating the entire mapping by diffing against all the
        // keys.
        $mapping_schema = \Drupal::service('config.typed')->get('config_test.validation')
            ->getValue();
        if ($diff = array_diff_key($mapping, $mapping_schema)) {
            $context->addViolation('Unexpected keys: ' . implode(', ', array_keys($diff)));
        }
    }
    
    /**
     * Validates a sequence.
     *
     * @param array $sequence
     *   The data to validate.
     * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
     *   The validation execution context.
     */
    public static function validateSequence($sequence, ExecutionContextInterface $context) {
        if (isset($sequence['invalid-key'])) {
            $context->addViolation('Invalid giraffe key.');
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary
ConfigValidation::validateCatCount public static function Validates a number.
ConfigValidation::validateCats public static function Validates cats.
ConfigValidation::validateGiraffes public static function Validates giraffes.
ConfigValidation::validateLlama public static function Validates a llama.
ConfigValidation::validateMapping public static function Validates a mapping.
ConfigValidation::validateSequence public static function Validates a sequence.

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