lucatume/args
Composer 安装命令:
composer require lucatume/args
包简介
A PHP 5.2 compatible arguments handling library.
README 文档
README
A library to make argument checking less of a chore.
Missing type hinting
PHP will not allow type hinting scalar values in functions and methods arguments forcing cycles like this to be added in each function body:
if (!is_string($s)){
throw ...
}
if(strlen($s) > 20 || strlen($s) < 10){
throw ...
}
if(preg_match($this->name_pattern, $s) === false){
throw ...
}
The library, still a work in progress aims at making argument check more granular, tested and DRYer ') by introducing a fluent interface.
The check above could become
Arg::_($s)->is_string()->length(10, 20)->match($this->name_pattern);
Some array-related functions are there too:
Arg::_($a)->count(6, 10)->contains('value1', 'value2')->has_key('key_one');
Each method will throw an Exception if the condition is not satisfied. In the array example if array is ['some', 23] then an exception will be thrown with the message:
Array must contain at least 6 elements!
Installation
Download the class and add it to your project or use Composer like
require: {
"lucatume/args": "~0.1"
}
Methods
Each call to the class should begin using the static method _() and passing it the value to check and, optionally, the name that value will be referred to in exceptions
Arg::_($value, 'amount');
To actually check the argument against a condition use the assert() method
Arg::_($value)->assert($value > 13);
While that's the base this syntax is best used in special cases the clas doesn't cover and the convenient methods provided by the class should be used.
Each method is defined in a positive logic: nothing will happen if the argument matches the expectation, an exception will be thrown otherwise.
is_bool()- checks if value is a boolean, same asis_bool()methodis_scalar()- checks if value is a scalar, same asis_scalar()methodis_string()- checks if value is a string, same asis_string()methodis_numeric()- checks if value is a numeric, same asis_numeric()methodis_int()- checks if value is a int, same asis_int()methodis_double()- checks if value is a double, same asis_double()methodis_float()- checks if value is a float, same asis_float()methodis_null()- checks if value is null, same asis_null()methodis_resource()- checks if value is a resource, same asis_resource()methodis_array()- checks if value is an array, same asis_array()methodis_associative_array()- checks if value is an associative arrayis_object()- checks if value is an object, same asis_object()methodelse_throw($exception)- throws the specified exception if the checks are failing;$exceptioncan be an object instance or a class name. If the class name ends inExceptionthen that part of the class name can be omitted (e.g. "NotGoodException" to "NotGood")
Scalar methods
If an argument is a scalar then additional check methods are available:
at_least($value)- checks if the argument is >= the valueat_most($value)- checks if the argument is <= the valuegreater_than($value)- checks if the argument is > the valueless_than($value)- checks if the argument is < the value
String methods
If an argument is a string then additional methods are available
length($min, [$max])- checks if the argument is at least$minlong and, optionally, at most$maxchars long.match($pattern)- checks if the argument matches the given regex patterns
Array methods
If an argument is an array then additional methods are available
-
count($min, [$max])- checks if the argument contains at least$minelements and, optionally, that it contains at most$maxelements. -
has_structure($structure)- checks if the array has exactly the provided strcture, e.g.$structure = array( 'key1' => null, 'key2' => null, 'key3' => array( 'subKey1' => null, 'subKey1' => null ) );
$structure = array( 'key1' => 'some', 'key2' => 'foo', 'key3' => array( 'subKey1' => 12, 'subKey1' => 23 ) );
Arg::_($arr)->has_structure($structure);
-
extends_structure($structure)- checks if the array extends the provided strcture -
defaults($defaults)- does not perform a check but merges the argument array with a default set -
contains($value [,$value] [,$value2])- checks if the argument contains the specified value(s) -
has_key($value [,$value] [,$value2])- checks if the argument contains the specified key(s)
Object methods
is_set($property [, $property][, $property] )- checks if one ore more specified properties are set
lucatume/args 适用场景与选型建议
lucatume/args 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.43M 次下载、GitHub Stars 达 1, 最近一次更新时间为 2014 年 10 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 lucatume/args 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lucatume/args 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.43M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 24
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0
- 更新时间: 2014-10-23