Completed
Pull Request — develop (#15)
by Narcotic
05:57 queued 02:56
created

ConstraintTrait::getEventClass()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
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
        $eventClass = $this->getEventClass();
73
74
        $event = new $eventClass($this->factory, $element, $schema, $path);
75
        $result = $this->dispatcher->dispatch($event::NAME, $event);
76
77
        $this->addErrors($result->getErrors());
78
    }
79
80
    /**
81
     * Returns the name of the Event class for this event
82
     *
83
     * @return string event class name
84
     */
85
    abstract public function getEventClass();
86
87
    /**
88
     * Adds errors
89
     *
90
     * @param array $errors errors
91
     *
92
     * @return void
93
     */
94
    abstract public function addErrors(array $errors);
95
}
96