Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 1187adc

Browse files
committed
feat: added Type rule documentation
1 parent b7e1580 commit 1187adc

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

docs/03-rules.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
## Basic Rules
99

1010
- [NotBlank](03x-rules-not-blank.md)
11+
- [Type](03x-rules-type.md)
1112

1213
## Comparison Rules
1314

docs/03x-rules-type.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 data type
19+
Validator::type('string')->validate('green'); // true
20+
Validator::type('alphanumeric')->validate('gr33n'); // true
21+
22+
// Multiple data 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 and interface is invalid.
35+
36+
## Options
37+
38+
### `constraints`
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 constraint types:
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+
- [`alphanumeric`](https://www.php.net/manual/en/function.ctype-alnum)
64+
- [`alpha`](https://www.php.net/manual/en/function.ctype-alpha.php)
65+
- [`digit`](https://www.php.net/manual/en/function.ctype-digit.php)
66+
- [`control`](https://www.php.net/manual/en/function.ctype-cntrl.php)
67+
- [`punctuation`](https://www.php.net/manual/en/function.ctype-punct.php)
68+
- [`hexadecimal`](https://www.php.net/manual/en/function.ctype-xdigit.php)
69+
- [`graph`](https://www.php.net/manual/en/function.ctype-graph.php)
70+
- [`printable`](https://www.php.net/manual/en/function.ctype-print.php)
71+
- [`whitespace`](https://www.php.net/manual/en/function.ctype-space.php)
72+
- [`lowercase`](https://www.php.net/manual/en/function.ctype-lower.php)
73+
- [`uppercase`](https://www.php.net/manual/en/function.ctype-upper.php)
74+
75+
### `message`
76+
77+
type `string` default: `The "{{ name }}" value should be of type "{{ constraint }}", "{{ value }}" given.`
78+
79+
Message that will be shown if input value is not of a specific type.
80+
81+
The following parameters are available:
82+
83+
| Parameter | Description |
84+
|---------------------|---------------------------|
85+
| `{{ value }}` | The current invalid value |
86+
| `{{ name }}` | Name of the invalid value |
87+
| `{{ constraints }}` | The valid type(s) |

src/ChainedValidatorInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,9 @@ public function range(
6464
): ChainedValidatorInterface;
6565

6666
public function rule(RuleInterface $constraint): ChainedValidatorInterface;
67+
68+
public function type(
69+
string|array $constraints,
70+
string $message = 'The "{{ name }}" value should be of type "{{ constraint }}", "{{ value }}" given.'
71+
): ChainedValidatorInterface;
6772
}

src/StaticValidatorInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,9 @@ public static function range(
5454
): ChainedValidatorInterface;
5555

5656
public static function rule(RuleInterface $constraint): ChainedValidatorInterface;
57+
58+
public static function type(
59+
string|array $constraints,
60+
string $message = 'The "{{ name }}" value should be of type "{{ constraint }}", "{{ value }}" given.'
61+
): ChainedValidatorInterface;
5762
}

0 commit comments

Comments
 (0)