1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* trait for custom constraint classes |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\JsonSchemaBundle\Validator\Constraint; |
7
|
|
|
|
8
|
|
|
use JsonSchema\Entity\JsonPointer; |
9
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
13
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
14
|
|
|
* @link http://swisscom.ch |
15
|
|
|
*/ |
16
|
|
|
trait ConstraintTrait |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var EventDispatcherInterface |
21
|
|
|
*/ |
22
|
|
|
private $dispatcher; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* sets the event dispatcher |
26
|
|
|
* |
27
|
|
|
* @param EventDispatcherInterface $dispatcher dispatcher |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function setEventDispatcher(EventDispatcherInterface $dispatcher) |
32
|
|
|
{ |
33
|
|
|
$this->dispatcher = $dispatcher; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* checks the input |
38
|
|
|
* |
39
|
|
|
* @param mixed $element element |
40
|
|
|
* @param null $schema schema |
41
|
|
|
* @param JsonPointer $path path |
42
|
|
|
* @param null $i iterator value |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function check(&$element, $schema = null, JsonPointer $path = null, $i = null) |
47
|
|
|
{ |
48
|
|
|
$eventClass = $this->getEventClass(); |
49
|
|
|
|
50
|
|
|
$event = new $eventClass($this->factory, $element, $schema, $path); |
|
|
|
|
51
|
|
|
$result = $this->dispatcher->dispatch($event::NAME, $event); |
52
|
|
|
|
53
|
|
|
$this->addErrors($result->getErrors()); |
54
|
|
|
|
55
|
|
|
parent::check($element, $schema, $path, $i); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns the name of the Event class for this event |
60
|
|
|
* |
61
|
|
|
* @return string event class name |
62
|
|
|
*/ |
63
|
|
|
abstract public function getEventClass(); |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Adds errors |
67
|
|
|
* |
68
|
|
|
* @param array $errors errors |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
abstract public function addErrors(array $errors); |
73
|
|
|
} |
74
|
|
|
|
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: