Download the PHP package guide42/plan without Composer
On this page you can find all versions of the php package guide42/plan. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package plan
Plan
Plan is a data validation library for PHP. It's planed to be used for validating data from external sources.
It has two core design goals:
- Simple: use language own features: construct schema from literals and function composition, errors are exceptions;
- Lightweight: lots of validations included without 3rd party libraries;
Usage
Simplest way would be requiring guide42/plan
with composer.
Concepts
Defining schemas is the first step to validate an input data. An schema is a tree of validators that can be manually defined or created based on literals and arrays.
With the schema defined, a plan must be make to validate data. This is made
through the \plan\Schema
class. This object, when called like a function,
will validate the data and return the modified (or not) data. If any error
occurs and exception will be thrown.
Schema information will be always be trusted, therefore will not be validate. Contrary input data will be never be trusted.
Literals
Scalars are treated as literals that are matched using the identity operator.
As any plan validator it throws an \plan\Invalid
when fails.
Arrays
Plan will distinguish between indexed and associative arrays. If an array has all indexes numeric and sequential will be considerer a sequence. If not will be considerer a dictionary.
Sequences
A sequence will be treated as a list of possible valid values. Will require that the input data is sequence that contains one or more elements of the schema. Elements can be repeated.
An array of possible values:
An empty array will be a sequence that accept any value.
Dictionaries
A dictionary will be used to validate structures. Each key in data will be checked with the validator of the same key in the schema.
Validators
All core validators live in \plan\assert
namespace.
literal
See Literals.
type
Will validate the type of data. The data type will be not casted.
Aliases of this validator are: bool
, int
, float
, str
.
scalar
Wrapper around is_scalar
function.
instance
Wrapper around instanceof
type operator.
iterable
Given data must be an array or implement Iterable
interface.
required
Rejected values are null
and ''
.
optional
Accepts null
and empty string without calling the given schema.
seq
See Sequences.
This is normally accepted as "a list of something (or something else)".
- A list of email?
new Schema([v\email()])
. - A list of people, but some of them are in text and some as a dictionary?
dict
See Dictionaries.
Elements of the dictionary not found in data will be called with null
; any
additional data key will throw an exception.
The validator dict
accept two more parameters to change this behavior.
Both parameters (required
and extra
) could be arrays, so only the given
keys will be taken in account.
If the extra
parameter is a dictionary it will be compiled and treat it as
a validator for each extra key.
There is no way of treat all items with the same validator. Nor having a default validator for extra keys.
keys
Is also possible to validate and/or filter the list of keys of a dictionary.
object
The structure of an object can also be validated.
any
Accept any of the given list of validators, as a valid value.
This is useful when you only need one choice a of set of values. If you need any quantity of choices use a sequence instead.
all
Require all validators to be valid.
not
Negative the given validator.
iif
Simple conditional.
length
The given data length is between some minimum and maximum value. This works
with strings using strlen
or count
for everything else.
validate
A wrapper for validate filters using filter_var
.
It accepts the name of the filter.
Aliases are: url
, email
, ip
.
And the "like-type": boolval
, intval
, floatval
. Note that this will check
that a string resemble to a boolean/int/float; for checking if the input data
IS a boolean/int/float use the type
validator. None of this will
modify the input data.
datetime
Validates if given datetime in string can be parsed by given format.
match
Value must be a string that matches the regular expression.
Filters
The input data can also be filtered, and the validation will return the modified data. By convention is called validator when it will not modified the input data; and filter when modification to the data are performed.
Core filters will be found in the \plan\filter
namespace.
type
Will cast the data into the given type.
Note that boolval
, strval
, intval
, floatval
are not aliases of this
filter but wrappers of the homonymous functions.
sanitize
Sanitization filters.
Aliases are: url
, email
.
datetime
Will parse a datetime formated string into a \DateTimeImmutable
object.
String Filters
All string transformations fall under the \plan\filter\str
namespace.
nullempty
Change empty strings into nulls.
strip
Wrapper of trim
function.
Internationalization
This library supports some filters to be language dependant. Before using any
of them make sure that the correct locale is set (ex. by using setlocale
).
chars
Will keep only characters in the current language and numbers. Optionally white-space could be keeped too.
Aliases are: alpha
, alnum
.
Writing Validators
A simple callable can be a validator.
Any validation error is thrown with the Invalid
exception. If several errors
must be reported, MultipleInvalid
is an exception that could contain multiple
exceptions. All other exceptions are considerer as errors in the validator.
Acknowledgments
This library is heavily inspired in Voluptuous by Alec Thomas.