1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Event |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Event; |
16
|
|
|
|
17
|
|
|
use Phossa2\Event\Message\Message; |
18
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
19
|
|
|
use Phossa2\Event\Interfaces\EventInterface; |
20
|
|
|
use Phossa2\Event\Exception\InvalidArgumentException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Event |
24
|
|
|
* |
25
|
|
|
* Basic implementation of EventInterface with array access to parameters |
26
|
|
|
* |
27
|
|
|
* ```php |
28
|
|
|
* // create event |
29
|
|
|
* $evt = new Event( |
30
|
|
|
* 'login.attempt', // event name |
31
|
|
|
* $this, // event target |
32
|
|
|
* ['username' => 'phossa'] // event parameters |
33
|
|
|
* ); |
34
|
|
|
* |
35
|
|
|
* // get/set event parameter |
36
|
|
|
* if ('phossa' === $evt['username']) { |
37
|
|
|
* $evt['username'] = 'phossa2'; |
38
|
|
|
* } |
39
|
|
|
* |
40
|
|
|
* // stop event |
41
|
|
|
* $evt->stopPropagation(); |
42
|
|
|
* ``` |
43
|
|
|
* |
44
|
|
|
* @package Phossa2\Event |
45
|
|
|
* @author Hong Zhang <[email protected]> |
46
|
|
|
* @see ObjectAbstract |
47
|
|
|
* @see EventInterface |
48
|
|
|
* @see \ArrayAccess |
49
|
|
|
* @version 2.0.0 |
50
|
|
|
* @since 2.0.0 added |
51
|
|
|
* @since 2.1.0 using psr EventInterface now |
52
|
|
|
*/ |
53
|
|
|
class Event extends ObjectAbstract implements EventInterface, \ArrayAccess |
54
|
|
|
{ |
55
|
|
|
/** |
56
|
|
|
* event name |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
* @access protected |
60
|
|
|
*/ |
61
|
|
|
protected $name; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* event target/context |
65
|
|
|
* |
66
|
|
|
* an object OR static class name (string) |
67
|
|
|
* |
68
|
|
|
* @var object|string|null |
69
|
|
|
* @access protected |
70
|
|
|
*/ |
71
|
|
|
protected $target; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* event parameters |
75
|
|
|
* |
76
|
|
|
* @var array |
77
|
|
|
* @access protected |
78
|
|
|
*/ |
79
|
|
|
protected $parameters; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* stop propagation |
83
|
|
|
* |
84
|
|
|
* @var bool |
85
|
|
|
* @access protected |
86
|
|
|
*/ |
87
|
|
|
protected $stopped = false; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Constructor |
91
|
|
|
* |
92
|
|
|
* @param string $eventName event name |
93
|
|
|
* @param string|object|null $target event context, object or classname |
94
|
|
|
* @param array $parameters (optional) event parameters |
95
|
|
|
* @throws InvalidArgumentException if event name is invalid |
96
|
|
|
* @access public |
97
|
|
|
* @api |
98
|
|
|
*/ |
99
|
|
|
public function __construct( |
100
|
|
|
/*# string */ $eventName, |
101
|
|
|
$target = null, |
102
|
|
|
array $parameters = [] |
103
|
|
|
) { |
104
|
|
|
$this->setName($eventName); |
105
|
|
|
$this->setTarget($target); |
106
|
|
|
$this->setParams($parameters); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritDoc} |
111
|
|
|
*/ |
112
|
|
|
public function getName() |
113
|
|
|
{ |
114
|
|
|
return $this->name; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritDoc} |
119
|
|
|
*/ |
120
|
|
|
public function getTarget() |
121
|
|
|
{ |
122
|
|
|
return $this->target; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritDoc} |
127
|
|
|
*/ |
128
|
|
|
public function getParams() |
129
|
|
|
{ |
130
|
|
|
return $this->parameters; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritDoc} |
135
|
|
|
*/ |
136
|
|
|
public function getParam($name) |
137
|
|
|
{ |
138
|
|
|
if (isset($this->parameters[(string) $name])) { |
139
|
|
|
return $this->parameters[(string) $name]; |
140
|
|
|
} |
141
|
|
|
return null; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritDoc} |
146
|
|
|
*/ |
147
|
|
|
public function setName($name) |
148
|
|
|
{ |
149
|
|
|
if (!is_string($name) || empty($name)) { |
150
|
|
|
throw new InvalidArgumentException( |
151
|
|
|
Message::get(Message::EVT_NAME_INVALID, $name), |
152
|
|
|
Message::EVT_NAME_INVALID |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
$this->name = $name; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritDoc} |
160
|
|
|
*/ |
161
|
|
|
public function setTarget($target) |
162
|
|
|
{ |
163
|
|
|
$this->target = $target; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritDoc} |
168
|
|
|
*/ |
169
|
|
|
public function setParams(array $params) |
170
|
|
|
{ |
171
|
|
|
$this->parameters = $params; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* {@inheritDoc} |
176
|
|
|
*/ |
177
|
|
|
public function stopPropagation($flag) |
178
|
|
|
{ |
179
|
|
|
$this->stopped = (bool) $flag; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritDoc} |
184
|
|
|
*/ |
185
|
|
|
public function isPropagationStopped() |
186
|
|
|
{ |
187
|
|
|
return $this->stopped; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritDoc} |
192
|
|
|
*/ |
193
|
|
|
public function offsetExists($offset)/*# : bool */ |
194
|
|
|
{ |
195
|
|
|
return isset($this->parameters[$offset]); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* {@inheritDoc} |
200
|
|
|
*/ |
201
|
|
|
public function offsetGet($offset) |
202
|
|
|
{ |
203
|
|
|
return $this->getParam($offset); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* {@inheritDoc} |
208
|
|
|
*/ |
209
|
|
|
public function offsetSet($offset, $value) |
210
|
|
|
{ |
211
|
|
|
$this->parameters[$offset] = $value; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* {@inheritDoc} |
216
|
|
|
*/ |
217
|
|
|
public function offsetUnset($offset) |
218
|
|
|
{ |
219
|
|
|
unset($this->parameters[$offset]); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|