承接 gravity/datas_checker 相关项目开发

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

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

gravity/datas_checker

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 :)

gravity/datas_checker 适用场景与选型建议

gravity/datas_checker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 89 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 11 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 gravity/datas_checker 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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