定制 adaddinsane/paramverify 二次开发

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

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

adaddinsane/paramverify

Composer 安装命令:

composer require adaddinsane/paramverify

包简介

Parameter verification tool

README 文档

README

A simple package to verify a set of array values, which are most likely parameters in a call. Sometimes you cannot specify in a list of arguments perhaps because it's too long, or because they might vary too much between derived class types.

This class is configured with an array which provides what values are required and their types. Non-required values can also be included to be checked - in which case they are only checked if they are present.

The main function returns an array of errors. If it's empty then everything was fine.

Structure

The settings array looks like this:

[
  'name' => [
    'required' => true,
    'type' => 'string',
    'data' => '',
    'range' => []
  ]
]

The 'name' is the key value in the array being tested (the name of the property).

Only the 'type' key is mandatory, and must be one of the acceptable values; the 'required' key is assumed false if missing; some types require an additional data string, for a 'regex' type it's the regular expression, for the 'class' type it's the fully qualified class name, and so on.

The 'range' key applies to string, int and float, and allows length and range restrictions to be inserted.

If a named parameter is in the settings array but not "required", no error is generated if it's missing, but it will be tested if it is present.

Types

The complete list of types:

  • any matches anything
  • null matches only a null value (for completeness, you never know)
  • class matches a specific class or interface (FQN in 'data')
  • object matches any object
  • array matches any array, arrays can contain their own definitions (see later).
  • int matches an integer value (and can have a range)
  • bool only matches true or false
  • int_bool matches true, false, 1 and 0
  • float matches a float value (and can have a range)
  • string matches any string (and can have a range)
  • string_list matches a string against a fixed list of strings (in 'data' separated by '|'). This is an old style "enum" which is also used to verify the types of the settings/configuration items.
  • regex tests a string with a regular expression (in 'data')
  • callable only matches a callable
  • resource only matches a resource
  • url matches a string which is also a valid URL
  • email matches a string which is also a valid email address
  • enum only matches a PHP8.1 enum (FQN in 'data')

How to use it

$settings = [
  'name' => [
    'required' => true,
    'type' => 'string'
  ],
  'value' => [
    'required' => true,
    'type' => 'int'
    'range' => [
      'min_value' => 1,
      'max_value' => 10
    ]
  ]
];

$paramVerifyFactory = new \ParamVerify\ParamVerifyFactory();

// This checks the settings and will throw an exception on error.
$verifier = $paramVerifyFactory->make($settings);

// Will return an error because 'name' is missing.
$parameters = ['value' => 5];
$errors = $verifier->verify($parameters);

// Will return no errors.
$parameters = ['name' => 'banana', 'value' => 5];
$errors = $verifier->verify($parameters);

// Will return an error because value is out of range.
$parameters = ['name' => 'banana', 'value' => 15];
$errors = $verifier->verify($parameters);

// Will return an error because name is not a string.
$parameters = ['name' => ['banana'], 'value' => 15];
$errors = $verifier->verify($parameters);

Array sub-properties

Arrays can have an additional property called settings which defines a set of sub-properties for the array. There is no limit on the nesting - apart from resources and whether it's really a good idea.

$settings = [
  'name' => [
    'required' => true,
    'type' => 'string'
  ],
  'address' => [
    'required' => true,
    'type' => 'array'
    'settings' => [
      'line1' => ['required' => true, 'type' => 'string'],
      'line2' => ['type' => 'string'],
      'line3' => ['type' => 'string'],
      'postal_code' => ['required' => true, 'type' => 'string'],
    ]
  ]
];

$paramVerifyFactory = new \ParamVerify\ParamVerifyFactory();

// This checks the settings and will throw an exception on error.
$verifier = $paramVerifyFactory->make($settings);

// This would not generate any errors.
$parameters = [
  'name' => 'jane',
  'address' => [
    'line1' => 'first line of address',
    'line3' => 'third line of address',
    'postal_code' => 'XX1 73YY'
  ]
];
$errors = $verifier->verify($parameters);

adaddinsane/paramverify 适用场景与选型建议

adaddinsane/paramverify 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 307 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 08 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「validation」 「array」 「parameter」 「argument」 「verification」 「values」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-08-04