Completed
Push — master ( 6565fb...b207da )
by Thomas
10:01
created

UserValidator::getCustomConstraints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
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
}