|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_EventManager |
|
17 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 */ |
|
20 |
|
21 require_once 'Zend/EventManager/EventDescription.php'; |
|
22 |
|
23 /** |
|
24 * Representation of an event |
|
25 * |
|
26 * Encapsulates the target context and parameters passed, and provides some |
|
27 * behavior for interacting with the event manager. |
|
28 * |
|
29 * @category Zend |
|
30 * @package Zend_EventManager |
|
31 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
|
32 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
33 */ |
|
34 class Zend_EventManager_Event implements Zend_EventManager_EventDescription |
|
35 { |
|
36 /** |
|
37 * @var string Event name |
|
38 */ |
|
39 protected $name; |
|
40 |
|
41 /** |
|
42 * @var string|object The event target |
|
43 */ |
|
44 protected $target; |
|
45 |
|
46 /** |
|
47 * @var array|ArrayAccess|object The event parameters |
|
48 */ |
|
49 protected $params = array(); |
|
50 |
|
51 /** |
|
52 * @var bool Whether or not to stop propagation |
|
53 */ |
|
54 protected $stopPropagation = false; |
|
55 |
|
56 /** |
|
57 * Constructor |
|
58 * |
|
59 * Accept a target and its parameters. |
|
60 * |
|
61 * @param string $name Event name |
|
62 * @param string|object $target |
|
63 * @param array|ArrayAccess $params |
|
64 * @return void |
|
65 */ |
|
66 public function __construct($name = null, $target = null, $params = null) |
|
67 { |
|
68 if (null !== $name) { |
|
69 $this->setName($name); |
|
70 } |
|
71 |
|
72 if (null !== $target) { |
|
73 $this->setTarget($target); |
|
74 } |
|
75 |
|
76 if (null !== $params) { |
|
77 $this->setParams($params); |
|
78 } |
|
79 } |
|
80 |
|
81 /** |
|
82 * Get event name |
|
83 * |
|
84 * @return string |
|
85 */ |
|
86 public function getName() |
|
87 { |
|
88 return $this->name; |
|
89 } |
|
90 |
|
91 /** |
|
92 * Get the event target |
|
93 * |
|
94 * This may be either an object, or the name of a static method. |
|
95 * |
|
96 * @return string|object |
|
97 */ |
|
98 public function getTarget() |
|
99 { |
|
100 return $this->target; |
|
101 } |
|
102 |
|
103 /** |
|
104 * Set parameters |
|
105 * |
|
106 * Overwrites parameters |
|
107 * |
|
108 * @param array|ArrayAccess|object $params |
|
109 * @return Event |
|
110 */ |
|
111 public function setParams($params) |
|
112 { |
|
113 if (!is_array($params) && !is_object($params)) { |
|
114 require_once 'Zend/EventManager/Exception/InvalidArgumentException.php'; |
|
115 throw new Zend_EventManager_Exception_InvalidArgumentException(sprintf( |
|
116 'Event parameters must be an array or object; received "%s"', |
|
117 (is_object($params) ? get_class($params) : gettype($params)) |
|
118 )); |
|
119 } |
|
120 |
|
121 $this->params = $params; |
|
122 return $this; |
|
123 } |
|
124 |
|
125 /** |
|
126 * Get all parameters |
|
127 * |
|
128 * @return array|object|ArrayAccess |
|
129 */ |
|
130 public function getParams() |
|
131 { |
|
132 return $this->params; |
|
133 } |
|
134 |
|
135 /** |
|
136 * Get an individual parameter |
|
137 * |
|
138 * If the parameter does not exist, the $default value will be returned. |
|
139 * |
|
140 * @param string|int $name |
|
141 * @param mixed $default |
|
142 * @return mixed |
|
143 */ |
|
144 public function getParam($name, $default = null) |
|
145 { |
|
146 // Check in params that are arrays or implement array access |
|
147 if (is_array($this->params) || $this->params instanceof ArrayAccess) { |
|
148 if (!isset($this->params[$name])) { |
|
149 return $default; |
|
150 } |
|
151 |
|
152 return $this->params[$name]; |
|
153 } |
|
154 |
|
155 // Check in normal objects |
|
156 if (!isset($this->params->{$name})) { |
|
157 return $default; |
|
158 } |
|
159 return $this->params->{$name}; |
|
160 } |
|
161 |
|
162 /** |
|
163 * Set the event name |
|
164 * |
|
165 * @param string $name |
|
166 * @return Zend_EventManager_Event |
|
167 */ |
|
168 public function setName($name) |
|
169 { |
|
170 $this->name = (string) $name; |
|
171 return $this; |
|
172 } |
|
173 |
|
174 /** |
|
175 * Set the event target/context |
|
176 * |
|
177 * @param null|string|object $target |
|
178 * @return Zend_EventManager_Event |
|
179 */ |
|
180 public function setTarget($target) |
|
181 { |
|
182 $this->target = $target; |
|
183 return $this; |
|
184 } |
|
185 |
|
186 /** |
|
187 * Set an individual parameter to a value |
|
188 * |
|
189 * @param string|int $name |
|
190 * @param mixed $value |
|
191 * @return Zend_EventManager_Event |
|
192 */ |
|
193 public function setParam($name, $value) |
|
194 { |
|
195 if (is_array($this->params) || $this->params instanceof ArrayAccess) { |
|
196 // Arrays or objects implementing array access |
|
197 $this->params[$name] = $value; |
|
198 } else { |
|
199 // Objects |
|
200 $this->params->{$name} = $value; |
|
201 } |
|
202 return $this; |
|
203 } |
|
204 |
|
205 /** |
|
206 * Stop further event propagation |
|
207 * |
|
208 * @param bool $flag |
|
209 * @return void |
|
210 */ |
|
211 public function stopPropagation($flag = true) |
|
212 { |
|
213 $this->stopPropagation = (bool) $flag; |
|
214 } |
|
215 |
|
216 /** |
|
217 * Is propagation stopped? |
|
218 * |
|
219 * @return bool |
|
220 */ |
|
221 public function propagationIsStopped() |
|
222 { |
|
223 return $this->stopPropagation; |
|
224 } |
|
225 } |