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