1. Go to this page and download the library: Download gobline/filter 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/ */
gobline / filter example snippets
(new Gobline\Filter\Sanitizer\LTrim('/'))->sanitize('/some/path/'); // returns "some/path/"
(new Gobline\Filter\Sanitizer\Cast('int'))->sanitize('42'); // returns integer 42
(new Gobline\Filter\Validator\Email())->isValid('[email protected]') // returns true
(new Gobline\Filter\Validator\Length(5))->isValid('foo') // returns false
(new Gobline\Filter\Validator\Int())->isValid('foo') // returns false, "foo" is not an integer
(new Gobline\Filter\Validator\Int())->isValid(42) // returns true
$validator = new Gobline\Filter\Validator\Int();
if (!$validator->isValid("foo")) {
echo $validator->getMessage(); // prints "The input is not a valid number"
}
$validator = new Gobline\Filter\Validator\Int();
$validator->setMessageTemplate('%value% is not a valid number');
if (!$validator->isValid("foo")) {
echo $validator->getMessage(); // prints "foo is not a valid number"
}
$age = (new Gobline\Filter\FilterFunnel())
->filter($age, 'optional|trim|int|between(1,110)|cast(int)');
$map = new FilterClassMap();
$map->addValidator('foo', 'My\\Filter\\Validator\\FooValidator');
$factory = new FilterFunnelFactory($map);
$funnel = $factory->createFunnel();
$age = (new Gobline\Filter\FilterFunnel())
->addSanitizer('trim')
->addValidator('int')
->setMessageTemplate('"%value%" is not a valid number')
->addValidator('between', 1, 110)
->setMessageTemplate('%value% must be a number between %min% and %max%')
->addSanitizer('cast', 'int')
->filter($age);
$objectFilter = new ObjectFilter();
$person = $objectFilter->filter(new Person($inputName, $inputAge, $inputGender));
if ($objectFilter->hasMessages()) {
// ...
}
class Person implements FilterableInterface
{
private $name;
private $age;
private $gender;
private $deceased;
private $email;
public function __construct($name, $age, $gender, $deceased = false)
{
$this->name = $name;
$this->age = $age;
$this->gender = $gender;
$this->deceased = $deceased;
}
public function getName() { return $this->name; }
public function setName($name) { $this->name = $name; }
public function getAge() { return $this->age; }
public function setAge($age) { $this->age = $age; }
public function getGender() { return $this->gender; }
public function setGender($gender) { $this->gender = $gender; }
public function isDeceased() { return $this->deceased; }
public function setDeceased($deceased) { $this->deceased = $deceased; }
public function getEmail() { return $this->email; }
public function setEmail($email) { $this->email = $email; }
public function getRules()
{
return [
'name' => 'trim|alpha|length(2,50)',
'age' => 'trim|int|between(0,110)|cast(int)',
'gender' => 'trim|value(M,F)',
'deceased' => 'trim|boolean|cast(boolean)',
'email' => 'optional|trim|email',
];
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.