class Shape extends Constraint implements ValidatesMany (View source)

Validates the shape of an array or object (traversable or not).

Properties

protected array<string,string|null> $messages

Constraint rules messages overrides.

from  Constraint
protected Strategy $strategy

Constraint strategy.

from  Constraint
protected string $validation

Constraint validation expression.

from  Constraint
protected Validator Since: 1.2.0
$validator

Constraint validator instance.

from  Constraint

Methods

__construct(array $fields, bool $traverse = false, array|null $messages = null, Strategy $strategy = Strategy::FailFast)

Shape constructor.

void
check(mixed $value = null)

Checks if the given value is valid and throws an exception if not.

static Validator
getMasterValidator()

Returns the master Validator instance that should be used in validation.

array
getMessages()

Gets constraint rules messages overrides.

Strategy
getStrategy()

Gets constraint strategy.

string
getValidation()

Gets constraint validation expression.

getValidator()

Gets constraint validator instance.

bool
isValid(mixed $value = null)

Checks if the given value is valid.

setMessages(array $messages)

Sets constraint rules messages overrides.

setStrategy(Strategy $strategy)

Sets constraint strategy.

setValidation(string $validation)

Sets constraint validation expression.

setValidator(Validator $validator)

Sets constraint validator instance.

Result
validate(mixed $value = null)

Validates the given value and returns the validation result.

Details

__construct(array $fields, bool $traverse = false, array|null $messages = null, Strategy $strategy = Strategy::FailFast)

Shape constructor.

Parameters

array $fields

The fields to validate. Nested elements can be accessed using dot notation (someKey.someNestedKey). Keys can have the wildcard * after a dot to match all nested keys.

bool $traverse

[optional] Whether to traverse the validatable data. The data this constraint is applied to will be casted to an array recursively to allow for accessing nested data. Nested objects properties (and arrays keys) can be accessed using dot notation using the $fields parameter.

array|null $messages
Strategy $strategy

void check(mixed $value = null)

Checks if the given value is valid and throws an exception if not.

Parameters

mixed $value

The value to validate.

Return Value

void

Exceptions

ValidationFailedException

final static protected Validator getMasterValidator()

Since: 1.2.0 This method used to be called `getValidator()` before `v1.2.0`.

Returns the master Validator instance that should be used in validation.

NOTE: All constraint share the same Validator. Always clone it to avoid any side effects.

FACT: The idea of master Validator is used to allow for better memory management and faster load times as the default Validator loads 200+ rules by default. Cloning will make a shallow copy resulting in reusing the same rules loaded by the master Validator.

Return Value

Validator

array getMessages()

Gets constraint rules messages overrides.

Return Value

array

Strategy getStrategy()

Gets constraint strategy.

Return Value

Strategy

string getValidation()

Gets constraint validation expression.

Return Value

string

Validator getValidator()

Since: 1.2.0

Gets constraint validator instance.

This method returns a clone of the master Validator if a Validator is not set explicitly via self::setValidator().

Return Value

Validator

bool isValid(mixed $value = null)

Checks if the given value is valid.

Parameters

mixed $value

The value to validate.

Return Value

bool

Whether the value is valid or not.

Constraint setMessages(array $messages)

Sets constraint rules messages overrides.

Parameters

array $messages

Return Value

Constraint

Constraint setStrategy(Strategy $strategy)

Sets constraint strategy.

Parameters

Strategy $strategy

Return Value

Constraint

Constraint setValidation(string $validation)

Sets constraint validation expression.

Parameters

string $validation

Return Value

Constraint

Constraint setValidator(Validator $validator)

Since: 1.2.0

Sets constraint validator instance.

Parameters

Validator $validator

Constraint validator instance.

Return Value

Constraint

Result validate(mixed $value = null)

Validates the given value and returns the validation result.

Parameters

mixed $value

The value to validate.

Return Value

Result

The validation result (single or multiple).