eappointment / mellon
Validator for parameters and validation helper
Requires
- php: >=7.3.0
- ext-json: >=0
- ext-pcre: >=0
- psr/http-message: >=0
Requires (Dev)
- phpmd/phpmd: ^2.8.0
- phpunit/phpunit: ^9.5.4
- squizlabs/php_codesniffer: *
This package is auto-updated.
Last update: 2024-10-29 08:22:01 UTC
README
Link to main repository: https://gitlab.com/eappointment/mellon/
Mellon is a validator library using the filter-functions offered by PHP. It is written for PHP micro frameworks which do not have a validation library by themself.
'Speak, friend, and enter'
Usage
In flat PHP, a simple validation would look like this:
$name = filter_input(INPUT_GET, 'name', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
Using Mellon, it looks like this:
$validator = new \BO\Mellon\Validator($_GET);
$name = $validator->getParameter('name')->isString()->getValue();
With a shortcut for all $_REQUEST variables, you could use:
use \BO\Mellon\Validator;
$name = Validator::param('name')
->isString()
->isSmallerThan(32)
->setDefault('John Doe')
->getValue();
To ensure, that nobody uses the superglobals in your project, take a look at the controversial rule in the PHP MD Config File.
Error Messages
Mellon allow to add error messages for custom validations:
$size = Validator::param('size')
->isMatchOf('/(px|em|%)/', 'Do include some valid units like px, em or % for a size')
->isFreeOf('/important/', 'The css statement "!important" is not allowed')
->isBiggerThan(2, 'The size should contain some value')
->isSmallerThan(10, 'You should enter a size, no short story expected')
->setDefault('100%');
if ($size->hasFailed()) {
throw new Exception("Error in size param: " . implode(';', $size->getMessages()));
}
else {
echo $size->getValue();
}
For the usage of validation in template engines, mellon support an output as array:
$sizeParam = $size->getStatus();
/**
* Contains the following keys:
* failed - True if validation has failed
* messages - A list of error messages in case the validation has failed
* value - Value, might be the default value if validation has failed
*/