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
27 * Encapsulate an event with $subject and $args.
29 * @param mixed $subject The subject of the event, usually an object or a callable
30 * @param array $arguments Arguments to store in the event
32 public function __construct($subject = null, array $arguments = array())
34 $this->subject = $subject;
35 $this->arguments = $arguments;
39 * Getter for subject property.
41 * @return mixed $subject The observer subject
43 public function getSubject()
45 return $this->subject;
49 * Get argument by key.
51 * @param string $key Key
53 * @return mixed Contents of array key
55 * @throws \InvalidArgumentException if key is not found
57 public function getArgument($key)
59 if ($this->hasArgument($key)) {
60 return $this->arguments[$key];
63 throw new \InvalidArgumentException(sprintf('Argument "%s" not found.', $key));
67 * Add argument to event.
69 * @param string $key Argument name
70 * @param mixed $value Value
74 public function setArgument($key, $value)
76 $this->arguments[$key] = $value;
82 * Getter for all arguments.
86 public function getArguments()
88 return $this->arguments;
94 * @param array $args Arguments
98 public function setArguments(array $args = array())
100 $this->arguments = $args;
108 * @param string $key Key of arguments array
112 public function hasArgument($key)
114 return array_key_exists($key, $this->arguments);
118 * ArrayAccess for argument getter.
120 * @param string $key Array key
124 * @throws \InvalidArgumentException if key does not exist in $this->args
126 public function offsetGet($key)
128 return $this->getArgument($key);
132 * ArrayAccess for argument setter.
134 * @param string $key Array key to set
135 * @param mixed $value Value
137 public function offsetSet($key, $value)
139 $this->setArgument($key, $value);
143 * ArrayAccess for unset argument.
145 * @param string $key Array key
147 public function offsetUnset($key)
149 if ($this->hasArgument($key)) {
150 unset($this->arguments[$key]);
155 * ArrayAccess has argument.
157 * @param string $key Array key
161 public function offsetExists($key)
163 return $this->hasArgument($key);
167 * IteratorAggregate for iterating over the object like an array.
169 * @return \ArrayIterator
171 public function getIterator()
173 return new \ArrayIterator($this->arguments);