annotate vendor/webmozart/assert/README.md @ 19:fa3358dc1485 tip

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