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 https://opensource.org/licenses/MIT MIT 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 $properties properties |
43
|
|
|
* @param null $additionalProp added props |
44
|
|
|
* @param null $patternProperties pattern props |
45
|
|
|
* @param array $appliedDefaults applied defaults |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function check( |
50
|
|
|
&$element, |
51
|
|
|
$schema = null, |
52
|
|
|
JsonPointer $path = null, |
53
|
|
|
$properties = null, |
54
|
|
|
$additionalProp = null, |
55
|
|
|
$patternProperties = null, |
56
|
|
|
$appliedDefaults = array() |
57
|
|
|
) { |
58
|
|
|
$eventClass = $this->getEventClass(); |
59
|
|
|
|
60
|
|
|
$event = new $eventClass($this->factory, $element, $schema, $path); |
|
|
|
|
61
|
|
|
$result = $this->dispatcher->dispatch($event, $event::NAME); |
62
|
|
|
|
63
|
|
|
$this->addErrors($result->getErrors()); |
64
|
|
|
|
65
|
|
|
parent::check($element, $schema, $path, $properties, $additionalProp, $patternProperties, $appliedDefaults); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the name of the Event class for this event |
70
|
|
|
* |
71
|
|
|
* @return string event class name |
72
|
|
|
*/ |
73
|
|
|
abstract public function getEventClass(); |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Adds errors |
77
|
|
|
* |
78
|
|
|
* @param array $errors errors |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
abstract public function addErrors(array $errors); |
83
|
|
|
} |
84
|
|
|
|
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: