2015-01-30 09:40:06 +01:00
|
|
|
# Attribute
|
|
|
|
|
2017-03-27 14:47:07 +02:00
|
|
|
- `Attribute(string $name)`
|
|
|
|
- `Attribute(string $name, Validatable $rule)`
|
|
|
|
- `Attribute(string $name, Validatable $rule, bool $mandatory)`
|
2015-01-30 09:40:06 +01:00
|
|
|
|
2018-03-16 16:29:21 +01:00
|
|
|
Validates an object attribute, even private ones.
|
2015-01-30 09:40:06 +01:00
|
|
|
|
|
|
|
```php
|
|
|
|
$obj = new stdClass;
|
|
|
|
$obj->foo = 'bar';
|
|
|
|
|
2015-10-20 02:53:25 +02:00
|
|
|
v::attribute('foo')->validate($obj); // true
|
2015-01-30 09:40:06 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
You can also validate the attribute itself:
|
|
|
|
|
|
|
|
```php
|
2015-10-20 02:53:25 +02:00
|
|
|
v::attribute('foo', v::equals('bar'))->validate($obj); // true
|
2015-01-30 09:40:06 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
Third parameter makes the attribute presence optional:
|
|
|
|
|
|
|
|
```php
|
2015-10-07 16:52:03 +02:00
|
|
|
v::attribute('lorem', v::stringType(), false)->validate($obj); // true
|
2015-01-30 09:40:06 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
The name of this validator is automatically set to the attribute name.
|
|
|
|
|
2019-05-11 18:32:13 +02:00
|
|
|
## Categorization
|
|
|
|
|
|
|
|
- Nesting
|
|
|
|
- Objects
|
|
|
|
- Structures
|
|
|
|
|
2017-03-27 22:04:13 +02:00
|
|
|
## Changelog
|
|
|
|
|
|
|
|
Version | Description
|
|
|
|
--------|-------------
|
|
|
|
0.3.9 | Created
|
|
|
|
|
2015-08-20 06:38:41 +02:00
|
|
|
***
|
2015-01-30 09:40:06 +01:00
|
|
|
See also:
|
|
|
|
|
2017-03-28 11:53:14 +02:00
|
|
|
- [Key](Key.md)
|
2018-12-11 13:31:50 +01:00
|
|
|
- [KeyNested](KeyNested.md)
|
|
|
|
- [ObjectType](ObjectType.md)
|