-
Notifications
You must be signed in to change notification settings - Fork 777
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a16ad3
commit 3dae64a
Showing
167 changed files
with
476 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Attributes | ||
|
||
- `Attributes()` | ||
|
||
Validates the PHP attributes defined in the properties of the input. | ||
|
||
Example of object: | ||
|
||
```php | ||
use Respect\Validation\Rules; | ||
|
||
final class Person | ||
{ | ||
public function __construct( | ||
#[Rules\NotEmpty] | ||
public readonly string $name, | ||
#[Rules\Email] | ||
public readonly string $email, | ||
#[Rules\Phone] | ||
public readonly string $phone | ||
) { | ||
} | ||
} | ||
``` | ||
|
||
Here is how you can validate the attributes of the object: | ||
|
||
```php | ||
v::attributes()->assert(new Person('John Doe', '[email protected]', '+1234567890')); | ||
// No exception | ||
|
||
v::attributes()->assert(new Person('', '[email protected]', '+1234567890')); | ||
// Message: name must not be empty | ||
|
||
v::attributes()->isValid(new Person('John Doe', 'not an email', '+1234567890')); | ||
// Message: email must be a valid email address | ||
|
||
v::attributes()->isValid(new Person('John Doe', '[email protected]', 'not a phone number')); | ||
// Message: phone must be a valid telephone number | ||
|
||
v::attributes()->assert(new Person('', 'not an email', 'not a phone number')); | ||
// Full message: | ||
// - The properties of `Person { +$name="" +$email="not an email" +$phone="not a phone number" }` must be valid | ||
// - name must not be empty | ||
// - email must be a valid email address | ||
// - phone must be a valid telephone number | ||
``` | ||
|
||
## Templates | ||
|
||
### `Attributes::TEMPLATE_STANDARD` | ||
|
||
| Mode | Template | | ||
|------------|----------------------------------------------| | ||
| `default` | The properties of {{name}} must be valid | | ||
| `inverted` | The properties of {{name}} must not be valid | | ||
|
||
## Template placeholders | ||
|
||
| Placeholder | Description | | ||
|-------------|------------------------------------------------------------------| | ||
| `name` | The validated input or the custom validator name (if specified). | | ||
|
||
## Categorization | ||
|
||
- Objects | ||
- Structures | ||
|
||
## Changelog | ||
|
||
| Version | Description | | ||
|--------:|-----------------------------------------| | ||
| 3.0.0 | Created | | ||
|
||
*** | ||
See also: | ||
|
||
- [ObjectType](ObjectType.md) | ||
- [Property](Property.md) | ||
- [PropertyExists](PropertyExists.md) | ||
- [PropertyOptional](PropertyOptional.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Validation\Rules; | ||
|
||
use Attribute; | ||
use ReflectionClass; | ||
use Respect\Validation\Message\Template; | ||
use Respect\Validation\Result; | ||
use Respect\Validation\Rule; | ||
use Respect\Validation\Rules\Core\Standard; | ||
|
||
use function array_reduce; | ||
use function is_object; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] | ||
#[Template( | ||
'The properties of {{name}} must be valid', | ||
'The properties of {{name}} must not be valid', | ||
)] | ||
final class Attributes extends Standard | ||
{ | ||
public function evaluate(mixed $input): Result | ||
{ | ||
if (!is_object($input)) { | ||
return Result::failed($input, $this); | ||
} | ||
|
||
$reflection = new ReflectionClass($input); | ||
$properties = $reflection->getProperties(); | ||
$children = []; | ||
foreach ($properties as $property) { | ||
foreach ($property->getAttributes(Rule::class) as $propertyAttribute) { | ||
$children[] = (new Property($property->getName(), $propertyAttribute->newInstance()))->evaluate($input); | ||
} | ||
} | ||
|
||
$isValid = array_reduce($children, static fn (bool $carry, Result $result) => $carry && $result->isValid, true); | ||
|
||
return (new Result($isValid, $input, $this))->withChildren(...$children); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.