Final Class Yiisoft\Validator\Helper\RulesNormalizer
| Inheritance | Yiisoft\Validator\Helper\RulesNormalizer |
|---|
A helper class used to normalize different types of data to the iterable with rule instances ({@see RuleInterface}).
Can be applied to the rules grouped by properties with adding some default settings if needed.
Note that when using {@see \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
| Method | Description | Defined By |
|---|---|---|
| normalize() | Normalizes different types of data to the iterable with rule instances ({@see RuleInterface}) maintaining the grouping by properties and applying some default settings if needed. | Yiisoft\Validator\Helper\RulesNormalizer |
| normalizeList() | Normalizes a set of rules using {@see RulesNormalizerIterator}. This is done for every individual rule: | Yiisoft\Validator\Helper\RulesNormalizer |
Method Details
Normalizes different types of data to the iterable with rule instances ({@see 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 ({@see \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 {@see \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 array normalize ( callable|iterable|object|string|null $rules = null, mixed|null $data = null, callable|null $defaultSkipOnEmptyCondition = null ) | ||
| $rules | callable|iterable|object|string|null |
Rules source. The following types are supported:
|
| $data | mixed|null |
Validated data, |
| $defaultSkipOnEmptyCondition | callable|null |
A default "skip on empty" condition
({@see \Yiisoft\Validator\SkipOnEmptyInterface}), already normalized. Used to optimize setting the same value in all the rules.
Defaults to |
| 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;
}
Normalizes a set of rules using {@see RulesNormalizerIterator}. This is done for every individual rule:
- Wrapping a callable with {@see \Yiisoft\Validator\Helper\Callback} rule.
- Verifying that it's a valid rule instance.
| public static iterable normalizeList ( callable|iterable|Yiisoft\Validator\RuleInterface $rules ) | ||
| $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 {@see \Yiisoft\Validator\RuleInterface} implementation. |
public static function normalizeList(iterable|callable|RuleInterface $rules): iterable
{
return new RulesNormalizerIterator(
is_iterable($rules) ? $rules : [$rules],
);
}
Signup or Login in order to comment.