commit | author | age
|
2207d6
|
1 |
Webmozart Assert |
W |
2 |
================ |
|
3 |
|
|
4 |
[![Build Status](https://travis-ci.org/webmozart/assert.svg?branch=master)](https://travis-ci.org/webmozart/assert) |
|
5 |
[![Build status](https://ci.appveyor.com/api/projects/status/lyg83bcsisrr94se/branch/master?svg=true)](https://ci.appveyor.com/project/webmozart/assert/branch/master) |
|
6 |
[![Latest Stable Version](https://poser.pugx.org/webmozart/assert/v/stable.svg)](https://packagist.org/packages/webmozart/assert) |
|
7 |
[![Total Downloads](https://poser.pugx.org/webmozart/assert/downloads.svg)](https://packagist.org/packages/webmozart/assert) |
|
8 |
|
|
9 |
Latest release: [1.2.0](https://packagist.org/packages/webmozart/assert#1.2.0) |
|
10 |
|
|
11 |
PHP >= 5.3.9 |
|
12 |
|
|
13 |
This library contains efficient assertions to test the input and output of |
|
14 |
your methods. With these assertions, you can greatly reduce the amount of coding |
|
15 |
needed to write a safe implementation. |
|
16 |
|
|
17 |
All assertions in the [`Assert`] class throw an `\InvalidArgumentException` if |
|
18 |
they fail. |
|
19 |
|
|
20 |
FAQ |
|
21 |
--- |
|
22 |
|
|
23 |
**What's the difference to [beberlei/assert]?** |
|
24 |
|
|
25 |
This library is heavily inspired by Benjamin Eberlei's wonderful [assert package], |
|
26 |
but fixes a usability issue with error messages that can't be fixed there without |
|
27 |
breaking backwards compatibility. |
|
28 |
|
|
29 |
This package features usable error messages by default. However, you can also |
|
30 |
easily write custom error messages: |
|
31 |
|
|
32 |
``` |
|
33 |
Assert::string($path, 'The path is expected to be a string. Got: %s'); |
|
34 |
``` |
|
35 |
|
|
36 |
In [beberlei/assert], the ordering of the `%s` placeholders is different for |
|
37 |
every assertion. This package, on the contrary, provides consistent placeholder |
|
38 |
ordering for all assertions: |
|
39 |
|
|
40 |
* `%s`: The tested value as string, e.g. `"/foo/bar"`. |
|
41 |
* `%2$s`, `%3$s`, ...: Additional assertion-specific values, e.g. the |
|
42 |
minimum/maximum length, allowed values, etc. |
|
43 |
|
|
44 |
Check the source code of the assertions to find out details about the additional |
|
45 |
available placeholders. |
|
46 |
|
|
47 |
Installation |
|
48 |
------------ |
|
49 |
|
|
50 |
Use [Composer] to install the package: |
|
51 |
|
|
52 |
``` |
|
53 |
$ composer require webmozart/assert |
|
54 |
``` |
|
55 |
|
|
56 |
Example |
|
57 |
------- |
|
58 |
|
|
59 |
```php |
|
60 |
use Webmozart\Assert\Assert; |
|
61 |
|
|
62 |
class Employee |
|
63 |
{ |
|
64 |
public function __construct($id) |
|
65 |
{ |
|
66 |
Assert::integer($id, 'The employee ID must be an integer. Got: %s'); |
|
67 |
Assert::greaterThan($id, 0, 'The employee ID must be a positive integer. Got: %s'); |
|
68 |
} |
|
69 |
} |
|
70 |
``` |
|
71 |
|
|
72 |
If you create an employee with an invalid ID, an exception is thrown: |
|
73 |
|
|
74 |
```php |
|
75 |
new Employee('foobar'); |
|
76 |
// => InvalidArgumentException: |
|
77 |
// The employee ID must be an integer. Got: string |
|
78 |
|
|
79 |
new Employee(-10); |
|
80 |
// => InvalidArgumentException: |
|
81 |
// The employee ID must be a positive integer. Got: -10 |
|
82 |
``` |
|
83 |
|
|
84 |
Assertions |
|
85 |
---------- |
|
86 |
|
|
87 |
The [`Assert`] class provides the following assertions: |
|
88 |
|
|
89 |
### Type Assertions |
|
90 |
|
|
91 |
Method | Description |
|
92 |
-------------------------------------------------------- | -------------------------------------------------- |
|
93 |
`string($value, $message = '')` | Check that a value is a string |
|
94 |
`stringNotEmpty($value, $message = '')` | Check that a value is a non-empty string |
|
95 |
`integer($value, $message = '')` | Check that a value is an integer |
|
96 |
`integerish($value, $message = '')` | Check that a value casts to an integer |
|
97 |
`float($value, $message = '')` | Check that a value is a float |
|
98 |
`numeric($value, $message = '')` | Check that a value is numeric |
|
99 |
`natural($value, $message= ''')` | Check that a value is a non-negative integer |
|
100 |
`boolean($value, $message = '')` | Check that a value is a boolean |
|
101 |
`scalar($value, $message = '')` | Check that a value is a scalar |
|
102 |
`object($value, $message = '')` | Check that a value is an object |
|
103 |
`resource($value, $type = null, $message = '')` | Check that a value is a resource |
|
104 |
`isCallable($value, $message = '')` | Check that a value is a callable |
|
105 |
`isArray($value, $message = '')` | Check that a value is an array |
|
106 |
`isTraversable($value, $message = '')` (deprecated) | Check that a value is an array or a `\Traversable` |
|
107 |
`isIterable($value, $message = '')` | Check that a value is an array or a `\Traversable` |
|
108 |
`isCountable($value, $message = '')` | Check that a value is an array or a `\Countable` |
|
109 |
`isInstanceOf($value, $class, $message = '')` | Check that a value is an `instanceof` a class |
|
110 |
`isInstanceOfAny($value, array $classes, $message = '')` | Check that a value is an `instanceof` a at least one class on the array of classes |
|
111 |
`notInstanceOf($value, $class, $message = '')` | Check that a value is not an `instanceof` a class |
|
112 |
`isArrayAccessible($value, $message = '')` | Check that a value can be accessed as an array |
|
113 |
|
|
114 |
### Comparison Assertions |
|
115 |
|
|
116 |
Method | Description |
|
117 |
----------------------------------------------- | -------------------------------------------------- |
|
118 |
`true($value, $message = '')` | Check that a value is `true` |
|
119 |
`false($value, $message = '')` | Check that a value is `false` |
|
120 |
`null($value, $message = '')` | Check that a value is `null` |
|
121 |
`notNull($value, $message = '')` | Check that a value is not `null` |
|
122 |
`isEmpty($value, $message = '')` | Check that a value is `empty()` |
|
123 |
`notEmpty($value, $message = '')` | Check that a value is not `empty()` |
|
124 |
`eq($value, $value2, $message = '')` | Check that a value equals another (`==`) |
|
125 |
`notEq($value, $value2, $message = '')` | Check that a value does not equal another (`!=`) |
|
126 |
`same($value, $value2, $message = '')` | Check that a value is identical to another (`===`) |
|
127 |
`notSame($value, $value2, $message = '')` | Check that a value is not identical to another (`!==`) |
|
128 |
`greaterThan($value, $value2, $message = '')` | Check that a value is greater than another |
|
129 |
`greaterThanEq($value, $value2, $message = '')` | Check that a value is greater than or equal to another |
|
130 |
`lessThan($value, $value2, $message = '')` | Check that a value is less than another |
|
131 |
`lessThanEq($value, $value2, $message = '')` | Check that a value is less than or equal to another |
|
132 |
`range($value, $min, $max, $message = '')` | Check that a value is within a range |
|
133 |
`oneOf($value, array $values, $message = '')` | Check that a value is one of a list of values |
|
134 |
|
|
135 |
### String Assertions |
|
136 |
|
|
137 |
You should check that a value is a string with `Assert::string()` before making |
|
138 |
any of the following assertions. |
|
139 |
|
|
140 |
Method | Description |
|
141 |
--------------------------------------------------- | ----------------------------------------------------------------- |
|
142 |
`contains($value, $subString, $message = '')` | Check that a string contains a substring |
|
143 |
`notContains($value, $subString, $message = '')` | Check that a string does not contains a substring |
|
144 |
`startsWith($value, $prefix, $message = '')` | Check that a string has a prefix |
|
145 |
`startsWithLetter($value, $message = '')` | Check that a string starts with a letter |
|
146 |
`endsWith($value, $suffix, $message = '')` | Check that a string has a suffix |
|
147 |
`regex($value, $pattern, $message = '')` | Check that a string matches a regular expression |
|
148 |
`notRegex($value, $pattern, $message = '')` | Check that a string does not match a regular expression |
|
149 |
`alpha($value, $message = '')` | Check that a string contains letters only |
|
150 |
`digits($value, $message = '')` | Check that a string contains digits only |
|
151 |
`alnum($value, $message = '')` | Check that a string contains letters and digits only |
|
152 |
`lower($value, $message = '')` | Check that a string contains lowercase characters only |
|
153 |
`upper($value, $message = '')` | Check that a string contains uppercase characters only |
|
154 |
`length($value, $length, $message = '')` | Check that a string has a certain number of characters |
|
155 |
`minLength($value, $min, $message = '')` | Check that a string has at least a certain number of characters |
|
156 |
`maxLength($value, $max, $message = '')` | Check that a string has at most a certain number of characters |
|
157 |
`lengthBetween($value, $min, $max, $message = '')` | Check that a string has a length in the given range |
|
158 |
`uuid($value, $message = '')` | Check that a string is a valid UUID |
|
159 |
`ip($value, $message = '')` | Check that a string is a valid IP (either IPv4 or IPv6) |
|
160 |
`ipv4($value, $message = '')` | Check that a string is a valid IPv4 |
|
161 |
`ipv6($value, $message = '')` | Check that a string is a valid IPv6 |
|
162 |
`notWhitespaceOnly($value, $message = '')` | Check that a string contains at least one non-whitespace character |
|
163 |
|
|
164 |
### File Assertions |
|
165 |
|
|
166 |
Method | Description |
|
167 |
----------------------------------- | -------------------------------------------------- |
|
168 |
`fileExists($value, $message = '')` | Check that a value is an existing path |
|
169 |
`file($value, $message = '')` | Check that a value is an existing file |
|
170 |
`directory($value, $message = '')` | Check that a value is an existing directory |
|
171 |
`readable($value, $message = '')` | Check that a value is a readable path |
|
172 |
`writable($value, $message = '')` | Check that a value is a writable path |
|
173 |
|
|
174 |
### Object Assertions |
|
175 |
|
|
176 |
Method | Description |
|
177 |
----------------------------------------------------- | -------------------------------------------------- |
|
178 |
`classExists($value, $message = '')` | Check that a value is an existing class name |
|
179 |
`subclassOf($value, $class, $message = '')` | Check that a class is a subclass of another |
|
180 |
`interfaceExists($value, $message = '')` | Check that a value is an existing interface name |
|
181 |
`implementsInterface($value, $class, $message = '')` | Check that a class implements an interface |
|
182 |
`propertyExists($value, $property, $message = '')` | Check that a property exists in a class/object |
|
183 |
`propertyNotExists($value, $property, $message = '')` | Check that a property does not exist in a class/object |
|
184 |
`methodExists($value, $method, $message = '')` | Check that a method exists in a class/object |
|
185 |
`methodNotExists($value, $method, $message = '')` | Check that a method does not exist in a class/object |
|
186 |
|
|
187 |
### Array Assertions |
|
188 |
|
|
189 |
Method | Description |
|
190 |
-------------------------------------------------- | ------------------------------------------------------------------ |
|
191 |
`keyExists($array, $key, $message = '')` | Check that a key exists in an array |
|
192 |
`keyNotExists($array, $key, $message = '')` | Check that a key does not exist in an array |
|
193 |
`count($array, $number, $message = '')` | Check that an array contains a specific number of elements |
|
194 |
`minCount($array, $min, $message = '')` | Check that an array contains at least a certain number of elements |
|
195 |
`maxCount($array, $max, $message = '')` | Check that an array contains at most a certain number of elements |
|
196 |
`countBetween($array, $min, $max, $message = '')` | Check that an array has a count in the given range |
|
197 |
`isList($array, $message = '')` | Check that an array is a non-associative list |
|
198 |
`isMap($array, $message = '')` | Check that an array is associative and has strings as keys |
|
199 |
|
|
200 |
### Function Assertions |
|
201 |
|
|
202 |
Method | Description |
|
203 |
------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
|
204 |
`throws($closure, $class, $message = '')` | Check that a function throws a certain exception. Subclasses of the exception class will be accepted. |
|
205 |
|
|
206 |
### Collection Assertions |
|
207 |
|
|
208 |
All of the above assertions can be prefixed with `all*()` to test the contents |
|
209 |
of an array or a `\Traversable`: |
|
210 |
|
|
211 |
```php |
|
212 |
Assert::allIsInstanceOf($employees, 'Acme\Employee'); |
|
213 |
``` |
|
214 |
|
|
215 |
### Nullable Assertions |
|
216 |
|
|
217 |
All of the above assertions can be prefixed with `nullOr*()` to run the |
|
218 |
assertion only if it the value is not `null`: |
|
219 |
|
|
220 |
```php |
|
221 |
Assert::nullOrString($middleName, 'The middle name must be a string or null. Got: %s'); |
|
222 |
``` |
|
223 |
|
|
224 |
Authors |
|
225 |
------- |
|
226 |
|
|
227 |
* [Bernhard Schussek] a.k.a. [@webmozart] |
|
228 |
* [The Community Contributors] |
|
229 |
|
|
230 |
Contribute |
|
231 |
---------- |
|
232 |
|
|
233 |
Contributions to the package are always welcome! |
|
234 |
|
|
235 |
* Report any bugs or issues you find on the [issue tracker]. |
|
236 |
* You can grab the source code at the package's [Git repository]. |
|
237 |
|
|
238 |
Support |
|
239 |
------- |
|
240 |
|
|
241 |
If you are having problems, send a mail to bschussek@gmail.com or shout out to |
|
242 |
[@webmozart] on Twitter. |
|
243 |
|
|
244 |
License |
|
245 |
------- |
|
246 |
|
|
247 |
All contents of this package are licensed under the [MIT license]. |
|
248 |
|
|
249 |
[beberlei/assert]: https://github.com/beberlei/assert |
|
250 |
[assert package]: https://github.com/beberlei/assert |
|
251 |
[Composer]: https://getcomposer.org |
|
252 |
[Bernhard Schussek]: http://webmozarts.com |
|
253 |
[The Community Contributors]: https://github.com/webmozart/assert/graphs/contributors |
|
254 |
[issue tracker]: https://github.com/webmozart/assert/issues |
|
255 |
[Git repository]: https://github.com/webmozart/assert |
|
256 |
[@webmozart]: https://twitter.com/webmozart |
|
257 |
[MIT license]: LICENSE |
|
258 |
[`Assert`]: src/Assert.php |