1 <?php
2 /**
3 * Part of the Joomla Framework Event Package
4 *
5 * @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE
7 */
8
9 namespace Joomla\Event;
10
11 use Serializable;
12 use ArrayAccess;
13 use Countable;
14
15 /**
16 * Implementation of EventInterface.
17 *
18 * @since 1.0
19 */
20 abstract class AbstractEvent implements EventInterface, ArrayAccess, Serializable, Countable
21 {
22 /**
23 * The event name.
24 *
25 * @var string
26 *
27 * @since 1.0
28 */
29 protected $name;
30
31 /**
32 * The event arguments.
33 *
34 * @var array
35 *
36 * @since 1.0
37 */
38 protected $arguments;
39
40 /**
41 * A flag to see if the event propagation is stopped.
42 *
43 * @var boolean
44 *
45 * @since 1.0
46 */
47 protected $stopped = false;
48
49 /**
50 * Constructor.
51 *
52 * @param string $name The event name.
53 * @param array $arguments The event arguments.
54 *
55 * @since 1.0
56 */
57 public function __construct($name, array $arguments = array())
58 {
59 $this->name = $name;
60 $this->arguments = $arguments;
61 }
62
63 /**
64 * Get the event name.
65 *
66 * @return string The event name.
67 *
68 * @since 1.0
69 */
70 public function getName()
71 {
72 return $this->name;
73 }
74
75 /**
76 * Get an event argument value.
77 *
78 * @param string $name The argument name.
79 * @param mixed $default The default value if not found.
80 *
81 * @return mixed The argument value or the default value.
82 *
83 * @since 1.0
84 */
85 public function getArgument($name, $default = null)
86 {
87 if (isset($this->arguments[$name]))
88 {
89 return $this->arguments[$name];
90 }
91
92 return $default;
93 }
94
95 /**
96 * Tell if the given event argument exists.
97 *
98 * @param string $name The argument name.
99 *
100 * @return boolean True if it exists, false otherwise.
101 *
102 * @since 1.0
103 */
104 public function hasArgument($name)
105 {
106 return isset($this->arguments[$name]);
107 }
108
109 /**
110 * Get all event arguments.
111 *
112 * @return array An associative array of argument names as keys
113 * and their values as values.
114 *
115 * @since 1.0
116 */
117 public function getArguments()
118 {
119 return $this->arguments;
120 }
121
122 /**
123 * Tell if the event propagation is stopped.
124 *
125 * @return boolean True if stopped, false otherwise.
126 *
127 * @since 1.0
128 */
129 public function isStopped()
130 {
131 return true === $this->stopped;
132 }
133
134 /**
135 * Count the number of arguments.
136 *
137 * @return integer The number of arguments.
138 *
139 * @since 1.0
140 */
141 public function count()
142 {
143 return count($this->arguments);
144 }
145
146 /**
147 * Serialize the event.
148 *
149 * @return string The serialized event.
150 *
151 * @since 1.0
152 */
153 public function serialize()
154 {
155 return serialize(array($this->name, $this->arguments, $this->stopped));
156 }
157
158 /**
159 * Unserialize the event.
160 *
161 * @param string $serialized The serialized event.
162 *
163 * @return void
164 *
165 * @since 1.0
166 */
167 public function unserialize($serialized)
168 {
169 list($this->name, $this->arguments, $this->stopped) = unserialize($serialized);
170 }
171
172 /**
173 * Tell if the given event argument exists.
174 *
175 * @param string $name The argument name.
176 *
177 * @return boolean True if it exists, false otherwise.
178 *
179 * @since 1.0
180 */
181 public function offsetExists($name)
182 {
183 return $this->hasArgument($name);
184 }
185
186 /**
187 * Get an event argument value.
188 *
189 * @param string $name The argument name.
190 *
191 * @return mixed The argument value or null if not existing.
192 *
193 * @since 1.0
194 */
195 public function offsetGet($name)
196 {
197 return $this->getArgument($name);
198 }
199 }
200