class Validator (View source)

Constants

protected DEFAULT_ALIASES_PATH

Default validation rules aliases array path.

protected DEFAULT_MACROS_PATH

Default validation rules macros array path.

protected DEFAULT_RULES_PATH

Default validation rules array path.

private LOGIC_PATH

Validation logic path.

Properties

protected array<string,string> $aliases

Available validation rules aliases.

protected array<string,array<string,mixed>> $context

Current validation context (available results).

protected string $current

The currently validated data key.

protected array $data

Currently loaded data.

protected Result> $errors

Validation errors of the currently loaded data.

protected array<string,string> $labels

Currently loaded validation labels.

protected array<string,string> $macros

Available validation rules macros.

protected array<string,array<string,string|null>> $messages

Currently loaded validation messages.

protected Result> $results

Validations results against the currently loaded data.

protected Rule> $rules

Available validation rules.

protected array<string|int,string|Validation> $validations

Currently loaded validations.

Methods

void
__clone()

Resets validator's internal state when cloned.

__construct(iterable|null $data = null, array|null $validations = null)

Validator constructor.

addAlias(string $name, string $rule)

Adds a new rule alias to the validator.

addMacro(string $name, Validation $rules)

Adds a new rule macro to the validator.

addRule(Rule|array $rule)

Adds a new rule to the validator.

void
check()

Checks current validator's data against current validator's validations.

array
createErrorMessages(array $validations)

Creates error messages for unsuccessful validations and injects the necessary variables into each message.

bool
executeRule(string $name, string $statement, mixed $input)

Executes a single validation rule.

array
getAliases()

Returns validator's available rules aliases.

array
getErrors()

Returns validation errors.

array
getMacros()

Returns validator's available rules macros.

array
getResults()

Returns validation results (errors and successes).

array
getRules()

Returns validator's available rules.

bool
isOK()

Checks whether the validation has passed or not.

array
load(string $path)

Loads the passed path as an array (include $path).

void
reset()

Resets validator's internal state.

setData(iterable $data)

Sets the data to validate.

setLabels(array $labels)

Sets labels for each data key. These labels will be injected in validation messages.

setMessages(array $messages)

Sets rule message override for each validation rule of each data key.

setValidations(array $validations)

Sets the validation expression for each data key.

array
validate()

Validates current validator's data against current validator's validations and return the results.

array
validateAll(iterable $data, array $validations)

Validates the passed data against the passed validations.

static Result
validateData(mixed $data, Validation|string|array $validation, array $messages = [], array $labels = [])

Validates the passed data against the passed validation.

validateOne(mixed $value, Validation|string $validation)

Validates a single value against the passed validation.

validation()

Returns an instance of the validation expression builder.

Details

void __clone()

Resets validator's internal state when cloned.

Return Value

void

__construct(iterable|null $data = null, array|null $validations = null)

Validator constructor.

Parameters

iterable|null $data

[optional] The data to validate.

array|null $validations

[optional] The validation rules for each data key.

Validator addAlias(string $name, string $rule)

Adds a new rule alias to the validator.

Parameters

string $name

Alias name.

string $rule

Validation rule name.

Return Value

Validator

Exceptions

InvalidRuleAliasException

Validator addMacro(string $name, Validation $rules)

Adds a new rule macro to the validator.

Parameters

string $name

Macro name.

Validation $rules

validation expression string or object.

Return Value

Validator

Exceptions

InvalidRuleMacroException

Validator addRule(Rule|array $rule)

Adds a new rule to the validator.

Parameters

Rule|array $rule

A rule object or a rule definition array (see MAKS\Mighty\Rule::SCHEMA for more info).

Return Value

Validator

Exceptions

InvalidRuleDefinitionException

void check()

Checks current validator's data against current validator's validations.

Return Value

void

Exceptions

ValidationFailedException

protected array createErrorMessages(array $validations)

Creates error messages for unsuccessful validations and injects the necessary variables into each message.

Parameters

array $validations

The validations array. An associative array where key is the rule name and value is the result of the execution.

Return Value

array

An associative array where key is the rule name and value is the message.

protected bool executeRule(string $name, string $statement, mixed $input)

Executes a single validation rule.

Parameters

string $name

The name of the rule.

string $statement

The statement of the rule.

mixed $input

The input to pass to the rule.

Return Value

bool

The result of the execution.

Exceptions

UnknownValidationRuleException
InexecutableRuleException

array getAliases()

Returns validator's available rules aliases.

Return Value

array

array getErrors()

Returns validation errors.

Return Value

array

array getMacros()

Returns validator's available rules macros.

Return Value

array

array getResults()

Returns validation results (errors and successes).

Return Value

array

array getRules()

Returns validator's available rules.

Return Value

array

bool isOK()

Checks whether the validation has passed or not.

Return Value

bool

private array load(string $path)

Loads the passed path as an array (include $path).

Parameters

string $path

Return Value

array

void reset()

Resets validator's internal state.

Return Value

void

Validator setData(iterable $data)

Sets the data to validate.

Parameters

iterable $data

The data to validate.

Return Value

Validator

Validator setLabels(array $labels)

Sets labels for each data key. These labels will be injected in validation messages.

Parameters

array $labels

An associative array where key is a data key and value is the label. Nested elements can be accessed using dot notation (someKey.someNestedKey). Keys can have the wildcard * after a dot to match all nested keys.

Return Value

Validator

Validator setMessages(array $messages)

Sets rule message override for each validation rule of each data key.

Parameters

array $messages

An associative array where key is a data key and value is an associative array where key is the rule name and value is the message. See MAKS\Mighty\Rule::SCHEMA['@message'] for more info about placeholders. Nested elements can be accessed using dot notation (someKey.someNestedKey). Keys can have the wildcard * after a dot to match all nested keys.

Return Value

Validator

Validator setValidations(array $validations)

Sets the validation expression for each data key.

Parameters

array $validations

The validation expression for each data key. Nested elements can be accessed using dot notation (someKey.someNestedKey). Keys can have the wildcard * after a dot to match all nested keys.

Return Value

Validator

array validate()

Validates current validator's data against current validator's validations and return the results.

Return Value

array

The validation results.

Exceptions

UnknownValidationRuleException
InexecutableRuleException
InvalidBitwiseExpressionException

array validateAll(iterable $data, array $validations)

Validates the passed data against the passed validations.

Parameters

iterable $data

The data to validate.

array $validations

The validation expression for each data key. Nested elements can be accessed using dot notation (someKey.someNestedKey). Keys can have the wildcard * after a dot to match all nested keys.

Return Value

array

The validation results.

Exceptions

UnknownValidationRuleException
InexecutableRuleException
InvalidBitwiseExpressionException

static Result validateData(mixed $data, Validation|string|array $validation, array $messages = [], array $labels = [])

Validates the passed data against the passed validation.

This method is a static helper that combines the functionality of self::validateAll() and self::validateOne().

Parameters

mixed $data

The data to validate. Can be a single value or an associative array of values.

Validation|string|array $validation

The validation expression. Can be a single validation expression (see self::validateOne()), or an array of validation expressions for each data key (see self::validateAll()).

array $messages

An associative array where key is a data key and value is an associative array where key is the rule name and value is the message. See MAKS\Mighty\Rule::SCHEMA['@message'] for more info about placeholders. NOTE: Relevant only if $data is not a single value ($validation is an array).

array $labels

An associative array where key is a data key and value is the label. NOTE: Relevant only if $data is not a single value ($validation is an array).

Return Value

Result

The validation results.

Exceptions

UnknownValidationRuleException
InexecutableRuleException
InvalidBitwiseExpressionException

Result validateOne(mixed $value, Validation|string $validation)

Validates a single value against the passed validation.

Parameters

mixed $value

The value to validate.

Validation|string $validation

The validation expression string or object.

Return Value

Result

The validation result.

Exceptions

UnknownValidationRuleException
InexecutableRuleException
InvalidBitwiseExpressionException

Validation validation()

Returns an instance of the validation expression builder.

Return Value

Validation