1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 3.3.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\Event;
16:
17: use ArrayAccess;
18: use Countable;
19:
20: /**
21: * The Event List
22: */
23: class EventList implements ArrayAccess, Countable
24: {
25: /**
26: * Events list
27: *
28: * @var \Cake\Event\Event[]
29: */
30: protected $_events = [];
31:
32: /**
33: * Empties the list of dispatched events.
34: *
35: * @return void
36: */
37: public function flush()
38: {
39: $this->_events = [];
40: }
41:
42: /**
43: * Adds an event to the list when event listing is enabled.
44: *
45: * @param \Cake\Event\Event $event An event to the list of dispatched events.
46: * @return void
47: */
48: public function add(Event $event)
49: {
50: $this->_events[] = $event;
51: }
52:
53: /**
54: * Whether a offset exists
55: *
56: * @link https://secure.php.net/manual/en/arrayaccess.offsetexists.php
57: * @param mixed $offset An offset to check for.
58: * @return bool True on success or false on failure.
59: */
60: public function offsetExists($offset)
61: {
62: return isset($this->_events[$offset]);
63: }
64:
65: /**
66: * Offset to retrieve
67: *
68: * @link https://secure.php.net/manual/en/arrayaccess.offsetget.php
69: * @param mixed $offset The offset to retrieve.
70: * @return mixed Can return all value types.
71: */
72: public function offsetGet($offset)
73: {
74: if ($this->offsetExists($offset)) {
75: return $this->_events[$offset];
76: }
77:
78: return null;
79: }
80:
81: /**
82: * Offset to set
83: *
84: * @link https://secure.php.net/manual/en/arrayaccess.offsetset.php
85: * @param mixed $offset The offset to assign the value to.
86: * @param mixed $value The value to set.
87: * @return void
88: */
89: public function offsetSet($offset, $value)
90: {
91: $this->_events[$offset] = $value;
92: }
93:
94: /**
95: * Offset to unset
96: *
97: * @link https://secure.php.net/manual/en/arrayaccess.offsetunset.php
98: * @param mixed $offset The offset to unset.
99: * @return void
100: */
101: public function offsetUnset($offset)
102: {
103: unset($this->_events[$offset]);
104: }
105:
106: /**
107: * Count elements of an object
108: *
109: * @link https://secure.php.net/manual/en/countable.count.php
110: * @return int The custom count as an integer.
111: */
112: public function count()
113: {
114: return count($this->_events);
115: }
116:
117: /**
118: * Checks if an event is in the list.
119: *
120: * @param string $name Event name.
121: * @return bool
122: */
123: public function hasEvent($name)
124: {
125: foreach ($this->_events as $event) {
126: if ($event->getName() === $name) {
127: return true;
128: }
129: }
130:
131: return false;
132: }
133: }
134: