terah/fluent-assert

基于 beberlei/assert 的轻量级断言包


README

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

这是一个优秀的包 Assert (https://github.com/beberlei/assert) 的分支/子集。我只使用了流畅(链式)接口,并且断言库是一个非常常用的代码块。通过将其分解成独立的库,库的运行速度大约提高了3倍(使用我非常非科学的基准测试)。

安装

通过 Composer

$ composer require terah/fluent-assert

用法


use Terah\Assert\Assert;

(new Assert($value))->eq($value2, string $message='', string $fieldName='');
(new Assert($value))->same($value2, string $message='', string $fieldName='');
(new Assert($value))->notEq($value2, string $message='', string $fieldName='');
(new Assert($value))->notSame($value2, string $message='', string $fieldName='');
(new Assert($value))->integer(string $message='', string $fieldName='');
(new Assert($value))->float(string $message='', string $fieldName='');
(new Assert($value))->digit(string $message='', string $fieldName='');
(new Assert($value))->date($message=null, $fieldName=null);
(new Assert($value))->integerish(string $message='', string $fieldName='');
(new Assert($value))->boolean(string $message='', string $fieldName='');
(new Assert($value))->scalar(string $message='', string $fieldName='');
(new Assert($value))->notEmpty(string $message='', string $fieldName='');
(new Assert($value))->noContent(string $message='', string $fieldName='');
(new Assert($value))->notNull(string $message='', string $fieldName='');
(new Assert($value))->string(string $message='', string $fieldName='');
(new Assert($value))->regex($pattern, string $message='', string $fieldName='');
(new Assert($value))->length($length, string $message='', string $fieldName='', $encoding = 'utf8');
(new Assert($value))->minLength($minLength, string $message='', string $fieldName='', $encoding = 'utf8');
(new Assert($value))->maxLength($maxLength, string $message='', string $fieldName='', $encoding = 'utf8');
(new Assert($value))->betweenLength($minLength, $maxLength, string $message='', string $fieldName='', $encoding = 'utf8');
(new Assert($value))->startsWith($needle, string $message='', string $fieldName='', $encoding = 'utf8');
(new Assert($value))->endsWith($needle, string $message='', string $fieldName='', $encoding = 'utf8');
(new Assert($value))->contains($needle, string $message='', string $fieldName='', $encoding = 'utf8');
(new Assert($value))->choice(array $choices, string $message='', string $fieldName='');
(new Assert($value))->inArray(array $choices, string $message='', string $fieldName='');
(new Assert($value))->numeric(string $message='', string $fieldName='');
(new Assert($value))->isArray(string $message='', string $fieldName='');
(new Assert($value))->isTraversable(string $message='', string $fieldName='');
(new Assert($value))->isArrayAccessible(string $message='', string $fieldName='');
(new Assert($value))->keyExists($key, string $message='', string $fieldName='');
(new Assert($value))->keysExist($keys, string $message='', string $fieldName='');
(new Assert($value))->propertyExists($key, string $message='', string $fieldName='');
(new Assert($value))->propertiesExist(array $keys, string $message='', string $fieldName='');
(new Assert($value))->utf8(string $message='', string $fieldName='');
(new Assert($value))->keyIsset($key, string $message='', string $fieldName='');
(new Assert($value))->notEmptyKey($key, string $message='', string $fieldName='');
(new Assert($value))->notBlank(string $message='', string $fieldName='');
(new Assert($value))->isInstanceOf($className, string $message='', string $fieldName='');
(new Assert($value))->notIsInstanceOf($className, string $message='', string $fieldName='');
(new Assert($value))->subclassOf($className, string $message='', string $fieldName='');
(new Assert($value))->range($minValue, $maxValue, string $message='', string $fieldName='');
(new Assert($value))->min($minValue, string $message='', string $fieldName='');
(new Assert($value))->max($maxValue, string $message='', string $fieldName='');
(new Assert($value))->file(string $message='', string $fieldName='');
(new Assert($value))->directory(string $message='', string $fieldName='');
(new Assert($value))->readable(string $message='', string $fieldName='');
(new Assert($value))->writeable(string $message='', string $fieldName='');
(new Assert($value))->email(string $message='', string $fieldName='');
(new Assert($value))->url(string $message='', string $fieldName='');
(new Assert($value))->alnum(string $message='', string $fieldName='');
(new Assert($value))->true(string $message='', string $fieldName='');
(new Assert($value))->false(string $message='', string $fieldName='');
(new Assert($value))->classExists(string $message='', string $fieldName='');
(new Assert($value))->implementsInterface($interfaceName, string $message='', string $fieldName='');
(new Assert($value))->isJsonString(string $message='', string $fieldName='');
(new Assert($value))->uuid(string $message='', string $fieldName='');
(new Assert($value))->samAccountName(string $message='', string $fieldName='');
(new Assert($value))->unc(string $message='', string $fieldName='');
(new Assert($value))->driveLetter(string $message='', string $fieldName='');
(new Assert($value))->userPrincipalName(string $message='', string $fieldName='');
(new Assert($value))->count($count, string $message='', string $fieldName='');
(new Assert($value))->choicesNotEmpty(array $choices, string $message='', string $fieldName='');
(new Assert($value))->methodExists($object, string $message='', string $fieldName='');
(new Assert($value))->isObject(string $message='', string $fieldName='');

// Chaining
(new Assert($myValue))->integer()->notEmpty()->eq(1);

// Checking members of arrays and objects)
(new Assert($myArray))->all()->integer()->notEmpty()->eq(1);

// Null or valid
(new Assert($myNullValue)->nullOr()->integer()->notEmpty()->eq(1);

// Reset the all and nullOr flags and set value
(new Assert($value))->reset($value)
// Set a new value
(new Assert($value))->value($value)
// Set the null or flag
(new Assert($value))->nullOr($nullOr=true)
// Set the all flag
(new Assert($value))->all($all=true)
// Set the exception class
(new Assert($value))->setExceptionClass('\\My\\Exception\\Class');

测试

$ bin/tester tests/

请参阅 tests/AssertSuite.php 以获取编写测试的示例

贡献

有关详细信息,请参阅CONTRIBUTING

安全性

如果您发现任何安全相关的问题,请通过 terry@terah.com.au 发送电子邮件,而不是使用问题跟踪器。

鸣谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件