|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* custom format constraint |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\JsonSchemaBundle\Validator\Constraint; |
|
7
|
|
|
|
|
8
|
|
|
use JsonSchema\Constraints\Factory; |
|
|
|
|
|
|
9
|
|
|
use JsonSchema\Constraints\FormatConstraint; |
|
10
|
|
|
use Graviton\JsonSchemaBundle\Validator\Constraint\Event\ConstraintEventFormat; |
|
11
|
|
|
use JsonSchema\Uri\UriRetriever; |
|
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
16
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
17
|
|
|
* @link http://swisscom.ch |
|
18
|
|
|
*/ |
|
19
|
|
|
class Format extends FormatConstraint |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var EventDispatcherInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $dispatcher; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Factory |
|
29
|
|
|
*/ |
|
30
|
|
|
private $factory; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Format constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param int $checkMode check mode |
|
36
|
|
|
* @param UriRetriever|null $uriRetriever uri retriever |
|
37
|
|
|
* @param Factory|null $factory factory |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct( |
|
40
|
|
|
$checkMode = self::CHECK_MODE_NORMAL, |
|
41
|
|
|
UriRetriever $uriRetriever = null, |
|
42
|
|
|
Factory $factory = null |
|
43
|
|
|
) { |
|
44
|
|
|
parent::__construct($checkMode, $uriRetriever, $factory); |
|
45
|
|
|
$this->factory = $factory; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* sets the event dispatcher |
|
50
|
|
|
* |
|
51
|
|
|
* @param EventDispatcherInterface $dispatcher dispatcher |
|
52
|
|
|
* |
|
53
|
|
|
* @return void |
|
54
|
|
|
*/ |
|
55
|
|
|
public function setEventDispatcher(EventDispatcherInterface $dispatcher) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->dispatcher = $dispatcher; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* checks the input |
|
62
|
|
|
* |
|
63
|
|
|
* @param mixed $element element |
|
64
|
|
|
* @param null $schema schema |
|
65
|
|
|
* @param null $path path |
|
66
|
|
|
* @param null $i iterator value |
|
67
|
|
|
* |
|
68
|
|
|
* @return void |
|
69
|
|
|
*/ |
|
70
|
|
|
public function check($element, $schema = null, $path = null, $i = null) |
|
71
|
|
|
{ |
|
72
|
|
|
parent::check($element, $schema, $path, $i); |
|
73
|
|
|
|
|
74
|
|
|
$event = new ConstraintEventFormat($this->factory, $element, $schema, $path); |
|
75
|
|
|
$result = $this->dispatcher->dispatch(ConstraintEventFormat::NAME, $event); |
|
76
|
|
|
|
|
77
|
|
|
$this->addErrors($result->getErrors()); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: