forked from ezsystems/repository-forms
/
UniqueRoleIdentifierValidator.php
58 lines (52 loc) · 1.73 KB
/
UniqueRoleIdentifierValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/**
* This file is part of the eZ RepositoryForms package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
*/
namespace EzSystems\RepositoryForms\Validator\Constraints;
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\RoleService;
use EzSystems\RepositoryForms\Data\Role\RoleData;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class UniqueRoleIdentifierValidator extends ConstraintValidator
{
/**
* @var RoleService
*/
private $roleService;
public function __construct(RoleService $roleService)
{
$this->roleService = $roleService;
}
/**
* Checks if the passed value is valid.
*
* @param mixed $value The value that should be validated
* @param Constraint $constraint The constraint for the validation
*
* @api
*/
public function validate($value, Constraint $constraint)
{
if (!$value instanceof RoleData || $value->identifier === null) {
return;
}
try {
$role = $this->roleService->loadRoleByIdentifier($value->identifier);
// It is of course OK to edit a draft of an existing Role :-)
if ($role->id === $value->roleDraft->id) {
return;
}
$this->context->buildViolation($constraint->message)
->atPath('identifier')
->setParameter('%identifier%', $value->identifier)
->addViolation();
} catch (NotFoundException $e) {
// Do nothing
}
}
}