e-ghl / param-validator
验证 eGHL API 输入参数数组
v1.0.7
2021-03-15 03:06 UTC
Requires
- e-ghl/exception: ^1.0
Requires (Dev)
- phpunit/phpunit: 4.0.*
This package is auto-updated.
Last update: 2024-09-14 14:03:53 UTC
README
验证用于 捕获、支付、查询、退款、冲正 和 结算 的 eGHL API 请求的输入参数。
Composer 需求命令
composer require eghl/param-validator
简介
此库/包可以在向 eGHL 发送请求之前使用。此库验证请求参数,并抛出带有明确错误信息的异常。然而,如果所有参数都有效,则不会抛出异常,代码将继续执行。
支持的请求类型
此库支持以下请求类型的参数验证。
- 捕获
- 支付
- 查询
- 退款
- 冲正
- 结算
验证范围
以下是此包对上述请求类型执行验证的范围。
- 查找所需参数。还验证某些参数是否条件性必需。
- 验证每个参数的数据类型,例如 数值、字母 和 字母数字。
- 验证每个参数的最大字符长度。
- 对某些参数进行非正式的金额格式、IP 地址、电子邮件地址和 URL 验证。
用法
eghl/param-validator 包可以使用两种方式。
- 显式定义请求类型类。
- 使用工厂类。
显式定义请求类型类
以下代码示例解释了 查询 请求类型参数的验证。
// Require composer generated autoloader
require_once('../vendor/autoload.php');
// Namespace to be used
use eGHL\ParamValidator\QueryRequest;
// Request Parameters to be validated
$params = array(
'TransactionType' => 'QUERY',
'PymtMethod' => 'CC',
'ServiceID' => 'SIT',
'PaymentID' => '123',
'Amount' => '100.00',
'CurrencyCode' => 'MYR',
'HashValue' => 'adasdasdasd'
);
try{
$Request = new QueryRequest($params);
$params = $Request->validate(); // This line actually performs validation and will throw exception if any parameter is invalid. If all parameters are defined correctly, the function will retrun array of parameters to be sent to respective eGHL API.
}
catch(\Exception $e){
echo "<pre>".$e->getMessage()."</pre>";
}
使用工厂类
以下代码示例解释了 查询 请求类型参数的验证。
// Require composer generated autoloader
require_once('../vendor/autoload.php');
// use Namespace of validatorFactory
use eGHL\ParamValidator\core\validatorFactory;
// Request Parameters to be validated
$params = array(
'TransactionType' => 'QUERY',
'PymtMethod' => 'CC',
'ServiceID' => 'SIT',
'PaymentID' => '123',
'Amount' => '100.00',
'CurrencyCode' => 'MYR',
'HashValue' => 'adasdasdasd'
);
try{
// The first parameter is type of request (Case-Sensitive) i.e. 'Query' in the current example
$Request = validatorFactory::create('Query', $params);
$params = $Request->validate();// This line actually performs validation and will throw exception if any parameter is invalid. If all parameters are defined correctly, the function will retrun array of parameters to be sent to respective eGHL API.
}
catch(\Exception $e){
echo "<pre>".$e->getMessage()."</pre>";
}