1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Webino™ (http://webino.sk) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/webino/event-emitter |
6
|
|
|
* @copyright Copyright (c) 2019 Webino, s.r.o. (http://webino.sk) |
7
|
|
|
* @author Peter Bačinský <[email protected]> |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Webino; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Trait EventTrait |
15
|
|
|
* @package event-emitter |
16
|
|
|
*/ |
17
|
|
|
trait EventTrait |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string Event name. |
21
|
|
|
*/ |
22
|
|
|
protected $name; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var EventEmitterInterface The event target |
26
|
|
|
*/ |
27
|
|
|
protected $target; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var EventResults |
31
|
|
|
*/ |
32
|
|
|
protected $results; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool Whether or not to stop event |
36
|
|
|
*/ |
37
|
|
|
protected $stop = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get event name. |
41
|
|
|
* |
42
|
|
|
* @api |
43
|
|
|
* @return string Event name |
44
|
|
|
*/ |
45
|
|
|
public function getName(): string |
46
|
|
|
{ |
47
|
|
|
if (!$this->name) { |
48
|
|
|
$this->name = get_class($this); |
49
|
|
|
} |
50
|
|
|
return $this->name; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Set event name. |
55
|
|
|
* |
56
|
|
|
* @param string $name Event name |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function setName(string $name): void |
60
|
|
|
{ |
61
|
|
|
$this->name = $name; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get target object from which event was emitted. |
66
|
|
|
* |
67
|
|
|
* @api |
68
|
|
|
* @return EventEmitterInterface Event target object |
69
|
|
|
*/ |
70
|
|
|
public function getTarget(): EventEmitterInterface |
71
|
|
|
{ |
72
|
|
|
return $this->target; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set the event target object. |
77
|
|
|
* |
78
|
|
|
* @param EventEmitterInterface $target Event target object |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
public function setTarget(EventEmitterInterface $target): void |
82
|
|
|
{ |
83
|
|
|
$this->target = $target; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get event value by name. |
88
|
|
|
* |
89
|
|
|
* If the event value does not exist, the default value will be returned. |
90
|
|
|
* |
91
|
|
|
* @api |
92
|
|
|
* @param string $name Value name |
93
|
|
|
* @param mixed $default Default value to return if event value does not exist |
94
|
|
|
* @return mixed Event value |
95
|
|
|
*/ |
96
|
|
|
public function getValue(string $name, $default = null) |
97
|
|
|
{ |
98
|
|
|
return isset($this[$name]) ? $this[$name] : $default; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set event values. |
103
|
|
|
* |
104
|
|
|
* @api |
105
|
|
|
* @param iterable $values |
106
|
|
|
*/ |
107
|
|
|
public function setValues(iterable $values): void |
108
|
|
|
{ |
109
|
|
|
foreach ($values as $index => $value) { |
110
|
|
|
$this[$index] = $value; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set event result value. |
116
|
|
|
* |
117
|
|
|
* Add new result value on top of existing results. |
118
|
|
|
* |
119
|
|
|
* @param mixed $result Event result value |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
public function setResult($result): void |
123
|
|
|
{ |
124
|
|
|
$this->getResults()->add($result); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns event results. |
129
|
|
|
* |
130
|
|
|
* @api |
131
|
|
|
* @return EventResults Event results |
132
|
|
|
*/ |
133
|
|
|
public function getResults(): EventResults |
134
|
|
|
{ |
135
|
|
|
return $this->results ?? new EventResults; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Set event results. |
140
|
|
|
* |
141
|
|
|
* Overwrites results. |
142
|
|
|
* |
143
|
|
|
* @param EventResults|array $results Results array |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
public function setResults($results): void |
147
|
|
|
{ |
148
|
|
|
$this->results = $results instanceof EventResults ? $results : new EventResults($results); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Indicate whether or not to stop propagating this event. |
153
|
|
|
* |
154
|
|
|
* @api |
155
|
|
|
* @param bool $stop Set true to stop propagation |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
|
|
public function stop(bool $stop = true): void |
159
|
|
|
{ |
160
|
|
|
$this->stop = $stop; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Indicates should stop. |
165
|
|
|
* |
166
|
|
|
* @api |
167
|
|
|
* @return bool True when event is stopped |
168
|
|
|
*/ |
169
|
|
|
public function isStopped(): bool |
170
|
|
|
{ |
171
|
|
|
return $this->stop; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|