|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OpenTribes\Core\Silex\Validator; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use OpenTribes\Core\Validator\LoginValidator; |
|
7
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
|
8
|
|
|
use Symfony\Component\Validator\Validator; |
|
9
|
|
|
use Symfony\Component\Validator\Constraints; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class SymfonyLoginValidator extends LoginValidator |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Validator |
|
16
|
|
|
*/ |
|
17
|
|
|
private $validator; |
|
18
|
|
|
|
|
19
|
10 |
|
public function __construct(Validator\RecursiveValidator $validator) |
|
20
|
|
|
{ |
|
21
|
10 |
|
$this->validator = $validator; |
|
|
|
|
|
|
22
|
10 |
|
} |
|
23
|
|
|
|
|
24
|
8 |
|
protected function validate() |
|
25
|
|
|
{ |
|
26
|
8 |
|
$constraint = new Constraints\Collection( |
|
27
|
|
|
[ |
|
28
|
|
|
|
|
29
|
|
|
'username' => [ |
|
30
|
8 |
|
new Constraints\NotBlank(array('message' => 'Username is empty')), |
|
31
|
8 |
|
new Constraints\Length(array( |
|
32
|
8 |
|
'min' => 3, |
|
33
|
8 |
|
'max' => 20, |
|
34
|
8 |
|
'minMessage' => 'Username is too short', |
|
35
|
|
|
'maxMessage' => 'Username is too long' |
|
36
|
8 |
|
)), |
|
37
|
8 |
|
new Constraints\Regex(array( |
|
38
|
8 |
|
'pattern' => '/^[-a-z0-9_]++$/iD', |
|
39
|
|
|
'message' => 'Username contains invalid character' |
|
40
|
8 |
|
)) |
|
41
|
8 |
|
] |
|
42
|
8 |
|
, |
|
43
|
|
|
'password' => [ |
|
44
|
8 |
|
new Constraints\NotBlank(array('message' => 'Password is empty')), |
|
45
|
8 |
|
new Constraints\Length(array('min' => 6, 'minMessage' => 'Password is too short')), |
|
46
|
|
|
|
|
47
|
8 |
|
], |
|
48
|
|
|
'verified' => [ |
|
49
|
8 |
|
new Constraints\IsTrue(['message' => 'Invalid login']) |
|
50
|
8 |
|
] |
|
51
|
8 |
|
]); |
|
52
|
|
|
|
|
53
|
|
|
$value = [ |
|
54
|
8 |
|
'username' => $this->username, |
|
55
|
8 |
|
'password' => $this->password, |
|
56
|
8 |
|
'verified' => $this->verified |
|
57
|
8 |
|
]; |
|
58
|
8 |
|
$result = $this->validator->validate($value, $constraint); |
|
|
|
|
|
|
59
|
8 |
|
if ($result instanceof ConstraintViolationList) { |
|
60
|
8 |
|
foreach ($result->getIterator() as $constraintViolation) { |
|
61
|
7 |
|
$this->addError($constraintViolation->getMessage()); |
|
62
|
8 |
|
} |
|
63
|
8 |
|
} |
|
64
|
|
|
|
|
65
|
8 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
} |
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..