0 follower

Final Class Yiisoft\Validator\Helper\RulesNormalizer

InheritanceYiisoft\Validator\Helper\RulesNormalizer

A helper class used to normalize different types of data to the iterable with rule instances (Yiisoft\Validator\RuleInterface).

Can be applied to the rules grouped by properties with adding some default settings if needed.

Note that when using Yiisoft\Validator\Validator, normalization is performed automatically.

Psalm Types

Name Value
NormalizedRulesList iterable<integer, Yiisoft\Validator\RuleInterface>
NormalizedRulesMap array<integer|string, \Yiisoft\Validator\Helper\NormalizedRulesList>

Public Methods

Hide inherited methods

Method Description Defined By
normalize() Normalizes different types of data to the iterable with rule instances (Yiisoft\Validator\RuleInterface) maintaining the grouping by properties and applying some default settings if needed. Yiisoft\Validator\Helper\RulesNormalizer
normalizeList() Normalizes a set of rules using Yiisoft\Validator\Helper\RulesNormalizerIterator. This is done for every individual rule: Yiisoft\Validator\Helper\RulesNormalizer

Method Details

Hide inherited methods

normalize() public static method

Normalizes different types of data to the iterable with rule instances (Yiisoft\Validator\RuleInterface) maintaining the grouping by properties and applying some default settings if needed.

Based on rules source and additionally provided data this is what is done initially:

  • If rules' source is already an iterable, it will be left as is.
  • If rules' source is not set (null) and validated data provided its own rules, they will be used instead.
  • If rules' source is an object providing rules via separate method, they will be fetched and used.
  • A single rule instance (Yiisoft\Validator\RuleInterface) or a callable will be wrapped with array resulting in list with 1 item.
  • If rules' source is a name of class providing rules via PHP attributes, they will be fetched and used.

And then for every individual rule within a set by property:

  • A callable is wrapped with \Yiisoft\Validator\Helper\Callback rule.
  • For any other type verifies that it's a valid rule instance.
  • If default "skip on empty" condition is set, applies it if possible.

For properties there is an additional internal validation for being integer / string.

public static normalize( callable|iterable|object|string|null $rules null, mixed|null $data null, callable|null $defaultSkipOnEmptyCondition null ): array
$rules callable|iterable|object|string|null

Rules source. The following types are supported:

  • Iterable (can contain single rule instances and callables for individual property).
  • null
  • Object providing rules via separate method.
  • Single rule instance / callable.
  • Name of a class providing rules via PHP attributes.
$data mixed|null

Validated data,

$defaultSkipOnEmptyCondition callable|null

A default "skip on empty" condition (Yiisoft\Validator\SkipOnEmptyInterface), already normalized. Used to optimize setting the same value in all the rules. Defaults to null meaning that it's not used.

return array

Rules normalized as a whole and individually, ready to use for validation.

throws InvalidArgumentException

When property is neither an integer nor a string.

throws ReflectionException

When parsing rules from PHP attributes failed.

                public static function normalize(
    callable|iterable|object|string|null $rules = null,
    mixed $data = null,
    ?callable $defaultSkipOnEmptyCondition = null,
): array {
    $rules = self::prepareRulesIterable($rules, $data);
    $normalizedRules = [];
    foreach ($rules as $property => $propertyRules) {
        if (!is_int($property) && !is_string($property)) {
            throw new InvalidArgumentException(
                sprintf(
                    'A property can only have an integer or a string type. %s given.',
                    get_debug_type($property),
                ),
            );
        }
        $normalizedRules[$property] = new RulesNormalizerIterator(
            is_iterable($propertyRules) ? $propertyRules : [$propertyRules],
            $defaultSkipOnEmptyCondition,
        );
    }
    return $normalizedRules;
}

            
normalizeList() public static method

Normalizes a set of rules using Yiisoft\Validator\Helper\RulesNormalizerIterator. This is done for every individual rule:

  • Wrapping a callable with \Yiisoft\Validator\Helper\Callback rule.
  • Verifying that it's a valid rule instance.
public static normalizeList( callable|iterable|Yiisoft\Validator\RuleInterface $rules ): iterable
$rules callable|iterable|Yiisoft\Validator\RuleInterface

A set of rules or a single rule for normalization.

return iterable

An iterable with every rule checked and normalized.

throws InvalidArgumentException

When at least one of the rules is neither a callable nor a Yiisoft\Validator\RuleInterface implementation.

                public static function normalizeList(iterable|callable|RuleInterface $rules): iterable
{
    return new RulesNormalizerIterator(
        is_iterable($rules) ? $rules : [$rules],
    );
}