1
|
|
|
<?php |
2
|
|
|
namespace keeko\core\validator; |
3
|
|
|
|
4
|
|
|
use keeko\framework\validator\ModelValidator; |
5
|
|
|
use keeko\framework\preferences\SystemPreferences; |
6
|
|
|
|
7
|
|
|
class UserValidator extends ModelValidator { |
8
|
|
|
|
9
|
|
|
protected function getValidations() { |
10
|
|
|
$prefs = $this->service->getPreferenceLoader()->getSystemPreferences(); |
11
|
|
|
|
12
|
|
|
$validations = []; |
13
|
|
|
|
14
|
|
|
if ($prefs->getUserLogin() != SystemPreferences::LOGIN_EMAIL) { |
15
|
|
|
$validations['user_name'] = [ |
16
|
|
|
['constraint' => 'notnull'], |
17
|
|
|
['constraint' => 'unique-username'] |
18
|
|
|
]; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
if ($prefs->getUserEmail() || $prefs->getUserLogin() != SystemPreferences::LOGIN_USERNAME) { |
22
|
|
|
$validations['email'] = [['constraint' => 'email']]; |
23
|
|
|
if ($prefs->getUserLogin() != SystemPreferences::LOGIN_USERNAME) { |
24
|
|
|
$validations['email'][] = ['constraint' => 'unique-email']; |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if ($prefs->getUserNames() == SystemPreferences::VALUE_REQUIRED) { |
29
|
|
|
$validations['given_name'] = [['constraint' => 'notnull']]; |
30
|
|
|
$validations['family_name'] = [['constraint' => 'notnull']]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if ($prefs->getUserBirth() == SystemPreferences::VALUE_REQUIRED) { |
34
|
|
|
$validations['birth'] = [['constraint' => 'required']]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ($prefs->getUserSex() == SystemPreferences::VALUE_REQUIRED) { |
38
|
|
|
$validations['sex'] = [['constraint' => 'choice', 'choices' => [0, 1]]]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $validations; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function getCustomConstraints() { |
45
|
|
|
return [ |
46
|
|
|
'unique-username' => 'keeko\core\validator\constraints\UniqueUsername', |
47
|
|
|
'unique-email' => 'keeko\core\validator\constraints\UniqueEmail' |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
} |