annotate vendor/symfony/event-dispatcher/GenericEvent.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
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 protected $subject;
Chris@0 24 protected $arguments;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * Encapsulate an event with $subject and $args.
Chris@0 28 *
Chris@14 29 * @param mixed $subject The subject of the event, usually an object or a callable
Chris@0 30 * @param array $arguments Arguments to store in the event
Chris@0 31 */
Chris@17 32 public function __construct($subject = null, array $arguments = [])
Chris@0 33 {
Chris@0 34 $this->subject = $subject;
Chris@0 35 $this->arguments = $arguments;
Chris@0 36 }
Chris@0 37
Chris@0 38 /**
Chris@0 39 * Getter for subject property.
Chris@0 40 *
Chris@17 41 * @return mixed The observer subject
Chris@0 42 */
Chris@0 43 public function getSubject()
Chris@0 44 {
Chris@0 45 return $this->subject;
Chris@0 46 }
Chris@0 47
Chris@0 48 /**
Chris@0 49 * Get argument by key.
Chris@0 50 *
Chris@0 51 * @param string $key Key
Chris@0 52 *
Chris@0 53 * @return mixed Contents of array key
Chris@0 54 *
Chris@14 55 * @throws \InvalidArgumentException if key is not found
Chris@0 56 */
Chris@0 57 public function getArgument($key)
Chris@0 58 {
Chris@0 59 if ($this->hasArgument($key)) {
Chris@0 60 return $this->arguments[$key];
Chris@0 61 }
Chris@0 62
Chris@0 63 throw new \InvalidArgumentException(sprintf('Argument "%s" not found.', $key));
Chris@0 64 }
Chris@0 65
Chris@0 66 /**
Chris@0 67 * Add argument to event.
Chris@0 68 *
Chris@0 69 * @param string $key Argument name
Chris@0 70 * @param mixed $value Value
Chris@0 71 *
Chris@0 72 * @return $this
Chris@0 73 */
Chris@0 74 public function setArgument($key, $value)
Chris@0 75 {
Chris@0 76 $this->arguments[$key] = $value;
Chris@0 77
Chris@0 78 return $this;
Chris@0 79 }
Chris@0 80
Chris@0 81 /**
Chris@0 82 * Getter for all arguments.
Chris@0 83 *
Chris@0 84 * @return array
Chris@0 85 */
Chris@0 86 public function getArguments()
Chris@0 87 {
Chris@0 88 return $this->arguments;
Chris@0 89 }
Chris@0 90
Chris@0 91 /**
Chris@0 92 * Set args property.
Chris@0 93 *
Chris@0 94 * @param array $args Arguments
Chris@0 95 *
Chris@0 96 * @return $this
Chris@0 97 */
Chris@17 98 public function setArguments(array $args = [])
Chris@0 99 {
Chris@0 100 $this->arguments = $args;
Chris@0 101
Chris@0 102 return $this;
Chris@0 103 }
Chris@0 104
Chris@0 105 /**
Chris@0 106 * Has argument.
Chris@0 107 *
Chris@0 108 * @param string $key Key of arguments array
Chris@0 109 *
Chris@0 110 * @return bool
Chris@0 111 */
Chris@0 112 public function hasArgument($key)
Chris@0 113 {
Chris@18 114 return \array_key_exists($key, $this->arguments);
Chris@0 115 }
Chris@0 116
Chris@0 117 /**
Chris@0 118 * ArrayAccess for argument getter.
Chris@0 119 *
Chris@0 120 * @param string $key Array key
Chris@0 121 *
Chris@0 122 * @return mixed
Chris@0 123 *
Chris@14 124 * @throws \InvalidArgumentException if key does not exist in $this->args
Chris@0 125 */
Chris@0 126 public function offsetGet($key)
Chris@0 127 {
Chris@0 128 return $this->getArgument($key);
Chris@0 129 }
Chris@0 130
Chris@0 131 /**
Chris@0 132 * ArrayAccess for argument setter.
Chris@0 133 *
Chris@0 134 * @param string $key Array key to set
Chris@0 135 * @param mixed $value Value
Chris@0 136 */
Chris@0 137 public function offsetSet($key, $value)
Chris@0 138 {
Chris@0 139 $this->setArgument($key, $value);
Chris@0 140 }
Chris@0 141
Chris@0 142 /**
Chris@0 143 * ArrayAccess for unset argument.
Chris@0 144 *
Chris@0 145 * @param string $key Array key
Chris@0 146 */
Chris@0 147 public function offsetUnset($key)
Chris@0 148 {
Chris@0 149 if ($this->hasArgument($key)) {
Chris@0 150 unset($this->arguments[$key]);
Chris@0 151 }
Chris@0 152 }
Chris@0 153
Chris@0 154 /**
Chris@0 155 * ArrayAccess has argument.
Chris@0 156 *
Chris@0 157 * @param string $key Array key
Chris@0 158 *
Chris@0 159 * @return bool
Chris@0 160 */
Chris@0 161 public function offsetExists($key)
Chris@0 162 {
Chris@0 163 return $this->hasArgument($key);
Chris@0 164 }
Chris@0 165
Chris@0 166 /**
Chris@0 167 * IteratorAggregate for iterating over the object like an array.
Chris@0 168 *
Chris@0 169 * @return \ArrayIterator
Chris@0 170 */
Chris@0 171 public function getIterator()
Chris@0 172 {
Chris@0 173 return new \ArrayIterator($this->arguments);
Chris@0 174 }
Chris@0 175 }