|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the StfalconApiBundle. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Stfalcon LLC <stfalcon.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace StfalconStudio\ApiBundle\Validator\Constraints\Password; |
|
14
|
|
|
|
|
15
|
|
|
use StfalconStudio\ApiBundle\Exception\Validator\UnexpectedConstraintException; |
|
16
|
|
|
use StfalconStudio\ApiBundle\Util\PasswordRequirementsValidator; |
|
17
|
|
|
use Symfony\Component\Validator\Constraint; |
|
18
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* PasswordMeetSpecialRequirementsValidator. |
|
22
|
|
|
*/ |
|
23
|
|
|
class PasswordMeetSpecialRequirementsValidator extends ConstraintValidator |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var PasswordRequirementsValidator */ |
|
26
|
|
|
private $passwordValidator; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param PasswordRequirementsValidator $passwordValidator |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(PasswordRequirementsValidator $passwordValidator) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->passwordValidator = $passwordValidator; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string|null $password |
|
38
|
|
|
* @param Constraint $constraint |
|
39
|
|
|
* |
|
40
|
|
|
* @throws UnexpectedConstraintException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function validate($password, Constraint $constraint): void |
|
43
|
|
|
{ |
|
44
|
|
|
if (!$constraint instanceof PasswordMeetSpecialRequirements) { |
|
45
|
|
|
throw new UnexpectedConstraintException($constraint, PasswordMeetSpecialRequirements::class); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (!empty($password) && !$this->passwordValidator->isValid($password)) { |
|
49
|
|
|
$this->context |
|
50
|
|
|
->buildViolation($constraint->message) |
|
51
|
|
|
->setCode(PasswordMeetSpecialRequirements::PASSWORD_DOES_NOT_MEET_SPECIAL_REQUIREMENTS) |
|
52
|
|
|
->addViolation() |
|
53
|
|
|
; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|