Download the PHP package hanneskod/clean without Composer
On this page you can find all versions of the php package hanneskod/clean. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hanneskod/clean
More information about hanneskod/clean
Files in hanneskod/clean
Package clean
Short Description A clean (as in simple) data cleaner (as in validation tool)
License WTFPL
Homepage https://github.com/hanneskod/clean
Informations about the package clean
hanneskod/clean
A clean (as in simple) data cleaner (as in validation tool)
Why?
Sometimes it's necessary to perform complex input validation, and a number of tools exist for this purpose (think Respect\Validation). At other times (arguably most times) built in php functions such as the ctype-family and regular expressions are simply good enough. At these times pulling in a heavy validation library to perform basic tasks can be unnecessarily complex.
Clean acts as a thin wrapper around callables and native php functions, in less than 100 logical lines of code, and allows you to filter and validate user input through a simple and compact fluent interface.
Installation
Clean requires php 7.4 or later and has no userland dependencies.
Usage
Basic usage consists of grouping a set of Rules in an ArrayValidator.
Defining rules
Rules are defined using the pre()
, match()
and post()
methods.
pre()
takes any number ofcallable
arguments to act as pre-match filters. Filters take an argument and return it in it's filtered state.post()
takes any number ofcallable
arguments to act as post-match filters. Filters take an argument and return it in it's filtered state.match()
takes any number ofcallable
arguments to act as validators. The callable should take an argument and return true if the argument is valid and false if it is not.
A rule definition might look like this:
Using the regexp matcher
The Rule
validator comes with one special case matcher: regexp()
to match
string input against a posix style regular expression (preg_match()
).
Making input optional
Rules may define a default value using the def()
method. The default value is
used as a replacement for null
. This effectively makes the field optional in
an ArrayValidator setting.
Specifying custom exception messages
When validation fails an exception is thrown with a generic message describing
the error. Each rule may define a custom exception message using the msg()
method to fine tune this behaviour.
Ignoring unknown input items
By default unkown intput items triggers exceptions.
Use ignoreUnknown()
to switch this functionality off.
Nesting validators
ArrayValidators can be nested as follows:
Inspecting validation results using applyTo()
The validate()
method throws an exception as soon as a match fails. This may
of course not always be what you want. You can inspect the validation result
directly by using the applyTo()
method instead.
Catching all of the failures
Individual errors can be accessed using the result object.