Create your own PHP framework from scratch - Chapter 5

target

Implementing parameter verification similar to ThinkPHP framework

Automatic calibration of parameters

Create a new file Validate.php under thecorelib folder
The code is as follows:

<?php

namespace core\lib;

class Validate
{
    //Parametric Verification Rules
    protected $rule = [];
    //Information returned from parameter validation failure
    protected $message = [];
    //Validation failure, error message
    public $error = [];

    public function check($input, $rule = [], $message = [])
    {
        if (empty($rule)) {
            // Read validation rules
            $rule = $this->rule;
        }
        if (empty($message)) {
            // Read error message
            $message = $this->message;
        }
        foreach ($rule as $field => $rules) {
            if (!isset($input[$field])) {
                continue;
            }
            $ruleList = explode('|', $rules);
            foreach ($ruleList as $rule) {
                $param = null;
                if (strstr($rule, ':') !== false) {
                    $pramas = explode(':', $rule);
                    $rule = $pramas[0];
                    $param = $pramas[1];
                }
                $method = $rule;
                if (!$this->$method($input[$field], $param)) {
                    $this->error = $message[$field];
                    return false;
                }
            }
        }
        return true;
    }

    function require $field
    {
        if (isset($field) && ($field === false || $field === 0 || $field === 0.0 || $field === '0' || !empty($field))) {
            return true;
        } else {
            return false;
        }
    }

    public static function tel($field)
    {
        if (preg_match("/^1[345678]{1}\d{9}$/", $field)) {
            return true;
        } else {
            return false;
        }
    }

    public static function email($field)
    {
        if (preg_match("/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/", $field)) {
            return true;
        } else {
            return false;
        }
    }

    public static function max($field, $param = null)
    {
        if (function_exists('mb_strlen')) {
            if (mb_strlen($field) <= (int) $param) {
                return true;
            } else {
                return false;
            }
        } else {
            if (strlen($field) <= (int) $param) {
                return true;
            } else {
                return false;
            }
        }
    }

    public static function min($field, $param = null)
    {
        if (function_exists('mb_strlen')) {
            if (mb_strlen($field) >= (int) $param) {
                return true;
            } else {
                return false;
            }
        } else {
            if (strlen($field) >= (int) $param) {
                return true;
            } else {
                return false;
            }
        }
    }

}

Use

Before using it, create a helper function input to get get and post parameters, and add code in core common function. php:

<?php
//Here you can write helper functions
function input()
{
    return array_merge($_GET, $_POST);
}

Then create a new folder validate under the app directory, and a new base.php base class, inheriting validate, the code is as follows:

<?php

namespace app\validate;

use core\lib\Validate;

class Base extends Validate
{
    public function goCheck()
    {
        if (!$this->check(input())) {
            throw new \Exception($this->error);
        }
    }
}

In the validate directory, create a new user.php file, inherit the base class, the code is as follows:

<?php
namespace app\validate;

class user extends Base
{
    protected $rule = [
        'username' => 'max:4',
        'password' => 'min:6',
    ];
    protected $message = [
        'username' => 'User Name Length Over Limit',
        'password' => 'Password length should not be less than 6',
    ];
}

Then in the user.php file of the ctrl directory, the code is as follows:

<?php

namespace app\ctrl;

use app\validate\user as userValidate;

class user
{
    public function get()
    {
        (new userValidate)->goCheck();
        //...todo
    }
}

test

Add routing in / config/route.php:

Route::post('/user', 'user/get');

Open postman software for testing

1. When the user name exceeds 4 digits

2. When the password is less than 6 bits


Download source code

Tags: PHP less

Posted on Mon, 07 Oct 2019 16:32:45 -0700 by pwicks