expresspaygh/exp-refine

一个具有自定义验证功能的请求过滤和清理包

1.0.3 2021-02-09 21:22 UTC

This package is not auto-updated.

Last update: 2024-10-02 14:38:10 UTC


README

  1. [Expresspay Refines]
    1. [基本用法]
    2. [自定义字段类型]
    3. [编写自定义规则]

Expresspay Refine

一个简单的服务器端请求过滤器、清理器和验证器

基本用法

`Expay\Refine\Filter` 执行大部分工作。大多数对类的方法调用将仅使用 `addField` 和 `check` 方法。

字段通过请求中的键和表示其类型的值来定义。当此值为字符串时,它用于查找存储的规则列表以过滤请求值。

use Expay\Refine\Filter;
use Expay\Refine\Rules;

$result = (new Filter())
	->addField("password", "string")
	->addField("email", "email")
	->check([
	"email" => "someone@expresspaygh.com",
	"password" => "hackme"
]);

print_r($result);
Array
(
	[status] => 0
	[message] => Success
	[output] => Array
		(
			[password] => hackme
			[email] => someone@expresspaygh.com
		)

)

自定义字段类型

可以通过指定应用于它们的规则来添加自定义字段类型。这可以通过以下代码完成

use Expay\Refine\Filter;
use Expay\Refine\Rules;


$result = (new Filter())
	// add a field type called boolean_value 
	->addRule("uppercase_bool", new Rules\Boolean('upper'))
	->addRules("email", [
		new Rules\Required,
		new Rules\PHPFilter([
			'filter' => FILTER_VALIDATE_EMAIL | FILTER_SANITIZE_EMAIL
		])])
	->addField("is_admin", "uppercase_bool")
	->addField("email", "email")
	->check([
		"is_admin" => "true",
		"email" => "admin@example.com"
	]);

print_r($result);
Array
(
	[status] => 0
	[message] => Success
	[output] => Array
		(
			[is_admin] => TRUE
			[email] => admin@example.com
		)

)

可以使用工厂函数来避免重复定义。

use Expay\Refine\Filter;
use Expay\Refine\Rules;

function filter() {
	return (new Filter())
	// add a field type called boolean_value 
	->addRule("uppercase_bool", new Rules\Boolean('upper'))
	->addRules("email", [
		new Rules\Required,
		new Rules\PHPFilter([
			'filter' => FILTER_VALIDATE_EMAIL | FILTER_SANITIZE_EMAIL
	])]);
}

print_r(filter()->check([
	"is_admin" => "true",
	"email" => "admin@example.com"
]));
Array
(
	[status] => 0
	[message] => Success
	[output] => Array
		(
			[email] => admin@example.com
		)

)

编写自定义规则

可以通过子类化 \Expay\Refine\Rules\Rule 并实现 apply 方法来编写自定义规则。

use Expay\Refine\Filter;
use Expay\Refine\Rules\Rule;
use Expay\Refine\Exceptions\InvalidField;

class CVV extends Rule
{
	public function apply($value, string $key, array $request): string
	{
		if (preg_match("/^\d\d\d$/", $value))
			return $value;
		throw new InvalidField("Invalid cvv");
	}
}

$result = (new Filter())
	->addRule("cvv", new CVV)
	->check(["cvv" => "123"]);

var_dump($result);
array(3) {
  'status' =>
  int(0)
  'message' =>
  string(7) "Success"
  'output' =>
  array(1) {
    'cvv' =>
    string(3) "123"
  }
}

编写验证

可以通过添加过滤器规则和验证规则来编写验证。

访问此 GitHub 仓库 Rakit/Validation 以获取更多可用的验证规则选项。

use Expay\Refine\Rules;
use Expay\Refine\Filter;
use Expay\Refine\Exceptions\ValidationError;

try
{
	$filter=new Filter;

	$vRules=["email"=>"required|present|email"];
	$fields=["email"=>[new Rules\Validate($vRules),new Rules\CleanTags]];
	$data=["email"=>"info@expresspaygh.com"];

	$result=$filter->addFields($fields)->check($data);
}
catch(ValidationError $e)
{
	$result=$e->getMessage();
}

var_dump($result);
array(3) {
  ["status"]=>
  int(0)
  ["message"]=>
  string(7) "Success"
  ["output"]=>
  array(1) {
    ["email"]=>
    string(19) "info@expresspaygh.com"
  }
}

编写自定义验证规则

可以通过添加过滤器规则,然后添加验证规则作为第一个参数,并将包含您的自定义验证规则名称及其相应类别的键值对作为第二个参数传递给过滤器规则来编写自定义验证规则。

访问此 GitHub 仓库 Rakit/Validation/Override/Rules 以获取更多自定义验证规则选项。

use Expay\Refine\Rules;
use Expay\Refine\Filter;
use Expay\Refine\Exceptions\ValidationError;
use Rakit\Validation\Rule as ValidationRule;

/**
 * ValidationRuleObjectProvider
 */
class ValidationRuleObjectProvider extends ValidationRule
{    
  /**
   * message
   *
   * @var string
   */
  protected $message = "";
    
  /**
   * __construct
   *
   * @return void
   */
  public function __construct()
  {
    $this->message=":value is not a valid object";
  }
    
  /**
   * check
   *
   * @param  mixed $value
   * @return bool
   */
  public function check($value) : bool
  {
    return is_object($value);
  }
}

try
{
	$filter=new Filter;

	$vRules=['randomObj'=>'required|object_value'];
	$customRules=["object_value"=>new ValidationRuleObjectProvider];

	$fields=["obj_field"=>[new Rules\Validate($vRules,$customRules)]];

	$objData=new \stdClass();
	$objData->{"hello"}="hello sir";
	$data=["obj_field"=>$objData];

	$result=$filter->addFields($fields)->check($data);
}
catch(ValidationError $e)
{
	$result=$e->getMessage();
}

var_dump($result);
array(3) {
  ["status"]=>
  int(0)
  ["message"]=>
  string(7) "Success"
  ["output"]=>
  array(1) {
    ["obj_field"]=>
    object(stdClass)#900 (1) {
      ["hello"]=>
      string(9) "hello sir"
    }
  }
}