1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenTribes\Core\Silex\Validator; |
4
|
|
|
|
5
|
|
|
use OpenTribes\Core\Validator\RegistrationValidator; |
6
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
7
|
|
|
use Symfony\Component\Validator\Validator; |
8
|
|
|
use Symfony\Component\Validator\Constraints; |
9
|
|
|
|
10
|
|
|
class SymfonyRegistrationValidator extends RegistrationValidator |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Validator |
14
|
|
|
*/ |
15
|
|
|
private $validator; |
16
|
|
|
|
17
|
15 |
|
public function __construct(Validator\RecursiveValidator $validator) |
18
|
|
|
{ |
19
|
15 |
|
$this->validator = $validator; |
|
|
|
|
20
|
15 |
|
} |
21
|
|
|
|
22
|
14 |
|
protected function validate() |
23
|
|
|
{ |
24
|
14 |
|
$constraint = new Constraints\Collection( |
25
|
|
|
[ |
26
|
|
|
|
27
|
14 |
|
'isUniqueEmail' => new Constraints\IsTrue(['message' => 'Email exists']), |
28
|
14 |
|
'isUniqueUsername' => new Constraints\IsTrue(['message' => 'Username exists']), |
29
|
14 |
|
'termsAndConditions' => new Constraints\IsTrue(['message' => 'Terms and Conditions are not accepted']), |
30
|
|
|
'username' => [ |
31
|
14 |
|
new Constraints\NotBlank(['message' => 'Username is empty']), |
32
|
14 |
|
new Constraints\Length([ |
33
|
14 |
|
'min' => 3, |
34
|
14 |
|
'max' => 20, |
35
|
14 |
|
'minMessage' => 'Username is too short', |
36
|
|
|
'maxMessage' => 'Username is too long' |
37
|
14 |
|
]), |
38
|
14 |
|
new Constraints\Regex([ |
39
|
14 |
|
'pattern' => '/^[-a-z0-9_]++$/iD', |
40
|
|
|
'message' => 'Username contains invalid character' |
41
|
14 |
|
]) |
42
|
14 |
|
], |
43
|
|
|
'email' => [ |
44
|
14 |
|
new Constraints\NotBlank(['message' => 'Email is empty']), |
45
|
14 |
|
new Constraints\Email(['message' => 'Email is invalid']) |
46
|
14 |
|
], |
47
|
|
|
'password' => [ |
48
|
14 |
|
new Constraints\NotBlank(['message' => 'Password is empty']), |
49
|
14 |
|
new Constraints\Length(['min' => 6, 'minMessage' => 'Password is too short']), |
50
|
14 |
|
], |
51
|
|
|
'passwordConfirm' => [ |
52
|
14 |
|
new Constraints\EqualTo(['value' => $this->password, 'message' => 'Password confirm not match']) |
53
|
14 |
|
], |
54
|
|
|
'emailConfirm' => [ |
55
|
14 |
|
new Constraints\EqualTo(['value' => $this->email, 'message' => 'Email confirm not match']) |
56
|
14 |
|
] |
57
|
14 |
|
]); |
58
|
|
|
|
59
|
|
|
$value = [ |
60
|
14 |
|
'username' => $this->username, |
61
|
14 |
|
'password' => $this->password, |
62
|
14 |
|
'isUniqueEmail' => !$this->emailExists, |
63
|
14 |
|
'isUniqueUsername' => !$this->usernameExists, |
64
|
14 |
|
'termsAndConditions' => $this->acceptedTerms, |
65
|
14 |
|
'email' => $this->email, |
66
|
14 |
|
'passwordConfirm' => $this->passwordConfirm, |
67
|
14 |
|
'emailConfirm' => $this->emailConfirm |
68
|
14 |
|
]; |
69
|
14 |
|
$result = $this->validator->validate($value, $constraint); |
|
|
|
|
70
|
14 |
|
if ($result instanceof ConstraintViolationList) { |
71
|
14 |
|
foreach ($result->getIterator() as $constraintViolation) { |
72
|
13 |
|
$this->addError($constraintViolation->getMessage()); |
73
|
14 |
|
} |
74
|
14 |
|
} |
75
|
|
|
|
76
|
14 |
|
} |
77
|
|
|
|
78
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..