承接 gravity/datas_checker 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

gravity/datas_checker

最新稳定版本:V1.0.2

Composer 安装命令:

composer require gravity/datas_checker

包简介

Control validity of datas by field name (Array or Object)

README 文档

README

As its name suggests, data checker allows you to quickly check if your array elements are valid according to your criteria:

PHP version >= 5.4

Install with Composer

composer require gravity/data_checker

Require with PSR-4 & Composer

<?php

use Gravity\data_checker;

Available methods:

Here are the different methods currently implemented to verify your data set:

 - 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

⚠️ Below are the only methods that take an argument ⚠️

  • greater_than => type string or number
  • lower_than => type string or number
  • max_length => type number
  • min_length => type number

Example

    // 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
    ]
];

Steps to build verification array & launch tests

First step, you need to create an array with the fields you want to verify:

$control_tests = [
    "myFirstField",
    "mySecondField"
];

Second step, for each field you create a second array contains verification methods you want to use:

$control_tests = [
    "myFirstField" => [
        "required",
        "string"
    ],
    "mySecondField" => [
        "required",
        "int"
    ]
];

Third step, create instance of data_checker and use the verify method with your array or object data to verify, and your verification array.

$checker = new Data_checker();
$isCorrectdata = $checker->verify($data_to_check, $control_tests);

Full Example :

<?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);

Result table:

Result 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',
          ]
]   

As you can see, you have everything you need to display a suitable message.

Hope this little tool will save you some time to check the validity of your dataets :)

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-11-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固