Total Complexity | 6 |
Total Lines | 48 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
5 | trait TraitObservable |
||
6 | { |
||
7 | |||
8 | private $listeners = []; |
||
9 | |||
10 | /** |
||
11 | * @param string $eventName |
||
12 | * @param array $eventData |
||
13 | */ |
||
14 | public function trigger(string $eventName, array $eventData) |
||
15 | { |
||
16 | $listeners = $this->listeners[$eventName] ?? []; |
||
17 | foreach ($listeners as $listener) { |
||
18 | \call_user_func($listener[1], $eventData); |
||
19 | } |
||
20 | } |
||
21 | |||
22 | /** |
||
23 | * @param string $eventName |
||
24 | * @param callable $callable |
||
25 | * @param int $priority |
||
26 | */ |
||
27 | public function on(string $eventName, callable $callable, int $priority = 100) |
||
28 | { |
||
29 | if (!isset($this->listeners[$eventName])) { |
||
30 | $this->listeners[$eventName] = []; |
||
31 | } |
||
32 | |||
33 | $this->listeners[$eventName][] = [$priority, $callable]; |
||
34 | \uasort($this->listeners[$eventName], function ($event1, $event2) { |
||
35 | return $event1[0] <=> $event2[0]; |
||
36 | }); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @return array |
||
41 | */ |
||
42 | public function getListeners(): array |
||
43 | { |
||
44 | return $this->listeners; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param array $listeners |
||
49 | */ |
||
50 | public function setListeners(array $listeners) |
||
53 | } |
||
54 | } |
||
55 |