1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\Validator\Constraints; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Api\IriConverterInterface; |
17
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\ComponentPosition; |
|
|
|
|
18
|
|
|
use Silverback\ApiComponentsBundle\Validator\Constraints\ComponentPosition as ComponentPositionConstraint; |
19
|
|
|
use Symfony\Component\Validator\Constraint; |
20
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Daniel West <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class ComponentPositionValidator extends ConstraintValidator |
26
|
|
|
{ |
27
|
|
|
private IriConverterInterface $iriConverter; |
28
|
|
|
|
29
|
|
|
public function __construct(IriConverterInterface $iriConverter) |
30
|
|
|
{ |
31
|
|
|
$this->iriConverter = $iriConverter; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ComponentPosition $componentPosition |
36
|
|
|
* @param ComponentPositionConstraint $constraint |
37
|
|
|
*/ |
38
|
|
|
public function validate($componentPosition, Constraint $constraint): void |
39
|
|
|
{ |
40
|
|
|
$collection = $componentPosition->componentCollection; |
41
|
|
|
if (!$collection) { |
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
$component = $componentPosition->component; |
45
|
|
|
if (!$component) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$iri = $this->iriConverter->getIriFromResourceClass(\get_class($component)); |
50
|
|
|
|
51
|
|
|
if ($allowedComponents = $collection->allowedComponents) { |
52
|
|
|
if (!$allowedComponents->contains($iri)) { |
53
|
|
|
$this->context->buildViolation($constraint->message) |
54
|
|
|
->setParameter('{{ iri }}', $iri) |
55
|
|
|
->setParameter('{{ reference }}', $collection->reference) |
|
|
|
|
56
|
|
|
->setParameter('{{ allowed }}', implode(',', $allowedComponents->toArray())) |
57
|
|
|
->addViolation(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($componentPosition->component->isPositionRestricted()) { |
64
|
|
|
$this->context->buildViolation($constraint->restrictedMessage) |
65
|
|
|
->setParameter('{{ iri }}', $iri) |
66
|
|
|
->setParameter('{{ reference }}', $collection->reference) |
67
|
|
|
->addViolation(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: