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 BadMethodCallException;
12
13 /**
14 * Implementation of an immutable Event.
15 * An immutable event cannot be modified after instanciation :
16 *
17 * - its propagation cannot be stopped
18 * - its arguments cannot be modified
19 *
20 * You may want to use this event when you want to ensure that
21 * the listeners won't manipulate it.
22 *
23 * @since 1.0
24 */
25 final class EventImmutable extends AbstractEvent
26 {
27 /**
28 * A flag to see if the constructor has been
29 * already called.
30 *
31 * @var boolean
32 */
33 private $constructed = false;
34
35 /**
36 * Constructor.
37 *
38 * @param string $name The event name.
39 * @param array $arguments The event arguments.
40 *
41 * @throws BadMethodCallException
42 *
43 * @since 1.0
44 */
45 public function __construct($name, array $arguments = array())
46 {
47 if ($this->constructed)
48 {
49 throw new BadMethodCallException(
50 sprintf('Cannot reconstruct the EventImmutable %s.', $this->name)
51 );
52 }
53
54 $this->constructed = true;
55
56 parent::__construct($name, $arguments);
57 }
58
59 /**
60 * Set the value of an event argument.
61 *
62 * @param string $name The argument name.
63 * @param mixed $value The argument value.
64 *
65 * @return void
66 *
67 * @throws BadMethodCallException
68 *
69 * @since 1.0
70 */
71 public function offsetSet($name, $value)
72 {
73 throw new BadMethodCallException(
74 sprintf(
75 'Cannot set the argument %s of the immutable event %s.',
76 $name,
77 $this->name
78 )
79 );
80 }
81
82 /**
83 * Remove an event argument.
84 *
85 * @param string $name The argument name.
86 *
87 * @return void
88 *
89 * @throws BadMethodCallException
90 *
91 * @since 1.0
92 */
93 public function offsetUnset($name)
94 {
95 throw new BadMethodCallException(
96 sprintf(
97 'Cannot remove the argument %s of the immutable event %s.',
98 $name,
99 $this->name
100 )
101 );
102 }
103 }
104