1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* event (https://github.com/phpgears/event). |
5
|
|
|
* Event handling. |
6
|
|
|
* |
7
|
|
|
* @license MIT |
8
|
|
|
* @link https://github.com/phpgears/event |
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Gears\Event; |
15
|
|
|
|
16
|
|
|
use Gears\Event\Exception\EventException; |
17
|
|
|
use Gears\Event\Exception\InvalidEventException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
21
|
|
|
*/ |
22
|
|
|
final class EventArrayCollection implements EventCollection |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Event[] |
26
|
|
|
*/ |
27
|
|
|
private $events = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* EventArrayCollection constructor. |
31
|
|
|
* |
32
|
|
|
* @param (Event|mixed)[] $events |
33
|
|
|
* |
34
|
|
|
* @throws InvalidEventException |
35
|
|
|
*/ |
36
|
|
|
public function __construct(array $events) |
37
|
|
|
{ |
38
|
|
|
foreach ($events as $event) { |
39
|
|
|
if (!$event instanceof Event) { |
40
|
|
|
throw new InvalidEventException(\sprintf( |
41
|
|
|
'Event collection only accepts "%s", "%s" given.', |
42
|
|
|
Event::class, |
43
|
|
|
\is_object($event) ? \get_class($event) : \gettype($event) |
44
|
|
|
)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->events[] = $event; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
* |
54
|
|
|
* @return Event |
55
|
|
|
*/ |
56
|
|
|
public function current(): Event |
57
|
|
|
{ |
58
|
|
|
return \current($this->events); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function next(): void |
65
|
|
|
{ |
66
|
|
|
\next($this->events); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
* |
72
|
|
|
* @return string|int|null |
73
|
|
|
*/ |
74
|
|
|
public function key() |
75
|
|
|
{ |
76
|
|
|
return \key($this->events); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function valid(): bool |
83
|
|
|
{ |
84
|
|
|
return \key($this->events) !== null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
public function rewind(): void |
91
|
|
|
{ |
92
|
|
|
\reset($this->events); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function count(): int |
99
|
|
|
{ |
100
|
|
|
return \count($this->events); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return string[] |
105
|
|
|
*/ |
106
|
|
|
public function __sleep(): array |
107
|
|
|
{ |
108
|
|
|
throw new EventException(\sprintf('Event collection "%s" cannot be serialized.', self::class)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function __wakeup(): void |
112
|
|
|
{ |
113
|
|
|
throw new EventException(\sprintf('Event collection "%s" cannot be unserialized.', self::class)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return array<string, mixed> |
118
|
|
|
*/ |
119
|
|
|
public function __serialize(): array |
120
|
|
|
{ |
121
|
|
|
throw new EventException(\sprintf('Event collection "%s" cannot be serialized.', self::class)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param array<string, mixed> $data |
126
|
|
|
* |
127
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
128
|
|
|
*/ |
129
|
|
|
public function __unserialize(array $data): void |
130
|
|
|
{ |
131
|
|
|
throw new EventException(\sprintf('Event collection "%s" cannot be unserialized.', self::class)); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|