gravity / datas_checker
通过字段名称(数组或对象)控制数据有效性
V1.0.2
2023-05-23 21:40 UTC
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2024-09-25 07:21:56 UTC
README
正如其名所示,数据检查器允许您快速检查您的数组元素是否符合您的标准
PHP版本 >= 5.4
使用Composer安装
composer require gravity/data_checker
使用PSR-4与Composer要求
<?php use Gravity\data_checker;
可用方法
以下是当前实现用于验证数据集的不同方法
- error_message //set an error on failure
- alias //change the default data_name to compose a more explicit message with the error array
- required //check null & empty values
- string
- int
- numeric
- date
- greater_than //works with dates, numerics and int values
- lower_than //works with dates, numerics and int values
- contains_special_character //define if your string contains special characters
- contains_lower //define if your string contains lower case characters
- contains_upper //define if your string contains upper case characters
- contains_number //define if your string contains number characters
- max_length
- min_length
- ip_address //IPV4 & IPV6
- email
- disposable_email
- street_address
- alphanumeric //doesn't match special chars
- not_alphanumeric //doesn't match special chars
⚠️ 以下是需要参数的方法 ⚠️
- greater_than => 字符串或数字类型
- lower_than => 字符串或数字类型
- max_length => 数字类型
- min_length => 数字类型
示例
// NUMBER TEST $control_tests = [ "myNumberField" => [ "required", "greater_than" => 3 ] ]; // DATE TEST $control_tests = [ "myDateField" => [ "required", "greater_than" => "2016-01-01" ] ]; // STRING TEST $control_tests = [ "myStringField" => [ "required", "max_length" => 8 ] ];
构建验证数组并启动测试的步骤
第一步,您需要创建一个包含您要验证的字段的数组
$control_tests = [ "myFirstField", "mySecondField" ];
第二步,对于您创建的每个字段,创建一个包含您要使用的验证方法的第二个数组
$control_tests = [ "myFirstField" => [ "required", "string" ], "mySecondField" => [ "required", "int" ] ];
第三步,创建data_checker实例,使用verify方法与您的数组或对象数据以及验证数组进行验证。
$checker = new Data_checker(); $isCorrectdata = $checker->verify($data_to_check, $control_tests);
完整示例
<?php use Gravity\data_checker; // Can be an array (like $_POST) or an object $data_to_check = [ "creation_date" => "2015-31-01", "first_name" => 4, "last_name" => "Paul", "id" => "24", "ip" => "random string", "email" => "tata@test", "test" => "random string", "captcha" => "4D#I3", "password" => "azerty123" ]; $control_tests = [ "creation_date" => [ "required", "date", "greater_than" => "2016-01-01", "error_message" => "the creation date isn't a date or be lower than '2016-01-01'" ], "first_name" => [ "required", "string", "min_length" => 4, "not_alphanumeric", "error_message" => "the firstname isn't a word or be lower than 4 characters" ], "last_name" => [ "required", "string", "min_length" => 1, "error_message" => "the name isn't a word or be lower than 1 characters" ], "id" => [ "required", "int", "error_message" => "the id doesn't exist or not an integer" ], "ip" => [ "required", "ip_address", "alphanumeric", "error_message" => "the ip address doesn't exist or not valid ip address" ], "email" => [ "required", "email", "disposable_email" ], "captcha" => [ "required", "string", "alphanumeric", "min_length" => 5, "max_length" => 5 ], "test" => [ "required", "string", "alias" => "Alias_test" ], "password" => [ "required", "alphanumeric", "min_length" => 8, "contains_int", "contains_lower", "contains_upper", "contains_special_character" ] ]; $data_control = new data_checker(); $isCorrectdata = $data_control->verify($data_to_check, $control_tests);
结果表
结果php
var_dump($isCorrectdata); [ 0 => [ 'error_message' => 'the creation date isn\'t date or be lower than \'2016-01-01\'', 'data_eval' => '2015-31-01', 'data_name' => 'creation_date', 'test_name' => 'greater_than', ], 1 => [ 'error_message' => 'the firstname isn\'t a word or be lower than 4 characters', 'data_eval' => 4, 'data_name' => 'first_name', 'test_name' => 'string', ], 2 => [ 'error_message' => 'the firstname isn\'t a word or be lower than 4 characters', 'data_eval' => 4, 'data_name' => 'first_name', 'test_name' => 'min_length', ], 3 => [ 'error_message' => 'the ip adress doesn\'t exist or not valid ip address', 'data_eval' => 'random string', 'data_name' => 'ip', 'test_name' => 'ip_address', ], 4 => [ 'error_message' => 'Doesn\'t match the control test EMAIL as excepted', 'data_eval' => 'tata@test', 'data_name' => 'email', 'test_name' => 'email', ], 5 => [ 'error_message' => 'Doesn\'t match the control test ALPHANUMERIC as excepted', 'data_eval' => '4D#I3', 'data_name' => 'captcha', 'test_name' => 'alphanumeric' ], 7 => [ 'error_message' => 'Doesn\'t match the control test NOT_ALPHANUMERIC as excepted', 'data_eval' => 'azerty123', 'data_name' => 'password', 'test_name' => 'not_alphanumeric' ], 6 => [ 'error_message' => 'Doesn\'t match the control test CONTAINS_UPPER as excepted', 'data_eval' => 'azerty123', 'data_name' => 'password', 'test_name' => 'contains_upper', ], 7 => [ 'error_message' => 'Doesn\'t match the control test CONTAINS_SPECIAL_CHARACTER as excepted', 'data_eval' => 'azerty123', 'data_name' => 'password', 'test_name' => 'contains_special_character', ] ]
如您所见,您有所有必要的内容来显示适当的消息。
希望这个小工具能为您节省检查数据集有效性的一些时间:)