yalesov/arg-validator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

yalesov/arg-validator

Composer 安装命令:

composer require yalesov/arg-validator

包简介

Validate function arguments, extending typehinting capabilities of PHP.

README 文档

README

Build Status

Solving several function argument validation/typehinting issues in PHP:

Primitive typehints:

function foo(string $foo) {}

Mixed / advanced typehints:

function foo(array|string|null $foo, int/*between 3-10*/ $bar) {}

"array of X" typehints:

function foo(Array_of_strings $foo) {}

Validating / typehinting "pseudo named function paramters" through array members:

/**
 * @param array $params
 *  - 'foo' => string
 *  - 'bar' => int
 */
function foo(array $params) {}

Declaring required constants in classes (interface-style function):

class Foo {
  /* const BAR required */
  /* const BAZ required */
}

Installation

Composer:

{
  "require": {
    "yalesov/arg-validator": "2.*"
  }
}

Usage

Simple argument validation

Validates that $foo is an integer, throwing an InvalidArgumentException if validation failed.

use Yalesov\ArgValidator\ArgValidator;

function foo($foo)
{
  ArgValidator::assert($foo, 'int');
  // throw InvalidArgumentException if $foo is not int
  // do something
}

Validates that $foo is an integer, return boolean.

use Yalesov\ArgValidator\ArgValidator;

function foo($foo)
{
  $result = ArgValidator::assert($foo, 'int', false);
  // $result = false if invalid
  // do something
}

Full function signature:

public static function assert($arg, $checks, $exception = true)

Valid argument types are specified through the $checks parameter. A string, or an array of the following accepted. $arg will be considered valid if it satisfies any one of the specified checks.

  • Flags
    • arrayof: will check for an array of remaining specified types, instead of plain types, e.g. array('arrayOf', 'string', 'int') = an array of string, or an array of int. Note: Empty array will be considered valid
    • min, max
      • combine with int, float: min and max value
      • combine with string: min and max length
      • combine with array: min and max count
  • Types
    • int
    • float
    • numeric
    • string
    • array
    • null
    • callable
    • bool
    • resource
    • notEmpty: equivalent to !empty()
    • (an array of scalars for in_array check), e.g. array('foo', 'bar') will check for in_array($arg, array('foo', 'bar')). Note: ArgValidator::assert($arg, array('foo', 'bar')) will be interpreted as instanceof checks against foo and bar. To specify an in_array check, wrap it in another array: ArgValidator::assert($arg, array(array('foo', 'bar'))).
    • (a string): assumed to be an instanceof check, should be a fully-qualified name of Class/Interface

"Named parameters" validation

use Yalesov\ArgValidator\ArgValidator;

function foo(array $params)
{
  ArgValidator::arrayAssert($params, array(
    'foo' => 'float',
    'bar' => array('string', 'notSet'),
    'baz' => array('int', 'string', 'min' => 2),
  ));
  // $params['foo'] should be a float
  // $params['bar'] should be a string, if set, or not set at all
  // $params['baz'] can be either an int (>=2), or a string (strlen >= 2)
}

Full function signature:

public static function arrayAssert(array $arg, array $checks, $exception = true)

Valid argument types are same as above, except with the addition of a notSet type.

Check class constants

namespace Foo;

use Yalesov\ArgValidator\ArgValidator;

class FooClass {
  const FOO = 'foo';
  const BAR = 2;
}

ArgValidator::assertClassConstant('Foo\FooClass', array('FOO', 'BAR'));
// \Foo\FooClass must have the constants 'FOO' and 'BAR' set

Full function signature:

public static function assertClassConstant($className, $constants, $exception = true)

$className should be a fully-qualified class name; $constants should be an array of strings, each member being the constant name.

ArgValidator::assertClassConstant() will check for:

  1. the class $className exists
  2. the class has declared the required constants specified in $constants

Misc

To centralize exception handling on argument validations, ArgValidator also provides two helper functions:

Assert that a class exists, throw InvalidArgumentException otherwise:

public static function assertClass($className, $exception = true)

Throw an InvalidArgumentException about the given variable name is not set:

public static function throwIssetException($argName)

Note: this function doesn't actually perform the isset check. I can't find a way to abstract the isset check away, without the variable being set in the first place (in order to act as argument to call this function with).

yalesov/arg-validator 适用场景与选型建议

yalesov/arg-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.78k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2016 年 07 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 26.78k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 16
  • 依赖项目数: 28
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 3
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: ISC
  • 更新时间: 2016-07-05