PHP code example of mapolun / easyswoole-normal-validate

1. Go to this page and download the library: Download mapolun/easyswoole-normal-validate 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/ */

    

mapolun / easyswoole-normal-validate example snippets



使用规则借鉴tp5规则语法,EasySwoole验证调用方法,验证字段规则值时用~ 符号隔开,可拓展func类型标识自定义函数进行验证规则。

一、创建validate规则

namespace App\Validate;

use MaPoLun\Validate;

/**
 * 登录验证类
 * Class SignIn
 * @package App\Validate\User
 */
class SignIn extends Validate
{
    protected $rule = [
        'username' => '控制器类

namespace App\HttpController;

use App\Validate\SignIn;

class Index extends Base
{
    public function index()
    {
        // TODO: Implement index() method.
        $validate = (new SignIn())->getExamplie();
        $params = $this->validateCheck($validate);
        var_dump($params); // 这是你客户端请求过来验证好的参数
    }
}

三、创建Base类

namespace App\HttpController;

use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\Http\Message\Status;
use EasySwoole\Validate\Validate;
use Swoole\Coroutine\Http\Client\Exception;

class Base extends Controller
{
    public function index()
    {
        // TODO: Implement index() method.
    }

    public final function validateCheck($validate) : array
    {
        if ($validate instanceof Validate) {
            if ($this->validate($validate)) {
                return $validate->getVerifiedData();
            } else {
                throw new Exception($validate->getError()->__toString(),Status::CODE_ACCEPTED);
            }
        } else {
            throw new Exception("不属于规则合法验证实例",Status::CODE_INTERNAL_SERVER_ERROR);
        }
    }

    protected function onException(\Throwable $throwable): void
    {
        // TODO: Change the autogenerated stub
        $this->writeJson($throwable->getCode(),'fail',$throwable->getMessage());
        return;
    }
}