bupy7/php-input-filter

A simple and powerful input filter for any PHP application

1.0.2 2020-02-11 16:20 UTC

This package is auto-updated.

Last update: 2021-10-11 19:56:51 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License Build Status Coverage Status

A simple and powerful input filter for any PHP application. It's like a form, but not the same. ;)

Installation

The preferred way to install this extension is through composer:

$ composer require bupy7/php-input-filter "*"

or add

"bupy7/php-input-filter": "*"

to the require section of your composer.json file.

Usage

Form:

// module/Application/src/Form/SignInForm.php

use Bupy7\InputFilter\FormAbstract;

class SignInForm extends FormAbstract
{
    /**
     * @var string 
     */
    public $email;
    /**
     * @var string 
     */
    public $password;

    protected function inputs()
    {
        return [
            [
                'name' => 'email',
                'required' => true,
                'validators' => [
                    [
                        'name' => 'EmailAddress',
                    ],
                ],
            ],
            [
                'name' => 'password',
                'required' => true,
            ],
        ];
    }
}

Action:

// module/Application/src/Action/AuthAction.php

use Application/Form/SignInForm;

$signInForm = new SignInForm();
if ($this->getRequest()->isPost()) {
    $signInForm->setValues($this->getRequest()->getPost());
    if ($signInForm->isValid()) {
        // authentication...
        // $auth->setLogin($signInForm->email)
        // $auth->setPassword($signInForm->password);
        // $result = $auth->authenticate();
        if ($result->isValid()) {
            // some actions
        }
    }
}

// to do something next

Using scenarios

By default using FormAbstract::DEFAULT_SCENARIO but you can use your customs one:

// module/Application/src/Form/SignInForm.php

use Bupy7\InputFilter\FormAbstract;

class SignInForm extends FormAbstract
{
    const SCENARIO_PASSWORD = 'password';

    /**
     * @var string
     */
    public $email;
    /**
     * @var string
     */
    public $password;

    protected function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios[self::SCENARIO_PASSWORD] = [
            'password',
        ];
        return $scenarios;
    }

    protected function inputs()
    {
        return [
            [
                'name' => 'email',
                'required' => true,
                'validators' => [
                    [
                        'name' => 'EmailAddress',
                    ],
                ],
            ],
            [
                'name' => 'password',
                'required' => true,
                'filters' => [
                    [
                        'name' => 'StringTrim',
                    ]
                ],
            ],
        ];
    }
}

Action:

// DEFAULT scenario
$signInForm = new SignInForm();
$signInForm->email = 'test@gmail.com';
$signInForm->password = '12q34e56t78';
if ($signInForm->isValid()) {
    // do something
}

// or PASSWORD scenario
$signInForm = new SignInForm();
$signInForm->setScenario(SignInForm::SCENARIO_PASSWORD);
$signInForm->password = '12q34e56t78';
if ($signInForm->isValid()) {
    // do something
}

Links

The php-input-filter was based on Zend Input Filter, Zend Validator and Zend Filters.

License

php-input-filter is released under the BSD 3-Clause License.