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: *
- dev-master
- v2.07.05
- v2.07.04
- v2.07.03
- v2.07.02
- v2.07.01
- v2.07.00
- v2.06.02
- v2.06.01
- v2.06.00
- v2.05.03
- v2.05.02
- v2.05.01
- v2.05.00
- v2.4.2
- v2.4.1
- v2.4.0
- v2.3.3
- v2.3.2
- v2.3.1
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.1.0
- v1.0.1
- v1.0.0
- dev-berlin-62361-php-8punkt3
- dev-ticket38145-DNS-caching
This package is auto-updated.
Last update: 2025-07-29 02:17:30 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
*/