/
Event.php
151 lines (136 loc) · 3.29 KB
/
Event.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\EventDispatcher;
/**
* Event.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class Event implements EventInterface
{
protected $value = null;
protected $processed = false;
protected $subject;
protected $name;
protected $parameters;
/**
* Constructs a new Event.
*
* @param mixed $subject The subject
* @param string $name The event name
* @param array $parameters An array of parameters
*/
public function __construct($subject, $name, $parameters = array())
{
$this->subject = $subject;
$this->name = $name;
$this->parameters = $parameters;
}
/**
* Returns the subject.
*
* @return mixed The subject
*/
public function getSubject()
{
return $this->subject;
}
/**
* Returns the event name.
*
* @return string The event name
*/
public function getName()
{
return $this->name;
}
/**
* Sets the return value for this event.
*
* @param mixed $value The return value
*/
public function setReturnValue($value)
{
$this->value = $value;
}
/**
* Returns the return value.
*
* @return mixed The return value
*/
public function getReturnValue()
{
return $this->value;
}
/**
* Sets the processed flag.
*
* @param Boolean $processed The processed flag value
*/
public function setProcessed($processed)
{
$this->processed = (Boolean) $processed;
}
/**
* Returns whether the event has been processed by a listener or not.
*
* @return Boolean true if the event has been processed, false otherwise
*/
public function isProcessed()
{
return $this->processed;
}
/**
* Returns the event parameters.
*
* @return array The event parameters
*/
public function all()
{
return $this->parameters;
}
/**
* Returns true if the parameter exists.
*
* @param string $name The parameter name
*
* @return Boolean true if the parameter exists, false otherwise
*/
public function has($name)
{
return array_key_exists($name, $this->parameters);
}
/**
* Returns a parameter value.
*
* @param string $name The parameter name
*
* @return mixed The parameter value
*
* @throws \InvalidArgumentException When parameter doesn't exists for this event
*/
public function get($name)
{
if (!array_key_exists($name, $this->parameters)) {
throw new \InvalidArgumentException(sprintf('The event "%s" has no "%s" parameter.', $this->name, $name));
}
return $this->parameters[$name];
}
/**
* Sets a parameter.
*
* @param string $name The parameter name
* @param mixed $value The parameter value
*/
public function set($name, $value)
{
$this->parameters[$name] = $value;
}
}