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: /**
27: * Events list
28: *
29: * @var \Cake\Event\Event[]
30: */
31: protected $_events = [];
32:
33: /**
34: * Empties the list of dispatched events.
35: *
36: * @return void
37: */
38: public function flush()
39: {
40: $this->_events = [];
41: }
42:
43: /**
44: * Adds an event to the list when event listing is enabled.
45: *
46: * @param \Cake\Event\Event $event An event to the list of dispatched events.
47: * @return void
48: */
49: public function add(Event $event)
50: {
51: $this->_events[] = $event;
52: }
53:
54: /**
55: * Whether a offset exists
56: *
57: * @link https://secure.php.net/manual/en/arrayaccess.offsetexists.php
58: * @param mixed $offset An offset to check for.
59: * @return bool True on success or false on failure.
60: */
61: public function offsetExists($offset)
62: {
63: return isset($this->_events[$offset]);
64: }
65:
66: /**
67: * Offset to retrieve
68: *
69: * @link https://secure.php.net/manual/en/arrayaccess.offsetget.php
70: * @param mixed $offset The offset to retrieve.
71: * @return mixed Can return all value types.
72: */
73: public function offsetGet($offset)
74: {
75: if ($this->offsetExists($offset)) {
76: return $this->_events[$offset];
77: }
78:
79: return null;
80: }
81:
82: /**
83: * Offset to set
84: *
85: * @link https://secure.php.net/manual/en/arrayaccess.offsetset.php
86: * @param mixed $offset The offset to assign the value to.
87: * @param mixed $value The value to set.
88: * @return void
89: */
90: public function offsetSet($offset, $value)
91: {
92: $this->_events[$offset] = $value;
93: }
94:
95: /**
96: * Offset to unset
97: *
98: * @link https://secure.php.net/manual/en/arrayaccess.offsetunset.php
99: * @param mixed $offset The offset to unset.
100: * @return void
101: */
102: public function offsetUnset($offset)
103: {
104: unset($this->_events[$offset]);
105: }
106:
107: /**
108: * Count elements of an object
109: *
110: * @link https://secure.php.net/manual/en/countable.count.php
111: * @return int The custom count as an integer.
112: */
113: public function count()
114: {
115: return count($this->_events);
116: }
117:
118: /**
119: * Checks if an event is in the list.
120: *
121: * @param string $name Event name.
122: * @return bool
123: */
124: public function hasEvent($name)
125: {
126: foreach ($this->_events as $event) {
127: if ($event->getName() === $name) {
128: return true;
129: }
130: }
131:
132: return false;
133: }
134: }
135: