定制 fresh-advance/array-validator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

fresh-advance/array-validator

Composer 安装命令:

composer require fresh-advance/array-validator

包简介

The component validates form data or any given array by provided rule list.

README 文档

README

Build Status Quality Gate Status Coverage Technical Debt Packagist

Simple form data / any array validation tool.

  • Component validates an array by provided rules list.
  • Its possible to use multiple configurations of one rule for one field in one validation run.
  • Not dependant on other third party components.
  • Tested with PHP 7.3 and up, including PHP 8.0. Fits PSR-12 coding style.

Installation

Installation via composer:

composer require fresh-advance/array-validator

Usage example

use Sieg\ArrayValidator\Keys;
use Sieg\ArrayValidator\Rule;
use Sieg\ArrayValidator\RuleCase;
use Sieg\ArrayValidator\RuleCaseCollection;
use Sieg\ArrayValidator\Validator;

$configurationExample = new RuleCaseCollection(
    new RuleCase(
        new Keys\All(),
        new Rule\LengthRange(5, 7)
    ),
    new RuleCase(
        new Keys\Collection('field1', 'field3'),
        new Rule\Expression('/value\d+/')
    ),
    new RuleCase(
        new Keys\Expression('/2$/'),
        new Rule\Expression('/value\d+/'),
        'Special message'
    )
);

$dataExample = [
    'field1' => 'value1',
    'field2' => 'something'
];

$validator = new Validator($configurationExample);
$errors = $validator->validate($dataExample);
if (empty($errors)) {
    // array fits validation configuration
    echo 'ok';
} else {
    print_r($errors);
}

Gives validation errors with fields as keys:

Array
(
    [field2] => Array
        (
            [0] => VALIDATOR_RULE_LENGTH_TOO_HIGH
            [1] => Special message
        )

    [field3] => Array
        (
            [0] => VALIDATOR_RULE_EXPRESSION_MATCH_FAILED
        )
)

Predefined Rules

There are some basic rules implemented with the component:

  • Callback(closure $closure)

    • Takes Closure as parameter. $key and $data will be sent to Closure.
  • EqualsTo(mixed $value)

    • Check if value is equal to Rule $value parameter.
  • EqualsToKey(string $key)

    • Check if value is equal to other key value.
  • Expression(string $regex)

    • Takes regex as parameter.
  • Filter(int $filterRule, array $filterOptions)

    • Rule uses filter_var function for validating the value.
    • Takes PHP filter constants to apply as first param:
      • FILTER_VALIDATE_EMAIL
      • FILTER_VALIDATE_FLOAT
      • FILTER_VALIDATE_INT
      • FILTER_VALIDATE_IP
      • FILTER_VALIDATE_MAC
      • FILTER_VALIDATE_REGEXP
      • FILTER_VALIDATE_URL
    • Takes filter_var options array as second param.
    • Refer to filter_var function documentation for more information
  • InArray(array $choices)

  • Length(int $length)

  • LengthRange(int $min, int $max)

  • Max(int $max)

  • MaxLength(int $max)

  • Min(int $min)

  • MinLength(int $min)

  • Range(int $min, int $max)

  • Required

    • Check if the field exists and not empty

Create and use custom Rules

  • Custom rule should extend \Sieg\ArrayValidator\Rule\AbstractRule
  • Use it as regular rules whose comes with the component.
  • Validator catches \Sieg\ArrayValidator\Exception\RuleFailed type Exceptions for setting field error messages.

fresh-advance/array-validator 适用场景与选型建议

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

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

围绕 fresh-advance/array-validator 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-11-09