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