Chris@0
|
1 Webmozart Assert
|
Chris@0
|
2 ================
|
Chris@0
|
3
|
Chris@0
|
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@0
|
92 Method | Description
|
Chris@0
|
93 ----------------------------------------------- | --------------------------------------------------
|
Chris@0
|
94 `string($value, $message = '')` | Check that a value is a string
|
Chris@0
|
95 `stringNotEmpty($value, $message = '')` | Check that a value is a non-empty string
|
Chris@0
|
96 `integer($value, $message = '')` | Check that a value is an integer
|
Chris@0
|
97 `integerish($value, $message = '')` | Check that a value casts to an integer
|
Chris@0
|
98 `float($value, $message = '')` | Check that a value is a float
|
Chris@0
|
99 `numeric($value, $message = '')` | Check that a value is numeric
|
Chris@0
|
100 `boolean($value, $message = '')` | Check that a value is a boolean
|
Chris@0
|
101 `scalar($value, $message = '')` | Check that a value is a scalar
|
Chris@0
|
102 `object($value, $message = '')` | Check that a value is an object
|
Chris@0
|
103 `resource($value, $type = null, $message = '')` | Check that a value is a resource
|
Chris@0
|
104 `isCallable($value, $message = '')` | Check that a value is a callable
|
Chris@0
|
105 `isArray($value, $message = '')` | Check that a value is an array
|
Chris@0
|
106 `isTraversable($value, $message = '')` | Check that a value is an array or a `\Traversable`
|
Chris@0
|
107 `isInstanceOf($value, $class, $message = '')` | Check that a value is an `instanceof` a class
|
Chris@0
|
108 `notInstanceOf($value, $class, $message = '')` | Check that a value is not an `instanceof` a class
|
Chris@0
|
109
|
Chris@0
|
110 ### Comparison Assertions
|
Chris@0
|
111
|
Chris@0
|
112 Method | Description
|
Chris@0
|
113 ----------------------------------------------- | --------------------------------------------------
|
Chris@0
|
114 `true($value, $message = '')` | Check that a value is `true`
|
Chris@0
|
115 `false($value, $message = '')` | Check that a value is `false`
|
Chris@0
|
116 `null($value, $message = '')` | Check that a value is `null`
|
Chris@0
|
117 `notNull($value, $message = '')` | Check that a value is not `null`
|
Chris@0
|
118 `isEmpty($value, $message = '')` | Check that a value is `empty()`
|
Chris@0
|
119 `notEmpty($value, $message = '')` | Check that a value is not `empty()`
|
Chris@0
|
120 `eq($value, $value2, $message = '')` | Check that a value equals another (`==`)
|
Chris@0
|
121 `notEq($value, $value2, $message = '')` | Check that a value does not equal another (`!=`)
|
Chris@0
|
122 `same($value, $value2, $message = '')` | Check that a value is identical to another (`===`)
|
Chris@0
|
123 `notSame($value, $value2, $message = '')` | Check that a value is not identical to another (`!==`)
|
Chris@0
|
124 `greaterThan($value, $value2, $message = '')` | Check that a value is greater than another
|
Chris@0
|
125 `greaterThanEq($value, $value2, $message = '')` | Check that a value is greater than or equal to another
|
Chris@0
|
126 `lessThan($value, $value2, $message = '')` | Check that a value is less than another
|
Chris@0
|
127 `lessThanEq($value, $value2, $message = '')` | Check that a value is less than or equal to another
|
Chris@0
|
128 `range($value, $min, $max, $message = '')` | Check that a value is within a range
|
Chris@0
|
129 `oneOf($value, array $values, $message = '')` | Check that a value is one of a list of values
|
Chris@0
|
130
|
Chris@0
|
131 ### String Assertions
|
Chris@0
|
132
|
Chris@0
|
133 You should check that a value is a string with `Assert::string()` before making
|
Chris@0
|
134 any of the following assertions.
|
Chris@0
|
135
|
Chris@0
|
136 Method | Description
|
Chris@0
|
137 --------------------------------------------------- | --------------------------------------------------
|
Chris@0
|
138 `contains($value, $subString, $message = '')` | Check that a string contains a substring
|
Chris@0
|
139 `startsWith($value, $prefix, $message = '')` | Check that a string has a prefix
|
Chris@0
|
140 `startsWithLetter($value, $message = '')` | Check that a string starts with a letter
|
Chris@0
|
141 `endsWith($value, $suffix, $message = '')` | Check that a string has a suffix
|
Chris@0
|
142 `regex($value, $pattern, $message = '')` | Check that a string matches a regular expression
|
Chris@0
|
143 `alpha($value, $message = '')` | Check that a string contains letters only
|
Chris@0
|
144 `digits($value, $message = '')` | Check that a string contains digits only
|
Chris@0
|
145 `alnum($value, $message = '')` | Check that a string contains letters and digits only
|
Chris@0
|
146 `lower($value, $message = '')` | Check that a string contains lowercase characters only
|
Chris@0
|
147 `upper($value, $message = '')` | Check that a string contains uppercase characters only
|
Chris@0
|
148 `length($value, $length, $message = '')` | Check that a string has a certain number of characters
|
Chris@0
|
149 `minLength($value, $min, $message = '')` | Check that a string has at least a certain number of characters
|
Chris@0
|
150 `maxLength($value, $max, $message = '')` | Check that a string has at most a certain number of characters
|
Chris@0
|
151 `lengthBetween($value, $min, $max, $message = '')` | Check that a string has a length in the given range
|
Chris@0
|
152 `uuid($value, $message = '')` | Check that a string is a valid UUID
|
Chris@0
|
153
|
Chris@0
|
154 ### File Assertions
|
Chris@0
|
155
|
Chris@0
|
156 Method | Description
|
Chris@0
|
157 ----------------------------------- | --------------------------------------------------
|
Chris@0
|
158 `fileExists($value, $message = '')` | Check that a value is an existing path
|
Chris@0
|
159 `file($value, $message = '')` | Check that a value is an existing file
|
Chris@0
|
160 `directory($value, $message = '')` | Check that a value is an existing directory
|
Chris@0
|
161 `readable($value, $message = '')` | Check that a value is a readable path
|
Chris@0
|
162 `writable($value, $message = '')` | Check that a value is a writable path
|
Chris@0
|
163
|
Chris@0
|
164 ### Object Assertions
|
Chris@0
|
165
|
Chris@0
|
166 Method | Description
|
Chris@0
|
167 ----------------------------------------------------- | --------------------------------------------------
|
Chris@0
|
168 `classExists($value, $message = '')` | Check that a value is an existing class name
|
Chris@0
|
169 `subclassOf($value, $class, $message = '')` | Check that a class is a subclass of another
|
Chris@0
|
170 `implementsInterface($value, $class, $message = '')` | Check that a class implements an interface
|
Chris@0
|
171 `propertyExists($value, $property, $message = '')` | Check that a property exists in a class/object
|
Chris@0
|
172 `propertyNotExists($value, $property, $message = '')` | Check that a property does not exist in a class/object
|
Chris@0
|
173 `methodExists($value, $method, $message = '')` | Check that a method exists in a class/object
|
Chris@0
|
174 `methodNotExists($value, $method, $message = '')` | Check that a method does not exist in a class/object
|
Chris@0
|
175
|
Chris@0
|
176 ### Array Assertions
|
Chris@0
|
177
|
Chris@0
|
178 Method | Description
|
Chris@0
|
179 ------------------------------------------- | --------------------------------------------------
|
Chris@0
|
180 `keyExists($array, $key, $message = '')` | Check that a key exists in an array
|
Chris@0
|
181 `keyNotExists($array, $key, $message = '')` | Check that a key does not exist in an array
|
Chris@0
|
182 `count($array, $number, $message = '')` | Check that an array contains a specific number of elements
|
Chris@0
|
183
|
Chris@0
|
184 ### Function Assertions
|
Chris@0
|
185
|
Chris@0
|
186 Method | Description
|
Chris@0
|
187 ------------------------------------------- | -----------------------------------------------------------------------------------------------------
|
Chris@0
|
188 `throws($closure, $class, $message = '')` | Check that a function throws a certain exception. Subclasses of the exception class will be accepted.
|
Chris@0
|
189
|
Chris@0
|
190 ### Collection Assertions
|
Chris@0
|
191
|
Chris@0
|
192 All of the above assertions can be prefixed with `all*()` to test the contents
|
Chris@0
|
193 of an array or a `\Traversable`:
|
Chris@0
|
194
|
Chris@0
|
195 ```php
|
Chris@0
|
196 Assert::allIsInstanceOf($employees, 'Acme\Employee');
|
Chris@0
|
197 ```
|
Chris@0
|
198
|
Chris@0
|
199 ### Nullable Assertions
|
Chris@0
|
200
|
Chris@0
|
201 All of the above assertions can be prefixed with `nullOr*()` to run the
|
Chris@0
|
202 assertion only if it the value is not `null`:
|
Chris@0
|
203
|
Chris@0
|
204 ```php
|
Chris@0
|
205 Assert::nullOrString($middleName, 'The middle name must be a string or null. Got: %s');
|
Chris@0
|
206 ```
|
Chris@0
|
207
|
Chris@0
|
208 Authors
|
Chris@0
|
209 -------
|
Chris@0
|
210
|
Chris@0
|
211 * [Bernhard Schussek] a.k.a. [@webmozart]
|
Chris@0
|
212 * [The Community Contributors]
|
Chris@0
|
213
|
Chris@0
|
214 Contribute
|
Chris@0
|
215 ----------
|
Chris@0
|
216
|
Chris@0
|
217 Contributions to the package are always welcome!
|
Chris@0
|
218
|
Chris@0
|
219 * Report any bugs or issues you find on the [issue tracker].
|
Chris@0
|
220 * You can grab the source code at the package's [Git repository].
|
Chris@0
|
221
|
Chris@0
|
222 Support
|
Chris@0
|
223 -------
|
Chris@0
|
224
|
Chris@0
|
225 If you are having problems, send a mail to bschussek@gmail.com or shout out to
|
Chris@0
|
226 [@webmozart] on Twitter.
|
Chris@0
|
227
|
Chris@0
|
228 License
|
Chris@0
|
229 -------
|
Chris@0
|
230
|
Chris@0
|
231 All contents of this package are licensed under the [MIT license].
|
Chris@0
|
232
|
Chris@0
|
233 [beberlei/assert]: https://github.com/beberlei/assert
|
Chris@0
|
234 [assert package]: https://github.com/beberlei/assert
|
Chris@0
|
235 [Composer]: https://getcomposer.org
|
Chris@0
|
236 [Bernhard Schussek]: http://webmozarts.com
|
Chris@0
|
237 [The Community Contributors]: https://github.com/webmozart/assert/graphs/contributors
|
Chris@0
|
238 [issue tracker]: https://github.com/webmozart/assert
|
Chris@0
|
239 [Git repository]: https://github.com/webmozart/assert
|
Chris@0
|
240 [@webmozart]: https://twitter.com/webmozart
|
Chris@0
|
241 [MIT license]: LICENSE
|
Chris@0
|
242 [`Assert`]: src/Assert.php
|