Webmozart Assert
================
[![Latest Stable Version](https://poser.pugx.org/webmozart/assert/v/stable.svg)](https://packagist.org/packages/webmozart/assert)
[![Total Downloads](https://poser.pugx.org/webmozart/assert/downloads.svg)](https://packagist.org/packages/webmozart/assert)
This library contains efficient assertions to test the input and output of
your methods. With these assertions, you can greatly reduce the amount of coding
needed to write a safe implementation.
All assertions in the [`Assert`] class throw an `Webmozart\Assert\InvalidArgumentException` if
they fail.
FAQ
---
**What's the difference to [beberlei/assert]?**
This library is heavily inspired by Benjamin Eberlei's wonderful [assert package],
but fixes a usability issue with error messages that can't be fixed there without
breaking backwards compatibility.
This package features usable error messages by default. However, you can also
easily write custom error messages:
```
Assert::string($path, 'The path is expected to be a string. Got: %s');
```
In [beberlei/assert], the ordering of the `%s` placeholders is different for
every assertion. This package, on the contrary, provides consistent placeholder
ordering for all assertions:
* `%s`: The tested value as string, e.g. `"/foo/bar"`.
* `%2$s`, `%3$s`, ...: Additional assertion-specific values, e.g. the
minimum/maximum length, allowed values, etc.
Check the source code of the assertions to find out details about the additional
available placeholders.
Installation
------------
Use [Composer] to install the package:
```
$ composer require webmozart/assert
```
Example
-------
```php
use Webmozart\Assert\Assert;
class Employee
{
public function __construct($id)
{
Assert::integer($id, 'The employee ID must be an integer. Got: %s');
Assert::greaterThan($id, 0, 'The employee ID must be a positive integer. Got: %s');
}
}
```
If you create an employee with an invalid ID, an exception is thrown:
```php
new Employee('foobar');
// => Webmozart\Assert\InvalidArgumentException:
// The employee ID must be an integer. Got: string
new Employee(-10);
// => Webmozart\Assert\InvalidArgumentException:
// The employee ID must be a positive integer. Got: -10
```
Assertions
----------
The [`Assert`] class provides the following assertions:
### Type Assertions
Method | Description
-------------------------------------------------------- | --------------------------------------------------
`string($value, $message = '')` | Check that a value is a string
`stringNotEmpty($value, $message = '')` | Check that a value is a non-empty string
`integer($value, $message = '')` | Check that a value is an integer
`integerish($value, $message = '')` | Check that a value casts to an integer
`positiveInteger($value, $message = '')` | Check that a value is a positive (non-zero) integer
`float($value, $message = '')` | Check that a value is a float
`numeric($value, $message = '')` | Check that a value is numeric
`natural($value, $message= ''')` | Check that a value is a non-negative integer
`boolean($value, $message = '')` | Check that a value is a boolean
`scalar($value, $message = '')` | Check that a value is a scalar
`object($value, $message = '')` | Check that a value is an object
`resource($value, $type = null, $message = '')` | Check that a value is a resource
`isCallable($value, $message = '')` | Check that a value is a callable
`isArray($value, $message = '')` | Check that a value is an array
`isTraversable($value, $message = '')` (deprecated) | Check that a value is an array or a `\Traversable`
`isIterable($value, $message = '')` | Check that a value is an array or a `\Traversable`
`isCountable($value, $message = '')` | Check that a value is an array or a `\Countable`
`isInstanceOf($value, $class, $message = '')` | Check that a value is an `instanceof` a class
`isInstanceOfAny($value, array $classes, $message = '')` | Check that a value is an `instanceof` at least one class on the array of classes
`notInstanceOf($value, $class, $message = '')` | Check that a value is not an `instanceof` a class
`isAOf($value, $class, $message = '')` | Check that a value is of the class or has one of its parents
`isAnyOf($value, array $classes, $message = '')` | Check that a value is of at least one of the classes or has one of its parents
`isNotA($value, $class, $message = '')` | Check that a value is not of the class or has not one of its parents
`isArrayAccessible($value, $message = '')` | Check that a value can be accessed as an array
`uniqueValues($values, $message = '')` | Check that the given array contains unique values
### Comparison Assertions
Method | Description
----------------------------------------------- | ------------------------------------------------------------------
`true($value, $message = '')` | Check that a value is `true`
`false($value, $message = '')` | Check that a value is `false`
`notFalse($value, $message = '')` | Check that a value is not `false`
`null($value, $message = '')` | Check that a value is `null`
`notNull($value, $message = '')` | Check that a value is not `null`
`isEmpty($value, $message = '')` | Check that a value is `empty()`
`notEmpty($value, $message = '')` | Check that a value is not `empty()`
`eq($value, $value2, $message = '')` | Check that a value equals another (`==`)
`notEq($value, $value2, $message = '')` | Check that a value does not equal another (`!=`)
`same($value, $value2, $message = '')` | Check that a value is identical to another (`===`)
`notSame($value, $value2, $message = '')` | Check that a value is not identical to another (`!==`)
`greaterThan($value, $value2, $message = '')` | Check that a value is greater than another
`greaterThanEq($value, $value2, $message = '')` | Check that a value is greater than or equal to another
`lessThan($value, $value2, $message = '')` | Check that a value is less than another
`lessThanEq($value, $value2, $message = '')` | Check that a value is less than or equal to another
`range($value, $min, $max, $message = '')` | Check that a value is within a range
`inArray($value, array $values, $message = '')` | Check that a value is one of a list of values
`oneOf($value, array $values, $message = '')` | Check that a value is one of a list of values (alias of `inArray`)
### String Assertions
You should check that a value is a string with `Assert::string()` before making
any of the following assertions.
Method | Description
--------------------------------------------------- | -----------------------------------------------------------------
`contains($value, $subString, $message = '')` | Check that a string contains a substring
`notContains($value, $subString, $message = '')` | Check that a string does not contain a substring
`startsWith($value, $prefix, $message = '')` | Check that a string has a prefix
`notStartsWith($value, $prefix, $message = '')` | Check that a string does not have a prefix
`startsWithLetter($value, $message = '')` | Check that a string starts with a letter
`endsWith($value, $suffix, $message = '')` | Check that a string has a suffix
`notEndsWith($value, $suffix, $message = '')` | Check that a string does not have a suffix
`regex(
没有合适的资源?快使用搜索试试~ 我知道了~
基于PHP的FunAdmin开发系统框架(支持PHP8.0) 免费版.zip
共2403个文件
php:1920个
js:68个
html:63个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 42 浏览量
2023-07-20
07:56:42
上传
评论
收藏 8.9MB ZIP 举报
温馨提示
基于PHP的FunAdmin开发系统框架(支持PHP8.0) 免费版.zip
资源推荐
资源详情
资源评论
收起资源包目录
基于PHP的FunAdmin开发系统框架(支持PHP8.0) 免费版.zip (2403个子文件)
var-dump-server.bat 138B
carbon.bat 114B
carbon.bat 93B
test.bmp 0B
carbon 378B
carbon 355B
composer 0B
layui.css 78KB
comm.css 47KB
fun.css 22KB
layer.css 14KB
index.css 9KB
laydate.css 7KB
comm.css 4KB
install.css 4KB
cropper.css 3KB
login.css 3KB
htmlDescriptor.css 3KB
city-picker.css 2KB
timePicker.css 2KB
code.css 1KB
step.css 1KB
inputTags.css 1KB
iconfont.css 877B
regionCheckBox.css 849B
multiSelect.css 58B
phpunit.xml.dist 1013B
phpunit.xml.dist 826B
.editorconfig 271B
.editorconfig 158B
.editorconfig 131B
.editorconfig 102B
.example.env 230B
iconfont.eot 46KB
iconfont.eot 6KB
hiddeninput.exe 9KB
test.gif 233KB
loading-0.gif 6KB
loading-2.gif 2KB
loading-1.gif 701B
image.gif 338B
image.gif 338B
.gitattributes 183B
gitattributes 84B
.gitattributes 70B
.gitignore 74B
.gitignore 71B
.gitignore 65B
.gitignore 43B
.gitignore 42B
.gitignore 42B
.gitignore 37B
.gitignore 37B
.gitignore 34B
.gitignore 30B
.gitignore 29B
.gitignore 27B
.gitignore 27B
.gitignore 20B
.gitignore 15B
.gitignore 14B
.gitignore 13B
.gitignore 6B
.gitignore 6B
.gitignore 1B
.htaccess 13B
.htaccess 1B
console.html 32KB
index.html 13KB
step2.html 13KB
set.html 10KB
index.html 10KB
step3.html 9KB
reg.html 6KB
step1.html 6KB
set.html 6KB
index.html 5KB
forget.html 5KB
index.html 4KB
index.html 3KB
fieldtype.html 3KB
index.html 2KB
message.html 2KB
config.html 2KB
header.html 2KB
main.html 2KB
home.html 2KB
bbs.html 2KB
activate.html 2KB
add.html 1KB
header.html 1KB
main.html 1KB
child.html 1KB
step4.html 1KB
logintpl.html 1KB
member-nav.html 1KB
error.html 1KB
index.html 1KB
footer.html 934B
add.html 912B
共 2403 条
- 1
- 2
- 3
- 4
- 5
- 6
- 25
资源评论
助力毕业
- 粉丝: 2173
- 资源: 5122
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功