2015-01-30 09:40:06 +01:00
|
|
|
# Key
|
|
|
|
|
2017-03-27 14:47:07 +02:00
|
|
|
- `new Key(string $name)`
|
|
|
|
- `new Key(string $name, Validatable $rule)`
|
|
|
|
- `new Key(string $name, Validatable $rule, bool $mandatory)`
|
2015-01-30 09:40:06 +01:00
|
|
|
|
|
|
|
Validates an array key.
|
|
|
|
|
|
|
|
```php
|
2015-10-18 03:44:47 +02:00
|
|
|
$dict = [
|
2015-01-30 09:40:06 +01:00
|
|
|
'foo' => 'bar'
|
2015-10-18 03:44:47 +02:00
|
|
|
];
|
2015-01-30 09:40:06 +01:00
|
|
|
|
2015-10-20 02:53:25 +02:00
|
|
|
v::key('foo')->validate($dict); // true
|
2015-01-30 09:40:06 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
You can also validate the key value itself:
|
|
|
|
|
|
|
|
```php
|
2015-10-20 02:53:25 +02:00
|
|
|
v::key('foo', v::equals('bar'))->validate($dict); // true
|
2015-01-30 09:40:06 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
Third parameter makes the key presence optional:
|
|
|
|
|
|
|
|
```php
|
2015-10-07 16:52:03 +02:00
|
|
|
v::key('lorem', v::stringType(), false)->validate($dict); // true
|
2015-01-30 09:40:06 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
The name of this validator is automatically set to the key name.
|
|
|
|
|
2015-08-20 06:38:41 +02:00
|
|
|
***
|
2015-01-30 09:40:06 +01:00
|
|
|
See also:
|
|
|
|
|
|
|
|
* [Attribute](Attribute.md)
|