webhkp/pvalidate

PHP的简单灵活验证器

v0.5.1 2024-07-29 12:31 UTC

This package is auto-updated.

Last update: 2024-09-29 13:04:09 UTC


README

Pvalidate

Latest Release Build Status Last Commit

Pvalidate 是一个健壮且灵活的 PHP 验证库。它旨在简化数据验证过程,确保数据完整性,并在 PHP 应用程序中实施业务规则。

无论您是在构建小型应用程序还是大型企业系统,Pvalidate 都提供了您所需的有效且高效地验证数据的工具。

在此处查看完整文档

在 packagist.org 上查看包

安装

composer require webhkp/pvalidate

用法

<?php

require_once "vendor/autoload.php";


use Webhkp\Pvalidate\Rules\Regex;
use Webhkp\Pvalidate\Rules\ValidationRule;
use Webhkp\Pvalidate\Validator;
use Webhkp\Pvalidate\Rules\Required;
use Webhkp\Pvalidate\Rules\Range;
use Webhkp\Pvalidate\Rules\Allow;
use Webhkp\Pvalidate\Rules\Disallow;


class MyClass {
    public function __construct(
        #[Required]
        public string $name
    ) {

    }

    #[Required]
    public string $address;

    #[Required]
    public string $description;

    #[Range(min: 50, max: 60)]
    const AGE = 40;

    #[Range(min: 50)]
    public int $prop1 = 40;

    #[Range(max: 10)]
    #[Disallow([1, 2, 3, 40])]
    public int $prop2 = 40;

    #[Range()]
    #[Allow([1, 2, 3, 40])]
    public int $prop3 = 40;

    #[Required]
    public array $myArr = [];

    #[Regex('/[A-Z0-9]+/')]
    public string $regexTestField = 'AB23DE';
}


// Usage
$myObj = new MyClass("Test ABC");
$myObj->description = "Some desc string for testing";

$validationResponse = Validator::validate($myObj);

var_dump($validationResponse->isValid());
var_dump($validationResponse->getErrors());
//var_dump($validationResponse->getResult());

var_dump($validationResponse->getMessages());

输出

bool(false)


array(5) {
  ["address"]=>
  array(3) {
    ["value"]=>
    NULL
    ["valid"]=>
    bool(false)
    ["errors"]=>
    array(1) {
      ["required"]=>
      string(25) "address field is Required"
    }
  }
  ["prop1"]=>
  array(3) {
    ["value"]=>
    int(40)
    ["valid"]=>
    bool(false)
    ["errors"]=>
    array(1) {
      ["min"]=>
      string(42) "prop1 should be larger than or equal to 50"
    }
  }
  ["prop2"]=>
  array(3) {
    ["value"]=>
    int(40)
    ["valid"]=>
    bool(false)
    ["errors"]=>
    array(1) {
      ["disallowed"]=>
      string(53) "prop2 should not be in the disallowed list (1,2,3,40)"
    }
  }
  ["myArr"]=>
  array(3) {
    ["value"]=>
    array(0) {
    }
    ["valid"]=>
    bool(false)
    ["errors"]=>
    array(1) {
      ["required"]=>
      string(23) "myArr field is Required"
    }
  }
}

array(5) {
  [0]=>
  string(25) "address field is Required"
  [1]=>
  string(42) "prop1 should be larger than or equal to 50"
  [2]=>
  string(53) "prop2 should not be in the disallowed list (1,2,3,40)"
  [3]=>
  string(23) "myArr field is Required"
}

单独规则使用

使用 "Allow" 规则

<?php

require_once "vendor/autoload.php";

use Webhkp\Pvalidate\Rules\Allow;

// Check single data with rule only
$validation = new Allow([1, 2, 3, 4, 5, 10]);
$validationResult = $validation->safeParse(22);

var_dump($validationResult->isValid());
var_dump($validationResult->getErrors());

输出

bool(false)


array(1) {
  ["allowed"]=>
  string(45) " should be in the allowed list (1,2,3,4,5,10)"
}

使用 'Regex' 规则

<?php

require_once "vendor/autoload.php";

use Webhkp\Pvalidate\Rules\Regex;

// Regex validation
$regexRule = new Regex('/^[A-Z0-9]{3}.*/');
$regexResult = $regexRule->safeParse('D2ab');
var_dump($regexResult->isValid());
var_dump($regexResult->getErrors());

输出

bool(false)

array(1) {
  ["regex"]=>
  string(42) " should match the regex '/^[A-Z0-9]{3}.*/'"
}

解析并抛出错误

<?php

require_once "vendor/autoload.php";

use Webhkp\Pvalidate\Rules\Required;

// Check required and throw error
try {
    $requiredValidationResult = (new Required)->parse(null);

    var_dump($requiredValidationResult->isValid());
    var_dump($requiredValidationResult->getErrors());
} catch (\Exception $e) {
    var_dump($e->getMessage());
}

输出

object(Webhkp\Pvalidate\Exceptions\PvalidateException)#8 (8) {
  ["message":protected]=>
  string(18) " field is Required"
  ["string":"Exception":private]=>
  string(0) ""
  ["code":protected]=>
  int(1)
  ["file":protected]=>
  string(80) "package/pvalidate/src/Rules/ValidationRule.php"
  ["line":protected]=>
  int(72)
  ["trace":"Exception":private]=>
  array(1) {
    [0]=>
    array(5) {
      ["file"]=>
      string(43) "index.php"
      ["line"]=>
      int(126)
      ["function"]=>
      string(5) "parse"
      ["class"]=>
      string(37) "Webhkp\Pvalidate\Rules\ValidationRule"
      ["type"]=>
      string(2) "->"
    }
  }
  ["previous":"Exception":private]=>
  NULL
  ["additionalData":"Webhkp\Pvalidate\Exceptions\PvalidateException":private]=>
  NULL
}

自定义规则

<?php

require_once "vendor/autoload.php";

use Webhkp\Pvalidate\Rules\Custom;
use Webhkp\Pvalidate\Rules\Regex;
use Webhkp\Pvalidate\Rules\ValidationRule;
use Webhkp\Pvalidate\Validator;
use Webhkp\Pvalidate\Rules\Required;
use Webhkp\Pvalidate\Rules\Range;
use Webhkp\Pvalidate\Rules\Allow;
use Webhkp\Pvalidate\Rules\Disallow;


// Define your own rule attribute
#[Attribute]
class MyCustomPasswordRule extends ValidationRule {
    public function __construct(private readonly string $minLength) {

    }

    public function isValid(): bool {
        if (strlen($this->value) >= $this->minLength) {
            return false;
        }

        // Must contain one Upper case letter
        if (!preg_match('/[A-Z]/', $this->value)) {
            return false;
        }

        // Must contain a digit
        if (!preg_match('/[0-9]/', $this->value)) {
            return false;
        }

        // Must contain a special character
        if (!preg_match('/[!@#$%^&*]$/', $this->value)) {
            return false;
        }

        return true;
    }

    public function getErrors(): array {
        $errors = [];

        if (!$this->isValid()) {
            $errors['password'] = $this->name . ' is not a valid password (minimum length ' . $this->minLength . ', must contain a uppercase letter, a digit and a special character from "!@#$%^&*")';
        }

        return $errors;
    }

}

class MyClass {
    public function __construct() {

    }

    #[Required]
    public array $myArr = [];

    #[Regex('/[A-Z0-9]+/')]
    public string $regexTestField = 'AB23DE';

    #[MyCustomPasswordRule(100)]
    public string $password = 'mysimplepass';
}


// Usage
$myObj = new MyClass();

$validationResponse = Validator::validate($myObj);

var_dump($validationResponse->isValid());
var_dump($validationResponse->getErrors());
var_dump($validationResponse->getResult());

var_dump($validationResponse->getMessages());

输出

bool(false)

array(2) {
  ["myArr"]=>
  array(3) {
    ["value"]=>
    array(0) {
    }
    ["valid"]=>
    bool(false)
    ["errors"]=>
    array(1) {
      ["required"]=>
      string(23) "myArr field is Required"
    }
  }
  ["password"]=>
  array(3) {
    ["value"]=>
    string(12) "mysimplepass"
    ["valid"]=>
    bool(false)
    ["errors"]=>
    array(1) {
      ["password"]=>
      string(135) "password is not a valid password (minimum length 100, must contain a uppercase letter, a digit and a special character from "!@#$%^&*")"
    }
  }
}

验证构建器

<?php

require_once "vendor/autoload.php";

use Webhkp\Pvalidate\ValidationBuilder;

$validation = ValidationBuilder::required()->gt(11)->gte(200)->range(min: 1, max: 1000)->safeParse(11);

var_dump($validation->isValid());
var_dump($validation->getErrors());
//var_dump($validation->getMessages());
//var_dump($validation->getResult());

输出

bool(false)

array(2) {
  ["gt"]=>
  array(3) {
    ["value"]=>
    int(11)
    ["valid"]=>
    bool(false)
    ["errors"]=>
    array(1) {
      ["min"]=>
      string(26) " should be larger than  11"
    }
  }
  ["gte"]=>
  array(3) {
    ["value"]=>
    int(11)
    ["valid"]=>
    bool(false)
    ["errors"]=>
    array(1) {
      ["min"]=>
      string(38) " should be larger than or equal to 200"
    }
  }
}