1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* trait for custom constraint classes |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\JsonSchemaBundle\Validator\Constraint; |
7
|
|
|
|
8
|
|
|
use JsonSchema\Constraints\Factory as CustomFactory; |
9
|
|
|
use JsonSchema\Uri\UriRetriever; |
10
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
14
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
15
|
|
|
* @link http://swisscom.ch |
16
|
|
|
*/ |
17
|
|
|
trait ConstraintTrait |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var EventDispatcherInterface |
22
|
|
|
*/ |
23
|
|
|
private $dispatcher; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var CustomFactory |
27
|
|
|
*/ |
28
|
|
|
private $factory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Format constructor. |
32
|
|
|
* |
33
|
|
|
* @param int $checkMode check mode |
34
|
|
|
* @param UriRetriever|null $uriRetriever uri retriever |
35
|
|
|
* @param CustomFactory|null $factory factory |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
$checkMode = self::CHECK_MODE_NORMAL, |
39
|
|
|
UriRetriever $uriRetriever = null, |
40
|
|
|
CustomFactory $factory = null |
41
|
|
|
) { |
42
|
|
|
parent::__construct($checkMode, $uriRetriever, $factory); |
43
|
|
|
$this->factory = $factory; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* sets the event dispatcher |
48
|
|
|
* |
49
|
|
|
* @param EventDispatcherInterface $dispatcher dispatcher |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function setEventDispatcher(EventDispatcherInterface $dispatcher) |
54
|
|
|
{ |
55
|
|
|
$this->dispatcher = $dispatcher; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* checks the input |
60
|
|
|
* |
61
|
|
|
* @param mixed $element element |
62
|
|
|
* @param null $schema schema |
63
|
|
|
* @param null $path path |
64
|
|
|
* @param null $i iterator value |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
public function check($element, $schema = null, $path = null, $i = null) |
69
|
|
|
{ |
70
|
|
|
parent::check($element, $schema, $path, $i); |
71
|
|
|
|
72
|
|
|
$event = new $this->eventClass($this->factory, $element, $schema, $path); |
|
|
|
|
73
|
|
|
$result = $this->dispatcher->dispatch($event::NAME, $event); |
74
|
|
|
|
75
|
|
|
$this->addErrors($result->getErrors()); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: