PHP code example of gobline / filter

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"
}

Gobline\Filter\Validator\AbstractValidator::setDefaultTranslator($translator);

$validator->setTranslator($translator);

$funnel = (new Gobline\Filter\FilterFunnel())
    ->addSanitizer(new Sanitizer\Trim())
    ->addValidator(new Validator\Int())
    ->addValidator(new Validator\Between(0, 110))
    ->addSanitizer(new Sanitizer\Cast('int'));

$funnel->filter(30); // returns 30

$funnel->filter("foo"); // returns null
echo $validator->getMessage(); // prints "The input is not a valid number"

$age = (new Gobline\Filter\FilterFunnel())
    ->addSanitizer('trim')
    ->addValidator('int')
    ->addValidator('between', 1, 110)
    ->addSanitizer('cast', 'int')
    ->filter($age);

$age = (new Gobline\Filter\FilterFunnel())
    ->add('trim')
    ->add('int')
    ->add('between', 1, 110)
    ->add('cast', 'int')
    ->filter($age);

$age = (new Gobline\Filter\FilterFunnel())
    ->trim
    ->int
    ->between(1, 110)
    ->cast('int')
    ->filter($age);

$age = (new Gobline\Filter\FilterFunnel())
    ->filter($age, 'trim|int|between(1,110)|cast(int)');

$age = (new Gobline\Filter\FilterFunnel())
    ->setOptional()
    ->addSanitizer('trim')
    ->addValidator('int')
    ->addValidator('between', 1, 110)
    ->addSanitizer('cast', 'int')
    ->filter($age);

$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',
        ];
    }
}