|
| 1 | +# Type |
| 2 | + |
| 3 | +Validates that a value is of a specific type. |
| 4 | + |
| 5 | +If an array with multiple types is provided, it will validate if the value is of at least one of the given types. |
| 6 | +For example, if `['alpha', 'numeric']` is provided, it will validate if the value is of type `alpha` or of type `numeric`. |
| 7 | + |
| 8 | +```php |
| 9 | +Type( |
| 10 | + string|array $constraint, |
| 11 | + string $message = 'The "{{ name }}" value should be of type "{{ constraint }}", "{{ value }}" given.' |
| 12 | +); |
| 13 | +``` |
| 14 | + |
| 15 | +## Basic Usage |
| 16 | + |
| 17 | +```php |
| 18 | +// Single type |
| 19 | +Validator::type('string')->validate('green'); // true |
| 20 | +Validator::type('alphanumeric')->validate('gr33n'); // true |
| 21 | + |
| 22 | +// Multiple types |
| 23 | +// Validates if value is of at least one of the provided types |
| 24 | +Validator::type(['alpha', 'numeric'])->validate('green'); // true (alpha) |
| 25 | +Validator::type(['alpha', 'numeric'])->validate('33'); // true (numeric) |
| 26 | +Validator::type(['alpha', 'numeric'])->validate('gr33n'); // false (not alpha nor numeric) |
| 27 | + |
| 28 | +// Class or interface type |
| 29 | +Validator::type(\DateTime::class)->validate(new \DateTime()); // true |
| 30 | +Validator::type(\DateTimeInterface::class)->validate(new \DateTime()); // true |
| 31 | +``` |
| 32 | + |
| 33 | +> **Note** |
| 34 | +> An `UnexpectedValueException` will be thrown when a constraint type, class or interface is invalid. |
| 35 | +
|
| 36 | +## Options |
| 37 | + |
| 38 | +### `constraint` |
| 39 | + |
| 40 | +type: `string`|`array` `required` |
| 41 | + |
| 42 | +Type(s) to validate the input value type. |
| 43 | +Can validate instances of classes and interfaces. |
| 44 | + |
| 45 | +If an array with multiple types is provided, it will validate if the value is of at least one of the given types. |
| 46 | +For example, if `['alpha', 'numeric']` is provided, it will validate if the value is of type `alpha` or of type `numeric`. |
| 47 | + |
| 48 | +Available data type constraints: |
| 49 | + |
| 50 | +- [`bool`](https://www.php.net/manual/en/function.is-bool.php), [`boolean`](https://www.php.net/manual/en/function.is-bool.php) |
| 51 | +- [`int`](https://www.php.net/manual/en/function.is-int.php), [`integer`](https://www.php.net/manual/en/function.is-int.php), [`long`](https://www.php.net/manual/en/function.is-int.php) |
| 52 | +- [`float`](https://www.php.net/manual/en/function.is-float.php), [`double`](https://www.php.net/manual/en/function.is-float.php), [`real`](https://www.php.net/manual/en/function.is-float.php) |
| 53 | +- [`numeric`](https://www.php.net/manual/en/function.is-numeric.php) |
| 54 | +- [`string`](https://www.php.net/manual/en/function.is-string.php) |
| 55 | +- [`scalar`](https://www.php.net/manual/en/function.is-scalar.php) |
| 56 | +- [`array`](https://www.php.net/manual/en/function.is-array.php) |
| 57 | +- [`iterable`](https://www.php.net/manual/en/function.is-iterable.php) |
| 58 | +- [`countable`](https://www.php.net/manual/en/function.is-countable.php) |
| 59 | +- [`callable`](https://www.php.net/manual/en/function.is-callable.php) |
| 60 | +- [`object`](https://www.php.net/manual/en/function.is-object.php) |
| 61 | +- [`resource`](https://www.php.net/manual/en/function.is-resource.php) |
| 62 | +- [`null`](https://www.php.net/manual/en/function.is-null.php) |
| 63 | + |
| 64 | +Available character type constraints: |
| 65 | + |
| 66 | +- [`alphanumeric`](https://www.php.net/manual/en/function.ctype-alnum) |
| 67 | +- [`alpha`](https://www.php.net/manual/en/function.ctype-alpha.php) |
| 68 | +- [`digit`](https://www.php.net/manual/en/function.ctype-digit.php) |
| 69 | +- [`control`](https://www.php.net/manual/en/function.ctype-cntrl.php) |
| 70 | +- [`punctuation`](https://www.php.net/manual/en/function.ctype-punct.php) |
| 71 | +- [`hexadecimal`](https://www.php.net/manual/en/function.ctype-xdigit.php) |
| 72 | +- [`graph`](https://www.php.net/manual/en/function.ctype-graph.php) |
| 73 | +- [`printable`](https://www.php.net/manual/en/function.ctype-print.php) |
| 74 | +- [`whitespace`](https://www.php.net/manual/en/function.ctype-space.php) |
| 75 | +- [`lowercase`](https://www.php.net/manual/en/function.ctype-lower.php) |
| 76 | +- [`uppercase`](https://www.php.net/manual/en/function.ctype-upper.php) |
| 77 | + |
| 78 | +### `message` |
| 79 | + |
| 80 | +type `string` default: `The "{{ name }}" value should be of type "{{ constraint }}", "{{ value }}" given.` |
| 81 | + |
| 82 | +Message that will be shown if input value is not of a specific type. |
| 83 | + |
| 84 | +The following parameters are available: |
| 85 | + |
| 86 | +| Parameter | Description | |
| 87 | +|--------------------|---------------------------| |
| 88 | +| `{{ value }}` | The current invalid value | |
| 89 | +| `{{ name }}` | Name of the invalid value | |
| 90 | +| `{{ constraint }}` | The valid type(s) | |
0 commit comments