forked from Webklex/php-imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HasEvents.php
77 lines (67 loc) · 1.46 KB
/
HasEvents.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/*
* File: HasEvents.php
* Category: -
* Author: M.Goldenbaum
* Created: 21.09.20 22:46
* Updated: -
*
* Description:
* -
*/
namespace Webklex\PHPIMAP\Traits;
use Webklex\PHPIMAP\Events\Event;
use Webklex\PHPIMAP\Exceptions\EventNotFoundException;
/**
* Trait HasEvents
*
* @package Webklex\PHPIMAP\Traits
*/
trait HasEvents {
/**
* Event holder
*
* @var array $events
*/
protected array $events = [];
/**
* Set a specific event
* @param string $section
* @param string $event
* @param mixed $class
*/
public function setEvent(string $section, string $event, mixed $class): void {
if (isset($this->events[$section])) {
$this->events[$section][$event] = $class;
}
}
/**
* Set all events
* @param array $events
*/
public function setEvents(array $events): void {
$this->events = $events;
}
/**
* Get a specific event callback
* @param string $section
* @param string $event
*
* @return Event|string
* @throws EventNotFoundException
*/
public function getEvent(string $section, string $event): Event|string {
if (isset($this->events[$section])) {
return $this->events[$section][$event];
}
throw new EventNotFoundException();
}
/**
* Get all events
*
* @return array
*/
public function getEvents(): array {
return $this->events;
}
}