dmamontov / restnormalizer
通过 'Representational State Transfer' 传输数据的标准化。
1.0.2
2015-09-30 09:20 UTC
Requires
- php: >=5.3.3
This package is auto-updated.
Last update: 2024-09-18 06:47:18 UTC
README
REST Normalizer
此类可以根据 JSON 规则验证和过滤参数。
它可以接受一个请求参数列表,该列表以数组或 JSON 字符串的形式传递,并按照定义在外部 JSON 格式文件中的规则进行验证。
该类可以遍历参数数据,并检查条目是否与特定请求类型的规则文件中定义的类型和验证规则匹配。
无效值可能会被丢弃并记录到指定的日志文件中。
在标准化过程前后,该类还可以调用回调函数。
更多信息.
要求
- PHP 版本 >5.0
安装
-
安装 composer
-
在项目文件夹中执行
composer require dmamontov/restnormalizer ~1.0.2
在 config composer.json
中,您的项目将添加到库 dmamontov/restnormalizer
中,该库位于 vendor/
文件夹中。如果没有配置文件或 vendors 文件夹,将创建它们。
如果您的项目之前没有使用 composer
,请连接启动文件 vendors。为此,请将代码输入到项目中
require 'path/to/vendor/autoload.php';
要格式化的数据类型和值
数据类型 "string"
- required - 检查必填值,接受参数(true,false)
- default - 默认值
- max - 字符串允许的最大长度
- min - 字符串允许的最小长度
- pad - 用符号补充(默认: " ")
"example": { "type": "string", "required": false, "default": "example", "max": 15, "min": 4, "pad": "+" }
数据类型 "int"
- required - 检查必填值,接受参数(true,false)
- default - 默认值
- max - 允许的最大值
- min - 允许的最小值
"example": { "type": "int", "required": true, "default": 55, "max": 203, "min": 10 }
数据类型 "double"
- required - 检查必填值,接受参数(true,false)
- default - 默认值
- max - 允许的最大值
- min - 允许的最小值
- decimals - 小数点后的数字数量
"example": { "type": "double", "required": true, "default": 5, "max": 20.5, "min": 1.1, "decimals": 5 }
数据类型 "bool"
- required - 检查必填值,接受参数(true,false)
- default - 默认值
"example": { "type": "bool", "required": true, "default": true }
数据类型 "datetime"
- required - 检查必填值,接受参数(true,false)
- default - 默认值(默认: "now")
- format - 日期和时间格式
"example": { "type": "datetime", "required": true, "default": "now", "format": "Y-m-d H:i:s" }
数据类型 "enum"
- required - 检查必填值,接受参数(true,false)
- default - 默认值
- values - 枚举值数组
"example": { "type": "enum", "required": true, "default": 999, "values": [1, 5, 999] }
数据类型 "skip"
"example": { "type": "skip" }
工作示例
require_once 'RestNormalizer.php'; $n = new RestNormalizer(); $n->setValidation('you-valid.json') ->setLogFile('valid.log'); $data = array( 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' ); $data = $n->normalize($data)