• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..28-Mar-2019-

src/H28-Mar-2019-1,188888

CHANGELOG.mdH A D27-Mar-20191.5 KiB7751

LICENSEH A D27-Mar-20191.1 KiB2116

README.mdH A D27-Mar-201913.1 KiB259203

composer.jsonH A D27-Mar-2019853 4039

README.md

1Webmozart Assert
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
9Latest release: [1.2.0](https://packagist.org/packages/webmozart/assert#1.2.0)
10
11PHP >= 5.3.9
12
13This library contains efficient assertions to test the input and output of
14your methods. With these assertions, you can greatly reduce the amount of coding
15needed to write a safe implementation.
16
17All assertions in the [`Assert`] class throw an `\InvalidArgumentException` if
18they fail.
19
20FAQ
21---
22
23**What's the difference to [beberlei/assert]?**
24
25This library is heavily inspired by Benjamin Eberlei's wonderful [assert package],
26but fixes a usability issue with error messages that can't be fixed there without
27breaking backwards compatibility.
28
29This package features usable error messages by default. However, you can also
30easily write custom error messages:
31
32```
33Assert::string($path, 'The path is expected to be a string. Got: %s');
34```
35
36In [beberlei/assert], the ordering of the `%s` placeholders is different for
37every assertion. This package, on the contrary, provides consistent placeholder
38ordering 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
44Check the source code of the assertions to find out details about the additional
45available placeholders.
46
47Installation
48------------
49
50Use [Composer] to install the package:
51
52```
53$ composer require webmozart/assert
54```
55
56Example
57-------
58
59```php
60use Webmozart\Assert\Assert;
61
62class 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
72If you create an employee with an invalid ID, an exception is thrown:
73
74```php
75new Employee('foobar');
76// => InvalidArgumentException:
77//    The employee ID must be an integer. Got: string
78
79new Employee(-10);
80// => InvalidArgumentException:
81//    The employee ID must be a positive integer. Got: -10
82```
83
84Assertions
85----------
86
87The [`Assert`] class provides the following assertions:
88
89### Type Assertions
90
91Method                                                   | 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
116Method                                          | 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
137You should check that a value is a string with `Assert::string()` before making
138any of the following assertions.
139
140Method                                              | 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
166Method                              | 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
176Method                                                | 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
189Method                                             | 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
202Method                                      | 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
208All of the above assertions can be prefixed with `all*()` to test the contents
209of an array or a `\Traversable`:
210
211```php
212Assert::allIsInstanceOf($employees, 'Acme\Employee');
213```
214
215### Nullable Assertions
216
217All of the above assertions can be prefixed with `nullOr*()` to run the
218assertion only if it the value is not `null`:
219
220```php
221Assert::nullOrString($middleName, 'The middle name must be a string or null. Got: %s');
222```
223
224Authors
225-------
226
227* [Bernhard Schussek] a.k.a. [@webmozart]
228* [The Community Contributors]
229
230Contribute
231----------
232
233Contributions 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
238Support
239-------
240
241If you are having problems, send a mail to bschussek@gmail.com or shout out to
242[@webmozart] on Twitter.
243
244License
245-------
246
247All 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
259