|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Platine\App\Validator; |
|
6
|
|
|
|
|
7
|
|
|
use Platine\App\Enum\UserStatus; |
|
8
|
|
|
use Platine\App\Param\UserParam; |
|
9
|
|
|
use Platine\Framework\Form\Validator\AbstractValidator; |
|
10
|
|
|
use Platine\Lang\Lang; |
|
11
|
|
|
use Platine\Validator\Rule\AlphaNumericDash; |
|
12
|
|
|
use Platine\Validator\Rule\Email; |
|
13
|
|
|
use Platine\Validator\Rule\InList; |
|
14
|
|
|
use Platine\Validator\Rule\Matches; |
|
15
|
|
|
use Platine\Validator\Rule\MaxLength; |
|
16
|
|
|
use Platine\Validator\Rule\MinLength; |
|
17
|
|
|
use Platine\Validator\Rule\NotEmpty; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @class UserValidator |
|
21
|
|
|
* @package Platine\App\Validator |
|
22
|
|
|
* @template TEntity as \Platine\Orm\Entity |
|
23
|
|
|
*/ |
|
24
|
|
|
class UserValidator extends AbstractValidator |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* The parameter instance |
|
28
|
|
|
* @var UserParam<TEntity> |
|
29
|
|
|
*/ |
|
30
|
|
|
protected UserParam $param; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Whether password field can be empty (on update page) |
|
34
|
|
|
* @var bool |
|
35
|
|
|
*/ |
|
36
|
|
|
protected bool $ignorePassword; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Create new instance |
|
40
|
|
|
* @param UserParam<TEntity> $param |
|
41
|
|
|
* @param Lang $lang |
|
42
|
|
|
* @param bool $ignorePassword |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(UserParam $param, Lang $lang, bool $ignorePassword = false) |
|
45
|
|
|
{ |
|
46
|
|
|
parent::__construct($lang); |
|
47
|
|
|
$this->param = $param; |
|
48
|
|
|
$this->ignorePassword = $ignorePassword; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function setValidationData(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$this->addData('username', $this->param->getUsername()); |
|
57
|
|
|
$this->addData('lastname', $this->param->getLastname()); |
|
58
|
|
|
$this->addData('firstname', $this->param->getFirstname()); |
|
59
|
|
|
$this->addData('email', $this->param->getEmail()); |
|
60
|
|
|
$this->addData('password', $this->param->getPassword()); |
|
61
|
|
|
$this->addData('password_confirm', $this->param->getPasswordConfirm()); |
|
62
|
|
|
$this->addData('status', $this->param->getStatus()); |
|
63
|
|
|
$this->addData('role', $this->param->getRole()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function setValidationRules(): void |
|
70
|
|
|
{ |
|
71
|
|
|
$this->addRules('username', [ |
|
72
|
|
|
new NotEmpty(), |
|
73
|
|
|
new MinLength(3), |
|
74
|
|
|
new MaxLength(20), |
|
75
|
|
|
new AlphaNumericDash(), |
|
76
|
|
|
]); |
|
77
|
|
|
|
|
78
|
|
|
$this->addRules('lastname', [ |
|
79
|
|
|
new NotEmpty(), |
|
80
|
|
|
new MinLength(2), |
|
81
|
|
|
new MaxLength(30), |
|
82
|
|
|
]); |
|
83
|
|
|
|
|
84
|
|
|
$this->addRules('firstname', [ |
|
85
|
|
|
new NotEmpty(), |
|
86
|
|
|
new MinLength(2), |
|
87
|
|
|
new MaxLength(30), |
|
88
|
|
|
]); |
|
89
|
|
|
|
|
90
|
|
|
$this->addRules('email', [ |
|
91
|
|
|
new NotEmpty(), |
|
92
|
|
|
new Email(), |
|
93
|
|
|
]); |
|
94
|
|
|
|
|
95
|
|
|
$this->addRules('password', [ |
|
96
|
|
|
new MinLength(5), |
|
97
|
|
|
new MaxLength(100), |
|
98
|
|
|
]); |
|
99
|
|
|
|
|
100
|
|
|
$this->addRules('password_confirm', [ |
|
101
|
|
|
new MinLength(5), |
|
102
|
|
|
new MaxLength(100), |
|
103
|
|
|
new Matches('password'), |
|
104
|
|
|
]); |
|
105
|
|
|
|
|
106
|
|
|
if ($this->ignorePassword === false) { |
|
107
|
|
|
$this->addRule('password', new NotEmpty()); |
|
108
|
|
|
$this->addRule('password_confirm', new NotEmpty()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$this->addRules('status', [ |
|
112
|
|
|
new NotEmpty(), |
|
113
|
|
|
new InList([UserStatus::ACTIVE, UserStatus::LOCKED]), |
|
114
|
|
|
]); |
|
115
|
|
|
|
|
116
|
|
|
$this->addRules('role', [ |
|
117
|
|
|
new MinLength(2), |
|
118
|
|
|
new MaxLength(50), |
|
119
|
|
|
]); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|