Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\EventDispatcher;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Event encapsulation class.
|
Chris@0
|
16 *
|
Chris@0
|
17 * Encapsulates events thus decoupling the observer from the subject they encapsulate.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @author Drak <drak@zikula.org>
|
Chris@0
|
20 */
|
Chris@0
|
21 class GenericEvent extends Event implements \ArrayAccess, \IteratorAggregate
|
Chris@0
|
22 {
|
Chris@0
|
23 /**
|
Chris@0
|
24 * Event subject.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @var mixed usually object or callable
|
Chris@0
|
27 */
|
Chris@0
|
28 protected $subject;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * Array of arguments.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @var array
|
Chris@0
|
34 */
|
Chris@0
|
35 protected $arguments;
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * Encapsulate an event with $subject and $args.
|
Chris@0
|
39 *
|
Chris@0
|
40 * @param mixed $subject The subject of the event, usually an object
|
Chris@0
|
41 * @param array $arguments Arguments to store in the event
|
Chris@0
|
42 */
|
Chris@0
|
43 public function __construct($subject = null, array $arguments = array())
|
Chris@0
|
44 {
|
Chris@0
|
45 $this->subject = $subject;
|
Chris@0
|
46 $this->arguments = $arguments;
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * Getter for subject property.
|
Chris@0
|
51 *
|
Chris@0
|
52 * @return mixed $subject The observer subject
|
Chris@0
|
53 */
|
Chris@0
|
54 public function getSubject()
|
Chris@0
|
55 {
|
Chris@0
|
56 return $this->subject;
|
Chris@0
|
57 }
|
Chris@0
|
58
|
Chris@0
|
59 /**
|
Chris@0
|
60 * Get argument by key.
|
Chris@0
|
61 *
|
Chris@0
|
62 * @param string $key Key
|
Chris@0
|
63 *
|
Chris@0
|
64 * @return mixed Contents of array key
|
Chris@0
|
65 *
|
Chris@0
|
66 * @throws \InvalidArgumentException If key is not found.
|
Chris@0
|
67 */
|
Chris@0
|
68 public function getArgument($key)
|
Chris@0
|
69 {
|
Chris@0
|
70 if ($this->hasArgument($key)) {
|
Chris@0
|
71 return $this->arguments[$key];
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 throw new \InvalidArgumentException(sprintf('Argument "%s" not found.', $key));
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 /**
|
Chris@0
|
78 * Add argument to event.
|
Chris@0
|
79 *
|
Chris@0
|
80 * @param string $key Argument name
|
Chris@0
|
81 * @param mixed $value Value
|
Chris@0
|
82 *
|
Chris@0
|
83 * @return $this
|
Chris@0
|
84 */
|
Chris@0
|
85 public function setArgument($key, $value)
|
Chris@0
|
86 {
|
Chris@0
|
87 $this->arguments[$key] = $value;
|
Chris@0
|
88
|
Chris@0
|
89 return $this;
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * Getter for all arguments.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @return array
|
Chris@0
|
96 */
|
Chris@0
|
97 public function getArguments()
|
Chris@0
|
98 {
|
Chris@0
|
99 return $this->arguments;
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 /**
|
Chris@0
|
103 * Set args property.
|
Chris@0
|
104 *
|
Chris@0
|
105 * @param array $args Arguments
|
Chris@0
|
106 *
|
Chris@0
|
107 * @return $this
|
Chris@0
|
108 */
|
Chris@0
|
109 public function setArguments(array $args = array())
|
Chris@0
|
110 {
|
Chris@0
|
111 $this->arguments = $args;
|
Chris@0
|
112
|
Chris@0
|
113 return $this;
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * Has argument.
|
Chris@0
|
118 *
|
Chris@0
|
119 * @param string $key Key of arguments array
|
Chris@0
|
120 *
|
Chris@0
|
121 * @return bool
|
Chris@0
|
122 */
|
Chris@0
|
123 public function hasArgument($key)
|
Chris@0
|
124 {
|
Chris@0
|
125 return array_key_exists($key, $this->arguments);
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 /**
|
Chris@0
|
129 * ArrayAccess for argument getter.
|
Chris@0
|
130 *
|
Chris@0
|
131 * @param string $key Array key
|
Chris@0
|
132 *
|
Chris@0
|
133 * @return mixed
|
Chris@0
|
134 *
|
Chris@0
|
135 * @throws \InvalidArgumentException If key does not exist in $this->args.
|
Chris@0
|
136 */
|
Chris@0
|
137 public function offsetGet($key)
|
Chris@0
|
138 {
|
Chris@0
|
139 return $this->getArgument($key);
|
Chris@0
|
140 }
|
Chris@0
|
141
|
Chris@0
|
142 /**
|
Chris@0
|
143 * ArrayAccess for argument setter.
|
Chris@0
|
144 *
|
Chris@0
|
145 * @param string $key Array key to set
|
Chris@0
|
146 * @param mixed $value Value
|
Chris@0
|
147 */
|
Chris@0
|
148 public function offsetSet($key, $value)
|
Chris@0
|
149 {
|
Chris@0
|
150 $this->setArgument($key, $value);
|
Chris@0
|
151 }
|
Chris@0
|
152
|
Chris@0
|
153 /**
|
Chris@0
|
154 * ArrayAccess for unset argument.
|
Chris@0
|
155 *
|
Chris@0
|
156 * @param string $key Array key
|
Chris@0
|
157 */
|
Chris@0
|
158 public function offsetUnset($key)
|
Chris@0
|
159 {
|
Chris@0
|
160 if ($this->hasArgument($key)) {
|
Chris@0
|
161 unset($this->arguments[$key]);
|
Chris@0
|
162 }
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 /**
|
Chris@0
|
166 * ArrayAccess has argument.
|
Chris@0
|
167 *
|
Chris@0
|
168 * @param string $key Array key
|
Chris@0
|
169 *
|
Chris@0
|
170 * @return bool
|
Chris@0
|
171 */
|
Chris@0
|
172 public function offsetExists($key)
|
Chris@0
|
173 {
|
Chris@0
|
174 return $this->hasArgument($key);
|
Chris@0
|
175 }
|
Chris@0
|
176
|
Chris@0
|
177 /**
|
Chris@0
|
178 * IteratorAggregate for iterating over the object like an array.
|
Chris@0
|
179 *
|
Chris@0
|
180 * @return \ArrayIterator
|
Chris@0
|
181 */
|
Chris@0
|
182 public function getIterator()
|
Chris@0
|
183 {
|
Chris@0
|
184 return new \ArrayIterator($this->arguments);
|
Chris@0
|
185 }
|
Chris@0
|
186 }
|