1. Go to this page and download the library: Download hanneskod/clean library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hanneskod / clean example snippets
use hanneskod\clean\ArrayValidator;
use hanneskod\clean\Rule;
$validator = new ArrayValidator([
'foo' => (new Rule)->match('ctype_digit'),
'bar' => (new Rule)->match('ctype_alpha'),
]);
$tainted = [
'foo' => 'not-valid only digits allowed',
'bar' => 'valid'
];
try {
$validator->validate($tainted);
} catch (Exception $e) {
echo $e->getMessage();
}
use hanneskod\clean\Rule;
$rule = (new Rule)->pre('trim')->match('ctype_alpha')->post('strtoupper');
// outputs FOOBAR
echo $rule->validate(' foobar ');
use hanneskod\clean\Rule;
$rule = (new Rule)->regexp('/A/');
// outputs ABC
echo $rule->validate('ABC');
use hanneskod\clean\ArrayValidator;
use hanneskod\clean\Rule;
$validator = new ArrayValidator([
'key' => (new Rule)->def('baz')
]);
$data = $validator->validate([]);
// outputs baz
echo $data['key'];
use hanneskod\clean\ArrayValidator;
$validator = new ArrayValidator([]);
// throws a clean\Exception as key is not present in validator
$validator->validate(['this-key-is-not-definied' => '']);
use hanneskod\clean\ArrayValidator;
use hanneskod\clean\Rule;
$validator = new ArrayValidator([
'nested' => new ArrayValidator([
'foo' => new Rule
])
]);
$tainted = [
'nested' => [
'foo' => 'bar'
]
];
$clean = $validator->validate($tainted);
//outputs bar
echo $clean['nested']['foo'];
use hanneskod\clean\Rule;
$rule = (new Rule)->match('ctype_digit');
$result = $rule->applyTo('12345');
$result->isValid() == true;
// outputs 12345
echo $result->getValidData();
use hanneskod\clean\ArrayValidator;
use hanneskod\clean\Rule;
$validator = new ArrayValidator([
'1' => (new Rule)->match('ctype_digit')->msg('failure 1'),
'2' => (new Rule)->match('ctype_digit')->msg('failure 2'),
]);
// Both 1 and 2 will fail as they are not numerical
$result = $validator->applyTo(['1' => '', '2' => '']);
//outputs failure 1failure 2
foreach ($result->getErrors() as $errorMsg) {
echo $errorMsg;
}
use hanneskod\clean\ArrayValidator;
use hanneskod\clean\Rule;
$validator = new ArrayValidator([
'foo' => (new Rule)->match('ctype_digit'),
'bar' => (new Rule)->match('ctype_digit'),
]);
$result = $validator->applyTo([
'foo' => 'not-valid',
'bar' => '12345'
]);
// outputs foo
echo implode(array_keys($result->getErrors()));
use hanneskod\clean\AbstractValidator;
use hanneskod\clean\Rule;
use hanneskod\clean\ValidatorInterface;
class NumberRule extends AbstractValidator
{
protected function create(): ValidatorInterface
{
return (new Rule)->match('ctype_digit');
}
}
// Outputs 1234
echo (new NumberRule)->validate('1234');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.