Completed
Branch master (c4b56e)
by Andrey
03:02 queued 01:04
created

Event::setParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Event;
4
5
use Psr\EventManager\EventInterface;
6
use Exception;
7
8
class Event implements EventInterface
9
{
10
    private $name;
11
    private $params = [];
12
    private $target;
13
    private $isPropagationStopped = false;
14
15
    /**
16
     * @param string $name 
17
     * @param array $params
18
     * @param object | string $target
19
     * @param bool $isPropagationStopped
20
     */
21
    public function __construct(
22
        $name,
23
        $params = [],
24
        $target = null,
25
        $isPropagationStopped = false
26
    )
27
    {
28
        $this->setName($name);
29
        $this->setParams($params);
30
        $this->setTarget($target);
31
        $this->stopPropagation($isPropagationStopped);
32
    }
33
34
    /**
35
     * Get event name
36
     *
37
     * @return string
38
     */
39
    public function getName()
40
    {
41
        return $this->name;
42
    }
43
44
    /**
45
     * Get target/context from which event was triggered
46
     *
47
     * @return null|string|object
48
     */
49
    public function getTarget()
50
    {
51
        return $this->target;
52
    }
53
54
    /**
55
     * Get parameters passed to the event
56
     *
57
     * @return array
58
     */
59
    public function getParams()
60
    {
61
        return $this->params;
62
    }
63
64
    /**
65
     * Get a single parameter by name
66
     *
67
     * @param  string $name
68
     * @return mixed
69
     */
70
    public function getParam($name)
71
    {
72
        return $this->params[$name];
73
    }
74
75
    /**
76
     * Set the event name
77
     *
78
     * @param  string $name
79
     * @return void
80
     */
81
    public function setName($name)
82
    {
83
        if (!$this->validateName($name)) {
84
            throw new Exception("Invalid event name format (Event name MUST contain \"A-z\", \"0-9\", \"_\", \".\"", 1);
85
        }
86
87
        $this->name = $name;
88
    }
89
90
    /**
91
     * Set the event target
92
     *
93
     * @param  null|string|object $target
94
     * @return void
95
     */
96
    public function setTarget($target)
97
    {
98
        $this->target = $target;
99
    }
100
101
    /**
102
     * Set event parameters
103
     *
104
     * @param  array $params
105
     * @return void
106
     */
107
    public function setParams(array $params)
108
    {
109
        $this->params = $params;
110
    }
111
112
    /**
113
     * Indicate whether or not to stop propagating this event
114
     *
115
     * @param  bool $flag
116
     * @return void
117
     */
118
    public function stopPropagation($flag = true)
119
    {
120
        $this->isPropagationStopped = $flag;
121
    }
122
123
    /**
124
     * Has this event indicated event propagation should stop?
125
     *
126
     * @return bool
127
     */
128
    public function isPropagationStopped()
129
    {
130
        return $this->isPropagationStopped;
131
    }
132
133
    /**
134
     * Validate name with PSR specification
135
     * 
136
     * @param string $name
137
     * @return bool
138
     */
139
    private function validateName($name)
140
    {
141
        if (preg_match_all('/[^(A-z0-9_.)]/', $name, $matches, PREG_SET_ORDER, 0)) {
142
            return false;
143
        }
144
145
        return true;
146
    }
147
}