class Rule extends Definition implements Stringable (View source)

Constants

protected DEFAULT

Rule definition array defaults.

SCHEMA

Rule definition schema/structure.

NOTE: All words prefixed with the @ symbol are variables specific to the rule definition.

  • @rule: (object) [magic] The rule object. This value is a magic value and is not required inside rule definition array. It is used to retrieve the rule object. Useful for getting/setting some data of the rule object like the statement or any other attribute inside the callback if needed.

  • @name: (string) [required] Rule name. This value can be any string consisting of alphanumeric characters, dots, dashes, underscores (matches: /^[A-Za-z0-9_\-\.]+$/), and between 2 and 255 characters.

  • @arguments: (array|null) [optional] Rule arguments. If empty, this means that the rule accepts no arguments. Otherwise, it denotes the data types of the arguments. This value serves two roles, at parsing time it will be used to set arguments data types, later on it will be filled with the actual values in their expected data types. Valid data types casts are: null, boolean or bool, integer or int, float or double, string, array, and object. All data types must be stringified using JSON Spec rules and when supplied, some extra care needs to be taken (especially with array and object), the JSON needs to be escaped by wrapping it as a whole with' (single quotes) and escaping any single quotes inside the JSON with \ (backslash) —using addcslashes($json, "'")— to avoid CSV collisions when parsing. Arguments can also be variadic by prefixing them with ... (i.e '...string'). Note that only the last argument can be variadic, if an argument is variadic and is not the last, all extra arguments will be packed into the resulting array starting from the variadic position.

  • @callback: (callable|null) [optional] Rule callback function. This is where the logic of the rule lives. The callback should validate, sanitize, or check the input and return some value. If empty, the input value will be simply returned as is (fallback: fn ($input) => $input).

  • @parameters: (array|null) [optional] Rule callback parameters. Normally this should look something like ['@input'] (fallback: ['@input']). More parameters can be added depending on callback function requirements. The position of the @input depends on the callback function (can be first or any other position). Note that the @input will be injected when the rule is executed. This value has access to the @rule, @name, @input, and @arguments injectables. Nested data can be accessed using dot notation @injectable.someKey or @injectable.0 (with numeric arrays). Fallbacks can also be specified using the syntax @injectable.someKey:fallback (fallbacks must be scalar).

  • @input: (mixed) [magic] Rule input. This value is a magic value and is not required inside rule definition array. It references the input value that was provided to the rule when executed.

  • @output: (mixed) [magic] Rule callback output. This value is a magic value and is not required inside rule definition array. It references the output resulting from executing rule callback on rule input (fallback: @input, see @callback).

  • @comparison: (array|null) [optional] Rule comparison. This is used to compare the rule's callback result against any currently available data. Normally this should look something like ['@output', '!==', false] (fallback: ['@output', '&&', true], returns truthiness of the result). This value has access to the @rule, @name, @input, @arguments, @callback, @parameters, and @output injectables. Nested data can be accessed using dot notation @injectable.someKey or @injectable.0 (with numeric arrays). Fallbacks can also be specified using the syntax @injectable.someKey:fallback (fallbacks must be scalar). All PHP logical and bitwise operators are available with the same syntax (but of course as strings), namely: (L) Logical: ! or not; == or eq; != or <> or neq; === or id; !== or nid; < or lt; <= or lte; > or gt; >= or gte; && or and; || or or; xor. (B) Bitwise: ~; &; |; ^; <<; >>. If an invalid operator is specified, && will be used instead.

  • @message: (string|null) [optional] Rule message. This value is optional and is used during validation to provide feedback to the user in case of failure. This value has access to the @input, @output, @arguments, @parameters, @label and @extra injectables. Nested data can be accessed using dot notation @injectable.someKey or @injectable.0 (with numeric arrays). Fallbacks can also be specified using the syntax @injectable.0:fallback (fallbacks must be scalar). Note that injectables in messages have to be wrapped with } (i.e. ${@injectable:fallback}) as placeholder.

  • @variables: (array|null) [magic|optional] Rule variables. This value is a magic/optional and is not required inside rule definition array. It is an associative array that holds injectables that can be used to inject some variables into @message placeholders. It contains the variables @input, @output, @arguments, @parameters, @label, and @extra after executing the rule. The @label injectable, is a special injectable that should be overwritten by the validator that executed the rule, it serves as a label for the message to provide more friendly and specific error messages (fallback: The field). The @variables may also sometimes contain the @extra variable, this injectable is also a special injectable that contains some extra variables that the @callback may want to make available to the @message (this is a convention and is not a hard rule). If the @variables is provided in the definition, it will be merged with rule variables after the execution. Note that variables (with the same keys) resulting from executing the rule will overwrite the ones provided in the definition.

  • @description: (string|null) [optional] Rule description. This value is optional and has no purpose other than to provide a description for the rule.

  • @example: (string|null) [optional] Rule example. This value is optional and has no purpose other than to provide an example of how to use the rule.

Properties

protected array<string,null|string|array|callable> $definition

Rule definition array.

from  Definition
protected mixed $input

Rule input value.

protected string $statement

Rule statement string.

static protected Closure $translator

Rule messages translator.

Methods

__construct(array|null $definition = null, string|null $statement = null, mixed $input = null)

Rule constructor.

mixed
__invoke(mixed $input)

Executes the rule when the object is invoked as a function.

string
__toString()

Returns the current rule statement.

array|static
arguments(array|false $arguments = false)

Sets the arguments of the rule (returns object), or gets it if parameter is false or not specified.

void
assertDefinitionIsValid(array $definition)

Asserts that the rule definition is valid.

void
assertStatementIsValid(string $statement)

Asserts that the rule statement is valid.

callable|static
callback(callable|false $callback = false)

Sets the callback of the rule (returns object), or gets it if parameter is false or not specified.

array|static
comparison(array|false $comparison = false)

Sets the comparison of the rule (returns object), or gets it if parameter is false or not specified.

string
createErrorMessage(string|null $message = null, array|null $variables = null)

Creates an error message for the rule using the currently available variables or the passed ones.

null|string|static
description(string|false $description = false)

Sets the description of the rule (returns object), or gets it if parameter is false or not specified.

null|string|static
example(string|false $example = false)

Sets the example of the rule (returns object), or gets it if parameter is false or not specified.

mixed
execute(mixed $input = null)

Executes the rule against some input (the current one or the passed one).

array
getArguments()

Gets the arguments of the rule.

callable
getCallback()

Gets the callback of the rule.

array
getComparison()

Gets the comparison of the rule.

array
getDefinition()

Gets rule definition array.

string|null
getDescription()

Gets the description of the rule.

string|null
getExample()

Gets the example of the rule.

mixed
getInput()

Gets rule input.

string
getMessage()

Gets the message of the rule.

string
getName()

Gets the name of the rule.

array
getParameters()

Gets the parameters of the rule.

string
getStatement()

Gets rule statement string.

array|null
getVariables()

Gets the variables of the rule.

string|static
message(string|false $message = false)

Sets the message of the rule (returns object), or gets it if parameter is false or not specified.

string|static
name(string|false $name = false)

Sets the name of the rule (returns object), or gets it if parameter is false or not specified.

array|static
parameters(array|false $parameters = false)

Sets the parameters of the rule (returns object), or gets it if parameter is false or not specified.

setArguments(array $arguments)

Gets the arguments of the rule.

setCallback(callable $callback)

Sets the callback of the rule.

setComparison(array $comparison)

Sets the comparison of the rule.

setDefinition(array $definition)

Sets rule definition array.

setDescription(string $description)

Sets the description of the rule.

setExample(string $example)

Sets the example of the rule.

setInput(mixed $input)

Sets rule input.

setMessage(string $message)

Sets the message of the rule.

static void
setMessageTranslator(Closure $translator)

Sets rule messages global translator.

setName(string $name)

Sets the name of the rule.

setParameters(array $parameters)

Sets the parameters of the rule.

setStatement(string $statement)

Sets rule statement string.

setVariables(array|null $variables)

Sets the variables of the rule.

null|array|static
variables(null|array|false $variables = false)

Sets the variables of the rule (returns object), or gets it if parameter is false or not specified.

Details

__construct(array|null $definition = null, string|null $statement = null, mixed $input = null)

Rule constructor.

Parameters

array|null $definition

[optional] Rule definition.

string|null $statement

[optional] Rule statement.

mixed $input

[optional] Rule input.

mixed __invoke(mixed $input)

Executes the rule when the object is invoked as a function.

Parameters

mixed $input

Rule input.

Return Value

mixed

string __toString()

Returns the current rule statement.

Return Value

string

array|static arguments(array|false $arguments = false)

Sets the arguments of the rule (returns object), or gets it if parameter is false or not specified.

Parameters

array|false $arguments

The arguments of the rule.

Return Value

array|static

protected void assertDefinitionIsValid(array $definition)

Asserts that the rule definition is valid.

Parameters

array $definition

The rule definition.

Return Value

void

Exceptions

InvalidRuleDefinitionException

protected void assertStatementIsValid(string $statement)

Asserts that the rule statement is valid.

Parameters

string $statement

Rule statement.

Return Value

void

Exceptions

InvalidRuleStatementException

callable|static callback(callable|false $callback = false)

Sets the callback of the rule (returns object), or gets it if parameter is false or not specified.

Parameters

callable|false $callback

The callback of the rule.

Return Value

callable|static

array|static comparison(array|false $comparison = false)

Sets the comparison of the rule (returns object), or gets it if parameter is false or not specified.

Parameters

array|false $comparison

The comparison of the rule.

Return Value

array|static

string createErrorMessage(string|null $message = null, array|null $variables = null)

Creates an error message for the rule using the currently available variables or the passed ones.

Parameters

string|null $message

[optional] Override for current rule message.

array|null $variables

[optional] Overrides for current rule variables (will be merged with current rule variables).

Return Value

string

The message with the variables injected.

null|string|static description(string|false $description = false)

Sets the description of the rule (returns object), or gets it if parameter is false or not specified.

Parameters

string|false $description

The description of the rule.

Return Value

null|string|static

null|string|static example(string|false $example = false)

Sets the example of the rule (returns object), or gets it if parameter is false or not specified.

Parameters

string|false $example

The example of the rule.

Return Value

null|string|static

mixed execute(mixed $input = null)

Executes the rule against some input (the current one or the passed one).

Parameters

mixed $input

[optional] The input that the rule will be executed against.

Return Value

mixed

Exceptions

InexecutableRuleException

array getArguments()

Gets the arguments of the rule.

Return Value

array

callable getCallback()

Gets the callback of the rule.

Return Value

callable

array getComparison()

Gets the comparison of the rule.

Return Value

array

array getDefinition()

Gets rule definition array.

Return Value

array

string|null getDescription()

Gets the description of the rule.

Return Value

string|null

string|null getExample()

Gets the example of the rule.

Return Value

string|null

mixed getInput()

Gets rule input.

Return Value

mixed

string getMessage()

Gets the message of the rule.

Return Value

string

string getName()

Gets the name of the rule.

Return Value

string

array getParameters()

Gets the parameters of the rule.

Return Value

array

string getStatement()

Gets rule statement string.

Return Value

string

array|null getVariables()

Gets the variables of the rule.

Return Value

array|null

string|static message(string|false $message = false)

Sets the message of the rule (returns object), or gets it if parameter is false or not specified.

Parameters

string|false $message

The message of the rule.

Return Value

string|static

string|static name(string|false $name = false)

Sets the name of the rule (returns object), or gets it if parameter is false or not specified.

Parameters

string|false $name

The name of the rule.

Return Value

string|static

array|static parameters(array|false $parameters = false)

Sets the parameters of the rule (returns object), or gets it if parameter is false or not specified.

Parameters

array|false $parameters

The parameters of the rule.

Return Value

array|static

Definition setArguments(array $arguments)

Gets the arguments of the rule.

Parameters

array $arguments

The arguments of the rule.

Return Value

Definition

Definition setCallback(callable $callback)

Sets the callback of the rule.

Parameters

callable $callback

The callback of the rule.

Return Value

Definition

Definition setComparison(array $comparison)

Sets the comparison of the rule.

Parameters

array $comparison

The comparison of the rule.

Return Value

Definition

Definition setDefinition(array $definition)

Sets rule definition array.

Parameters

array $definition

Rule definition array.

Return Value

Definition

Exceptions

InvalidRuleDefinitionException

Definition setDescription(string $description)

Sets the description of the rule.

Parameters

string $description

The description of the rule.

Return Value

Definition

Definition setExample(string $example)

Sets the example of the rule.

Parameters

string $example

The example of the rule.

Return Value

Definition

Rule setInput(mixed $input)

Sets rule input.

Parameters

mixed $input

Rule input.

Return Value

Rule

Definition setMessage(string $message)

Sets the message of the rule.

Parameters

string $message

The message of the rule.

Return Value

Definition

static void setMessageTranslator(Closure $translator)

Sets rule messages global translator.

Parameters

Closure $translator

Rule messages translator callback. The callback will be passed the following arguments and must return a string:

  • The $message before injecting the placeholders.
  • The $name of the rule.

Return Value

void

Definition setName(string $name)

Sets the name of the rule.

Parameters

string $name

The name of the rule.

Return Value

Definition

Definition setParameters(array $parameters)

Sets the parameters of the rule.

Parameters

array $parameters

The parameters of the rule.

Return Value

Definition

Rule setStatement(string $statement)

Sets rule statement string.

Parameters

string $statement

Rule statement.

Return Value

Rule

Exceptions

InvalidRuleStatementException

Definition setVariables(array|null $variables)

Sets the variables of the rule.

Parameters

array|null $variables

The variables of the rule.

Return Value

Definition

null|array|static variables(null|array|false $variables = false)

Sets the variables of the rule (returns object), or gets it if parameter is false or not specified.

Parameters

null|array|false $variables

The variables of the rule.

Return Value

null|array|static