Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\EventDispatcher; Chris@0: Chris@0: /** Chris@0: * Event encapsulation class. Chris@0: * Chris@0: * Encapsulates events thus decoupling the observer from the subject they encapsulate. Chris@0: * Chris@0: * @author Drak Chris@0: */ Chris@0: class GenericEvent extends Event implements \ArrayAccess, \IteratorAggregate Chris@0: { Chris@0: protected $subject; Chris@0: protected $arguments; Chris@0: Chris@0: /** Chris@0: * Encapsulate an event with $subject and $args. Chris@0: * Chris@14: * @param mixed $subject The subject of the event, usually an object or a callable Chris@0: * @param array $arguments Arguments to store in the event Chris@0: */ Chris@17: public function __construct($subject = null, array $arguments = []) Chris@0: { Chris@0: $this->subject = $subject; Chris@0: $this->arguments = $arguments; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Getter for subject property. Chris@0: * Chris@17: * @return mixed The observer subject Chris@0: */ Chris@0: public function getSubject() Chris@0: { Chris@0: return $this->subject; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get argument by key. Chris@0: * Chris@0: * @param string $key Key Chris@0: * Chris@0: * @return mixed Contents of array key Chris@0: * Chris@14: * @throws \InvalidArgumentException if key is not found Chris@0: */ Chris@0: public function getArgument($key) Chris@0: { Chris@0: if ($this->hasArgument($key)) { Chris@0: return $this->arguments[$key]; Chris@0: } Chris@0: Chris@0: throw new \InvalidArgumentException(sprintf('Argument "%s" not found.', $key)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Add argument to event. Chris@0: * Chris@0: * @param string $key Argument name Chris@0: * @param mixed $value Value Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setArgument($key, $value) Chris@0: { Chris@0: $this->arguments[$key] = $value; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Getter for all arguments. Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function getArguments() Chris@0: { Chris@0: return $this->arguments; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set args property. Chris@0: * Chris@0: * @param array $args Arguments Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@17: public function setArguments(array $args = []) Chris@0: { Chris@0: $this->arguments = $args; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Has argument. Chris@0: * Chris@0: * @param string $key Key of arguments array Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function hasArgument($key) Chris@0: { Chris@18: return \array_key_exists($key, $this->arguments); Chris@0: } Chris@0: Chris@0: /** Chris@0: * ArrayAccess for argument getter. Chris@0: * Chris@0: * @param string $key Array key Chris@0: * Chris@0: * @return mixed Chris@0: * Chris@14: * @throws \InvalidArgumentException if key does not exist in $this->args Chris@0: */ Chris@0: public function offsetGet($key) Chris@0: { Chris@0: return $this->getArgument($key); Chris@0: } Chris@0: Chris@0: /** Chris@0: * ArrayAccess for argument setter. Chris@0: * Chris@0: * @param string $key Array key to set Chris@0: * @param mixed $value Value Chris@0: */ Chris@0: public function offsetSet($key, $value) Chris@0: { Chris@0: $this->setArgument($key, $value); Chris@0: } Chris@0: Chris@0: /** Chris@0: * ArrayAccess for unset argument. Chris@0: * Chris@0: * @param string $key Array key Chris@0: */ Chris@0: public function offsetUnset($key) Chris@0: { Chris@0: if ($this->hasArgument($key)) { Chris@0: unset($this->arguments[$key]); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * ArrayAccess has argument. Chris@0: * Chris@0: * @param string $key Array key Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function offsetExists($key) Chris@0: { Chris@0: return $this->hasArgument($key); Chris@0: } Chris@0: Chris@0: /** Chris@0: * IteratorAggregate for iterating over the object like an array. Chris@0: * Chris@0: * @return \ArrayIterator Chris@0: */ Chris@0: public function getIterator() Chris@0: { Chris@0: return new \ArrayIterator($this->arguments); Chris@0: } Chris@0: }