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