4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\EventDispatcher;
15 * Event encapsulation class.
17 * Encapsulates events thus decoupling the observer from the subject they encapsulate.
19 * @author Drak <drak@zikula.org>
21 class GenericEvent extends Event implements \ArrayAccess, \IteratorAggregate
26 * @var mixed usually object or callable
38 * Encapsulate an event with $subject and $args.
40 * @param mixed $subject The subject of the event, usually an object
41 * @param array $arguments Arguments to store in the event
43 public function __construct($subject = null, array $arguments = array())
45 $this->subject = $subject;
46 $this->arguments = $arguments;
50 * Getter for subject property.
52 * @return mixed $subject The observer subject
54 public function getSubject()
56 return $this->subject;
60 * Get argument by key.
62 * @param string $key Key
64 * @return mixed Contents of array key
66 * @throws \InvalidArgumentException If key is not found.
68 public function getArgument($key)
70 if ($this->hasArgument($key)) {
71 return $this->arguments[$key];
74 throw new \InvalidArgumentException(sprintf('Argument "%s" not found.', $key));
78 * Add argument to event.
80 * @param string $key Argument name
81 * @param mixed $value Value
85 public function setArgument($key, $value)
87 $this->arguments[$key] = $value;
93 * Getter for all arguments.
97 public function getArguments()
99 return $this->arguments;
105 * @param array $args Arguments
109 public function setArguments(array $args = array())
111 $this->arguments = $args;
119 * @param string $key Key of arguments array
123 public function hasArgument($key)
125 return array_key_exists($key, $this->arguments);
129 * ArrayAccess for argument getter.
131 * @param string $key Array key
135 * @throws \InvalidArgumentException If key does not exist in $this->args.
137 public function offsetGet($key)
139 return $this->getArgument($key);
143 * ArrayAccess for argument setter.
145 * @param string $key Array key to set
146 * @param mixed $value Value
148 public function offsetSet($key, $value)
150 $this->setArgument($key, $value);
154 * ArrayAccess for unset argument.
156 * @param string $key Array key
158 public function offsetUnset($key)
160 if ($this->hasArgument($key)) {
161 unset($this->arguments[$key]);
166 * ArrayAccess has argument.
168 * @param string $key Array key
172 public function offsetExists($key)
174 return $this->hasArgument($key);
178 * IteratorAggregate for iterating over the object like an array.
180 * @return \ArrayIterator
182 public function getIterator()
184 return new \ArrayIterator($this->arguments);