expresspaygh/exp-refine 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

expresspaygh/exp-refine

Composer 安装命令:

composer require expresspaygh/exp-refine

包简介

A request filtering and sanitization package with custom validations

README 文档

README

  1. [Expresspay Refines]
    1. [Basic Usage]
    2. [Custom field types]
    3. [Writing custom rules]

Expresspay Refine

A simple server-side request filter, sanitizer and validator

Basic Usage

`Expay\Refine\Filter` does most of the work. Most calls to the class will only use the methods `addField` and `check`.

A field is defined with its key in the request and a value indicating its type. When this value is a string, its used to lookup a stored list of rules to filter the request value with.

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
		)

)

Custom field types

Custom field types can be added by specifying the rules to apply to them. This can be done with the following code:

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
		)

)

A factory function can be used to avoid repeating definitions.

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
		)

)

Writing custom rules

Custom rules can be written by sub-classing \Expay\Refine\Rules\Rule and implementing the apply method.

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"
  }
}

Writing validations

Validations can be written by adding the filter rule and validation rules.

Visit this github repository Rakit/Validation for more validation rule options you can use.

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"
  }
}

Writing Custom Validation Rules

Custom validation rules can be written by adding the filter rule, then add validation rules as first argument and key value pairs containing your custom validation rule names and their respective classes as a second argument to the filter rule.

Visit this github repository Rakit/Validation/Override/Rules for more custom validation rule options you can use.

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"
    }
  }
}

expresspaygh/exp-refine 适用场景与选型建议

expresspaygh/exp-refine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 208 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 06 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 expresspaygh/exp-refine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 expresspaygh/exp-refine 我们能提供哪些服务?
定制开发 / 二次开发

基于 expresspaygh/exp-refine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 208
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 15
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-06-12