|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Auth\Form; |
|
6
|
|
|
|
|
7
|
|
|
use App\User\SignupService; |
|
8
|
|
|
use App\User\UserLoginException; |
|
9
|
|
|
use App\User\UserPasswordException; |
|
10
|
|
|
use Yiisoft\Form\FormModel; |
|
11
|
|
|
use Yiisoft\Translator\TranslatorInterface; |
|
12
|
|
|
use Yiisoft\Validator\Result; |
|
13
|
|
|
use Yiisoft\Validator\Rule\Equal; |
|
14
|
|
|
use Yiisoft\Validator\Rule\Required; |
|
15
|
|
|
use Yiisoft\Validator\ValidatorInterface; |
|
16
|
|
|
|
|
17
|
|
|
final class SignupForm extends FormModel |
|
18
|
|
|
{ |
|
19
|
|
|
private string $login = ''; |
|
20
|
|
|
private string $password = ''; |
|
21
|
5 |
|
private string $passwordVerify = ''; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
5 |
|
public function __construct( |
|
24
|
|
|
private SignupService $signupService, |
|
25
|
5 |
|
private ValidatorInterface $validator, |
|
26
|
5 |
|
private TranslatorInterface $translator |
|
27
|
|
|
) { |
|
28
|
|
|
parent::__construct(); |
|
29
|
5 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getAttributeLabels(): array |
|
32
|
5 |
|
{ |
|
33
|
5 |
|
return [ |
|
34
|
5 |
|
'email' => $this->translator->translate('layout.login'), |
|
35
|
|
|
'password' => $this->translator->translate('layout.password'), |
|
36
|
|
|
'passwordVerify' => $this->translator->translate('layout.password-verify'), |
|
37
|
|
|
]; |
|
38
|
5 |
|
} |
|
39
|
|
|
|
|
40
|
5 |
|
public function getFormName(): string |
|
41
|
|
|
{ |
|
42
|
|
|
return 'Signup'; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getLogin(): string |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->login; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getPassword(): string |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->password; |
|
53
|
5 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function signup(): bool |
|
56
|
5 |
|
{ |
|
57
|
5 |
|
if ($this->validator->validate($this)->isValid()) { |
|
58
|
5 |
|
$this->signupService->signup(); |
|
59
|
|
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
return false; |
|
62
|
5 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getRules(): array |
|
65
|
5 |
|
{ |
|
66
|
|
|
return [ |
|
67
|
5 |
|
'login' => fn() => $this->fillLogin(), |
|
68
|
4 |
|
'password' => fn() => $this->fillPassword(), |
|
69
|
4 |
|
'passwordVerify' => [ |
|
70
|
1 |
|
new Required(), |
|
71
|
1 |
|
new Equal( |
|
72
|
|
|
targetValue: $this->password, |
|
73
|
|
|
message: $this->translator->translate('validator.password.not.match') |
|
74
|
4 |
|
), |
|
75
|
1 |
|
], |
|
76
|
|
|
]; |
|
77
|
|
|
} |
|
78
|
4 |
|
|
|
79
|
|
|
private function fillLogin(): Result |
|
80
|
|
|
{ |
|
81
|
|
|
$result = new Result(); |
|
82
|
|
|
try { |
|
83
|
|
|
$this->signupService->setLogin($this->login); |
|
84
|
|
|
} catch (UserLoginException $exception) { |
|
85
|
|
|
$result->addError($exception->getMessage()); |
|
86
|
|
|
} |
|
87
|
|
|
return $result; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function fillPassword(): Result |
|
91
|
|
|
{ |
|
92
|
|
|
$result = new Result(); |
|
93
|
|
|
try { |
|
94
|
|
|
$this->signupService->setPassword($this->password); |
|
95
|
|
|
} catch (UserPasswordException $exception) { |
|
96
|
|
|
$result->addError($exception->getMessage()); |
|
97
|
|
|
} |
|
98
|
|
|
return $result; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|