Download the PHP package alleyinteractive/laminas-validator-extensions without Composer
On this page you can find all versions of the php package alleyinteractive/laminas-validator-extensions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alleyinteractive/laminas-validator-extensions
More information about alleyinteractive/laminas-validator-extensions
Files in alleyinteractive/laminas-validator-extensions
Package laminas-validator-extensions
Short Description Additional validation classes for the laminas-validator framework.
License GPL-2.0-or-later
Informations about the package laminas-validator-extensions
Laminas Validator Extensions
This package provides additional validation classes for the Laminas Validator framework, plus a custom base validation class.
Installation
Install the latest version with:
Basic usage
For more information about what validators do, how to use them, and how to write your own, visit the Laminas documentation.
Base validators
ExtendedAbstractValidator
The abstract Alley\Validator\ExtendedAbstractValidator
class standardizes the implementation of custom validators with \Laminas\Validator\AbstractValidator
.
When extending ExtendedAbstractValidator
, validation logic goes into a new testValue()
method, which is responsible only for applying the logic and adding any validation errors. It's no longer necessary to call setValue()
prior to evaluating the input, and isValid()
will return true
if there are no error messages after evaluating the input and false
if there are any messages.
Before:
After:
FreeformValidator
The standalone, abstract Alley\Validator\FreeformValidator
class leaves most of the implementation details to your discretion, but it's often easier to use for validators that are project-specific or not ready for wider distribution.
Like the ExtendedAbstractValidator
class, the FreeformValidator
expects that validation logic goes into a testValue()
method, and isValid()
will return true
or false
based on whether there are error messages.
Validation errors can be added using the error()
method, which accepts the message key and text.
"Any Validator" chains
\Alley\Validator\AnyValidator
is like a Laminas validator chain except that it connects the validators with "OR," marking input as valid as soon it passes one of the given validators.
Unlike a Laminas validator chain, validators can only be attached, not prepended, and there is no $priority
argument.
Basic usage
"Fast fail" validator chains
\Alley\Validator\FastFailValidatorChain
is like a Laminas validator chain except that if a validator fails, the chain will automatically be broken; there is no $breakChainOnFailure
parameter.
Unlike a Laminas validator chain, validators can only be attached, not prepended, and there is no $priority
argument.
Basic usage
Validators by operator name
\Alley\Validator\ValidatorByOperator
allows you to access a validator using a readable operator name, such as REGEX
or NOT IN
.
Its primary use case is to allow you to write functions that accept the readable operator names as parameters while using validators internally. Here's a demonstrative function call from the wp-match-blocks library:
The supported operator names are:
CONTAINS
andNOT CONTAINS
, which forward to\Alley\Validator\ContainsString
using a case-sensitive search.IN
andNOT IN
, which forward to\Alley\Validator\OneOf
.LIKE
andNOT LIKE
, which forward to\Alley\Validator\ContainsString
using a case-insensitive search.REGEX
andNOT REGEX
, which forward to\Laminas\Validator\Regex
.===
,!==
, and the other operators supported by\Alley\Validator\Comparison
.
Any operator name that isn't forwarded to a different validator must be a valid Comparison
operator.
Basic usage
Validators
AlwaysValid
\Alley\Validator\AlwaysValid
marks all input as valid. It can be used to satisfy type requirements when full validation needs to be disabled or is impractical.
Supported options
None.
Basic usage
Comparison
\Alley\Validator\Comparison
compares input to another value using a PHP comparison operator. The input passes validation if the comparison is true. Input is placed on the left side of the operator.
Supported options
The following options are supported for \Alley\Validator\Comparison
:
compared
: The value the inputs are compared to. It is placed on the right side of the operator.operator
: The PHP comparison operator used to compare the input andcompared
.
Basic usage
ContainsString
\Alley\Validator\ContainsString
is a validator around the str_contains()
function. Each instance of the validator represents the "needle" string and validates whether the string is found within the input "haystack." Inputs will automatically be cast to strings.
Supported options
The following options are supported for \Alley\Validator\ContainsString
:
needle
: The string or instance of\Stringable
the inputs are searched for. It will automatically be cast to a string at the time of validation.ignoreCase
: Whether to perform a case-insensitive search. False by default.
Basic usage
DivisibleBy
\Alley\Validator\DivisibleBy
allows you to validate whether the input is evenly divisible by a given numeric value. Inputs will automatically be cast to integers.
Supported options
The following options are supported for \Alley\Validator\DivisibleBy
:
divisor
: The value the inputs are divided by. It will automatically be cast to an integer.
Basic usage
Not
Alley\Validator\Not
inverts the validity of a given validator. It allows for creating validators that test whether input is, for example, "not one of" in addition to "one of."
Supported options
None.
Basic usage
OneOf
Alley\Validator\OneOf
validates whether an array of scalar values contains the input.
OneOf
is a simpler version of \Laminas\Validator\InArray
that accepts only scalar values in the haystack and does only strict comparisons. In return, it produces a friendlier error message that lists the allowed values.
Supported options
The following options are supported for \Alley\Validator\OneOf
:
haystack
: The array to be searched for the input.
Basic Usage
Type
\Alley\Validator\Type
allows you to validate whether the input is of the given PHP type. The input passes if it is the expected type.
This validator is inspired by PHPUnit's \PHPUnit\Framework\Constraint\IsType
class.
Supported options
The following options are supported for \Alley\Validator\Type
:
type
: The expected PHP type. Supported types arearray
,bool
,boolean
,callable
,double
,float
,int
,integer
,iterable
,null
,numeric
,object
,real
,resource
,string
, andscalar
.
Basic usage
WithMessage
Alley\Validator\WithMessage
allows you to decorate a validator with a custom failure code and message, replacing the validator's usual failure messages.
Supported options
None.