-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEvent.php
More file actions
66 lines (56 loc) · 1.47 KB
/
Event.php
File metadata and controls
66 lines (56 loc) · 1.47 KB
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
<?php
declare(strict_types=1);
namespace PhpSchool\PhpWorkshop\Event;
use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException;
/**
* A generic `PhpSchool\PhpWorkshop\Event\EventInterface` implementation.
*/
class Event implements EventInterface
{
private string $name;
/**
* @var array<mixed>
*/
protected array $parameters;
/**
* @param string $name The event name.
* @param array<mixed> $parameters The event parameters.
*/
public function __construct(string $name, array $parameters = [])
{
$this->name = $name;
$this->parameters = $parameters;
}
/**
* Get the name of this event.
*
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* Get an array of parameters that were triggered with this event.
*
* @return array<mixed>
*/
public function getParameters(): array
{
return $this->parameters;
}
/**
* Get a parameter by its name.
*
* @param string $name The name of the parameter.
* @return mixed The value.
* @throws InvalidArgumentException If the parameter by name does not exist.
*/
public function getParameter(string $name): mixed
{
if (!array_key_exists($name, $this->parameters)) {
throw new InvalidArgumentException(sprintf('Parameter: "%s" does not exist', $name));
}
return $this->parameters[$name];
}
}