1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Event Dispatcher |
5
|
|
|
* |
6
|
|
|
* Platine Event Dispatcher is the minimal implementation of PSR 14 |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine Event Dispatcher |
11
|
|
|
* |
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
14
|
|
|
* in the Software without restriction, including without limitation the rights |
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
17
|
|
|
* furnished to do so, subject to the following conditions: |
18
|
|
|
* |
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
20
|
|
|
* copies or substantial portions of the Software. |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28
|
|
|
* SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @file Event.php |
33
|
|
|
* |
34
|
|
|
* The Event class used to contains information about event to dispatch to |
35
|
|
|
* listeners or subscribers |
36
|
|
|
* |
37
|
|
|
* @package Platine\Event |
38
|
|
|
* @author Platine Developers Team |
39
|
|
|
* @copyright Copyright (c) 2020 |
40
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
41
|
|
|
* @link https://www.platine-php.com |
42
|
|
|
* @version 1.0.0 |
43
|
|
|
* @filesource |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
declare(strict_types=1); |
47
|
|
|
|
48
|
|
|
namespace Platine\Event; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @class Event |
52
|
|
|
* @package Platine\Event |
53
|
|
|
*/ |
54
|
|
|
class Event implements EventInterface |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* Whether the propagation is stopped. |
58
|
|
|
* @var boolean |
59
|
|
|
*/ |
60
|
|
|
protected bool $stopPropagation = false; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Create the new instance of the Event |
64
|
|
|
* @param string $name the event name |
65
|
|
|
* @param array<string, mixed> $arguments the event data |
66
|
|
|
*/ |
67
|
|
|
public function __construct(protected string $name, protected array $arguments = []) |
68
|
|
|
{ |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function getName(): string |
75
|
|
|
{ |
76
|
|
|
return $this->name; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set the event name. |
81
|
|
|
* |
82
|
|
|
* @param string $name the new event name |
83
|
|
|
* |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
|
|
public function setName(string $name): self |
87
|
|
|
{ |
88
|
|
|
$this->name = $name; |
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get all event data or arguments. |
94
|
|
|
* @return array<string, mixed> |
95
|
|
|
*/ |
96
|
|
|
public function getArguments(): array |
97
|
|
|
{ |
98
|
|
|
return $this->arguments; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set array of arguments. |
103
|
|
|
* |
104
|
|
|
* @param array<string, mixed> $arguments |
105
|
|
|
* |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function setArguments(array $arguments): self |
109
|
|
|
{ |
110
|
|
|
$this->arguments = $arguments; |
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get event data for the given key. |
116
|
|
|
* @param string $key |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
|
|
public function getArgument(string $key): mixed |
120
|
|
|
{ |
121
|
|
|
return array_key_exists($key, $this->arguments) |
122
|
|
|
? $this->arguments[$key] |
123
|
|
|
: null; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Set event data for the given key. |
128
|
|
|
* |
129
|
|
|
* @param string $key |
130
|
|
|
* @param mixed $value the event value |
131
|
|
|
* |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
public function setArgument(string $key, mixed $value): self |
135
|
|
|
{ |
136
|
|
|
$this->arguments[$key] = $value; |
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
|
|
public function isStopPropagation(): bool |
144
|
|
|
{ |
145
|
|
|
return $this->stopPropagation; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
|
|
public function stopPropagation(): self |
152
|
|
|
{ |
153
|
|
|
$this->stopPropagation = true; |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|