Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
22 / 22 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| Account | |
100.00% |
22 / 22 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
2 | |||
| validate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Validation; |
| 6 | |
| 7 | use Celema\Sire\Contract\Validator; |
| 8 | use Celema\Sire\Result; |
| 9 | use Celema\Sire\Shape; |
| 10 | use Override; |
| 11 | |
| 12 | final class Account implements Validator |
| 13 | { |
| 14 | private Shape $shape; |
| 15 | |
| 16 | public function __construct(bool $passwordRequired) |
| 17 | { |
| 18 | $this->shape = new Shape()->messages(Validators::messages()); |
| 19 | $this->shape |
| 20 | ->add('email', 'string') |
| 21 | ->rules('required', 'email', 'maxlen:254') |
| 22 | ->label(__('user:email')); |
| 23 | // Without an @, so a username can never equal another account's email: |
| 24 | // the login form matches a login against both. |
| 25 | $this->shape |
| 26 | ->add('username', 'string') |
| 27 | ->rules('maxlen:64', 'regex:/^[^@\s]+$/') |
| 28 | ->optional() |
| 29 | ->nullable() |
| 30 | ->label(__('user:username')); |
| 31 | $this->shape |
| 32 | ->add('name', 'string') |
| 33 | ->rules('maxlen:200') |
| 34 | ->optional() |
| 35 | ->nullable() |
| 36 | ->label(__('user:name')); |
| 37 | // Its length and strength are PasswordPolicy's to judge. |
| 38 | $password = $this->shape->add('password', 'string')->label(__('user:password')); |
| 39 | |
| 40 | if ($passwordRequired) { |
| 41 | $password->rules('required'); |
| 42 | } else { |
| 43 | $password->optional()->nullable(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | #[Override] |
| 48 | public function validate(array $data): Result |
| 49 | { |
| 50 | return $this->shape->validate($data); |
| 51 | } |
| 52 | } |