|
3
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the FOSUserBundle package. |
|
|
5 |
* |
|
|
6 |
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace FOS\UserBundle\Validator; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\Validator\Constraint; |
|
|
15 |
use Symfony\Component\Validator\ConstraintValidator; |
|
|
16 |
use FOS\UserBundle\Model\UserManagerInterface; |
|
|
17 |
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
|
|
18 |
use Symfony\Component\Validator\Exception\ValidatorException; |
|
|
19 |
|
|
|
20 |
/** |
|
|
21 |
* UniqueValidator |
|
|
22 |
*/ |
|
|
23 |
class UniqueValidator extends ConstraintValidator |
|
|
24 |
{ |
|
|
25 |
/** |
|
|
26 |
* @var UserManagerInterface |
|
|
27 |
*/ |
|
|
28 |
protected $userManager; |
|
|
29 |
|
|
|
30 |
/** |
|
|
31 |
* Constructor |
|
|
32 |
* |
|
|
33 |
* @param UserManagerInterface $userManager |
|
|
34 |
*/ |
|
|
35 |
public function __construct(UserManagerInterface $userManager) |
|
|
36 |
{ |
|
|
37 |
$this->userManager = $userManager; |
|
|
38 |
} |
|
|
39 |
|
|
|
40 |
/** |
|
|
41 |
* Sets the user manager |
|
|
42 |
* |
|
|
43 |
* @param UserManagerInterface $userManager |
|
|
44 |
*/ |
|
|
45 |
public function setUserManager(UserManagerInterface $userManager) |
|
|
46 |
{ |
|
|
47 |
$this->userManager = $userManager; |
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
/** |
|
|
51 |
* Gets the user manager |
|
|
52 |
* |
|
|
53 |
* @return UserManagerInterface |
|
|
54 |
*/ |
|
|
55 |
public function getUserManager() |
|
|
56 |
{ |
|
|
57 |
return $this->userManager; |
|
|
58 |
} |
|
|
59 |
|
|
|
60 |
/** |
|
|
61 |
* Indicates whether the constraint is valid |
|
|
62 |
* |
|
|
63 |
* @param Entity $value |
|
|
64 |
* @param Constraint $constraint |
|
|
65 |
*/ |
|
|
66 |
public function isValid($value, Constraint $constraint) |
|
|
67 |
{ |
|
|
68 |
if (!$this->getUserManager()->validateUnique($value, $constraint)) { |
|
|
69 |
$this->setMessage($constraint->message, array( |
|
|
70 |
'%property%' => $constraint->property |
|
|
71 |
)); |
|
|
72 |
return false; |
|
|
73 |
} |
|
|
74 |
|
|
|
75 |
return true; |
|
|
76 |
} |
|
|
77 |
} |