|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Validator\Constraints; |
|
4
|
|
|
|
|
5
|
|
|
use Silverback\ApiComponentBundle\Entity\ValidComponentInterface; |
|
6
|
|
|
use Silverback\ApiComponentBundle\Validator\ClassNameValidator; |
|
7
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\ComponentLocation as ComponentLocationEntity; |
|
8
|
|
|
use Symfony\Component\Validator\Constraint; |
|
9
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
10
|
|
|
|
|
11
|
|
|
class ComponentLocationValidator extends ConstraintValidator |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @param ComponentLocationEntity $entity |
|
15
|
|
|
* @param Constraint $constraint |
|
16
|
|
|
* @throws \ReflectionException |
|
17
|
|
|
*/ |
|
18
|
3 |
|
public function validate($entity, Constraint $constraint): void |
|
19
|
|
|
{ |
|
20
|
3 |
|
if (!$entity instanceof ComponentLocationEntity) { |
|
|
|
|
|
|
21
|
1 |
|
throw new \InvalidArgumentException( |
|
22
|
1 |
|
sprintf('The ComponentLocationValidator should only be used with %s', ComponentLocationEntity::class) |
|
23
|
|
|
); |
|
24
|
|
|
} |
|
25
|
2 |
|
$content = $entity->getContent(); |
|
26
|
2 |
|
if ($content instanceof ValidComponentInterface) { |
|
27
|
2 |
|
$validComponents = $content->getValidComponents(); |
|
28
|
2 |
|
if ($validComponents->count()) { |
|
29
|
2 |
|
$componentIsValid = false; |
|
30
|
2 |
|
$component = $entity->getComponent(); |
|
31
|
2 |
|
foreach ($validComponents as $validComponent) { |
|
32
|
2 |
|
if (ClassNameValidator::isClassSame($validComponent, $component)) { |
|
33
|
1 |
|
$componentIsValid = true; |
|
34
|
2 |
|
break; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
2 |
|
if (!$componentIsValid) { |
|
38
|
1 |
|
$this->context->buildViolation($constraint->message) |
|
39
|
1 |
|
->atPath('component') |
|
40
|
1 |
|
->setParameter('{{ string }}', implode(', ', $validComponents->toArray())) |
|
41
|
1 |
|
->addViolation(); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
2 |
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|