@@ 11-62 (lines=52) @@ | ||
8 | use Symfony\Component\Validator\Constraint; |
|
9 | use Symfony\Component\Validator\ConstraintValidator; |
|
10 | ||
11 | class IdDoesNotExistConstraintValidator extends ConstraintValidator |
|
12 | { |
|
13 | /** |
|
14 | * @var ObjectManager |
|
15 | */ |
|
16 | private $em; |
|
17 | ||
18 | /** |
|
19 | * @param ObjectManager $em |
|
20 | */ |
|
21 | public function __construct(ObjectManager $em) |
|
22 | { |
|
23 | $this->em = $em; |
|
24 | } |
|
25 | ||
26 | /** |
|
27 | * @param mixed $identifier |
|
28 | * @param Constraint $constraint |
|
29 | * @throws \ReflectionException |
|
30 | */ |
|
31 | public function validate($identifier, Constraint $constraint) |
|
32 | { |
|
33 | if ($identifier === null || !is_string($identifier)) { |
|
34 | return; |
|
35 | } |
|
36 | ||
37 | if (!class_exists($constraint->getFqcn())) { |
|
38 | throw new \InvalidArgumentException(sprintf( |
|
39 | 'Constraint FQCN [%s] does not exist', |
|
40 | $constraint->getFqcn() |
|
41 | )); |
|
42 | } |
|
43 | ||
44 | if ($this->getRepository($constraint->getFqcn())->find($identifier) !== null) { |
|
45 | $this->context->buildViolation($constraint->getMessage()) |
|
46 | ->setParameters([ |
|
47 | '%className%' => (new \ReflectionClass($constraint->getFqcn()))->getShortName(), |
|
48 | '%id%' => AppGlobalId::toGlobalId((new \ReflectionClass($constraint->getFqcn()))->getShortName(), $identifier), |
|
49 | ]) |
|
50 | ->addViolation(); |
|
51 | } |
|
52 | } |
|
53 | ||
54 | /** |
|
55 | * @param string $fqcn |
|
56 | * @return EntityRepository |
|
57 | */ |
|
58 | protected function getRepository(string $fqcn): EntityRepository |
|
59 | { |
|
60 | return $this->em->getRepository($fqcn); |
|
61 | } |
|
62 | } |
|
63 |
@@ 11-62 (lines=52) @@ | ||
8 | use Symfony\Component\Validator\Constraint; |
|
9 | use Symfony\Component\Validator\ConstraintValidator; |
|
10 | ||
11 | class IdExistConstraintValidator extends ConstraintValidator |
|
12 | { |
|
13 | /** |
|
14 | * @var ObjectManager |
|
15 | */ |
|
16 | private $em; |
|
17 | ||
18 | /** |
|
19 | * @param ObjectManager $em |
|
20 | */ |
|
21 | public function __construct(ObjectManager $em) |
|
22 | { |
|
23 | $this->em = $em; |
|
24 | } |
|
25 | ||
26 | /** |
|
27 | * @param mixed $identifier |
|
28 | * @param Constraint $constraint |
|
29 | * @throws \ReflectionException |
|
30 | */ |
|
31 | public function validate($identifier, Constraint $constraint) |
|
32 | { |
|
33 | if ($identifier === null || !is_string($identifier)) { |
|
34 | return; |
|
35 | } |
|
36 | ||
37 | if (!class_exists($constraint->getFqcn())) { |
|
38 | throw new \InvalidArgumentException(sprintf( |
|
39 | 'Constraint FQCN [%s] does not exist', |
|
40 | $constraint->getFqcn() |
|
41 | )); |
|
42 | } |
|
43 | ||
44 | if ($this->getRepository($constraint->getFqcn())->find($identifier) === null) { |
|
45 | $this->context->buildViolation($constraint->getMessage()) |
|
46 | ->setParameters([ |
|
47 | '%className%' => (new \ReflectionClass($constraint->getFqcn()))->getShortName(), |
|
48 | '%id%' => AppGlobalId::toGlobalId((new \ReflectionClass($constraint->getFqcn()))->getShortName(), $identifier), |
|
49 | ]) |
|
50 | ->addViolation(); |
|
51 | } |
|
52 | } |
|
53 | ||
54 | /** |
|
55 | * @param string $fqcn |
|
56 | * @return EntityRepository |
|
57 | */ |
|
58 | protected function getRepository(string $fqcn): EntityRepository |
|
59 | { |
|
60 | return $this->em->getRepository($fqcn); |
|
61 | } |
|
62 | } |
|
63 |