]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Event/Handler.php
e308df8a5633b052b7b45fe6b2b6c14476160c93
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Event / Handler.php
1 <?php
2 /**
3  * Phergie
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
9  * This source file is subject to the new BSD license that is bundled
10  * with this package in the file LICENSE.
11  * It is also available through the world-wide-web at this URL:
12  * http://phergie.org/license
13  *
14  * @category  Phergie
15  * @package   Phergie
16  * @author    Phergie Development Team <team@phergie.org>
17  * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18  * @license   http://phergie.org/license New BSD License
19  * @link      http://pear.phergie.org/package/Phergie
20  */
21
22 /**
23  * Handles events initiated by plugins.
24  *
25  * @category Phergie
26  * @package  Phergie
27  * @author   Phergie Development Team <team@phergie.org>
28  * @license  http://phergie.org/license New BSD License
29  * @link     http://pear.phergie.org/package/Phergie
30  */
31 class Phergie_Event_Handler implements IteratorAggregate, Countable
32 {
33     /**
34      * Current queue of events
35      *
36      * @var array
37      */
38     protected $events;
39
40     /**
41      * Constructor to initialize the event queue.
42      *
43      * @return void
44      */
45     public function __construct()
46     {
47         $this->events = array();
48     }
49
50     /**
51      * Adds an event to the queue.
52      *
53      * @param Phergie_Plugin_Abstract $plugin Plugin originating the event
54      * @param string                  $type   Event type, corresponding to a
55      *        Phergie_Event_Command::TYPE_* constant
56      * @param array                   $args   Optional event arguments
57      *
58      * @return Phergie_Event_Handler Provides a fluent interface
59      */
60     public function addEvent(Phergie_Plugin_Abstract $plugin, $type,
61         array $args = array()
62     ) {
63         if (!defined('Phergie_Event_Command::TYPE_' . strtoupper($type))) {
64             throw new Phergie_Event_Exception(
65                 'Unknown event type "' . $type . '"',
66                 Phergie_Event_Exception::ERR_UNKNOWN_EVENT_TYPE
67             );
68         }
69
70         $event = new Phergie_Event_Command;
71         $event
72             ->setPlugin($plugin)
73             ->setType($type)
74             ->setArguments($args);
75
76         $this->events[] = $event;
77
78         return $this;
79     }
80
81     /**
82      * Returns the current event queue.
83      *
84      * @return array Enumerated array of Phergie_Event_Command objects
85      */
86     public function getEvents()
87     {
88         return $this->events;
89     }
90
91     /**
92      * Clears the event queue.
93      *
94      * @return Phergie_Event_Handler Provides a fluent interface
95      */
96     public function clearEvents()
97     {
98         $this->events = array();
99         return $this;
100     }
101
102     /**
103      * Replaces the current event queue with a given queue of events.
104      *
105      * @param array $events Ordered list of objects of the class
106      *        Phergie_Event_Command
107      *
108      * @return Phergie_Event_Handler Provides a fluent interface
109      */
110     public function replaceEvents(array $events)
111     {
112         $this->events = $events;
113         return $this;
114     }
115
116     /**
117      * Returns whether an event of the given type exists in the queue.
118      *
119      * @param string $type Event type from Phergie_Event_Request::TYPE_*
120      *        constants
121      *
122      * @return bool TRUE if an event of the specified type exists in the
123      *         queue, FALSE otherwise
124      */
125     public function hasEventOfType($type)
126     {
127         foreach ($this->events as $event) {
128             if ($event->getType() == $type) {
129                 return true;
130             }
131         }
132         return false;
133     }
134
135     /**
136      * Returns a list of events of a specified type.
137      *
138      * @param string $type Event type from Phergie_Event_Request::TYPE_*
139      *        constants
140      *
141      * @return array Array containing event instances of the specified type
142      *         or an empty array if no such events were found
143      */
144     public function getEventsOfType($type)
145     {
146         $events = array();
147         foreach ($this->events as $event) {
148             if ($event->getType() == $type) {
149                 $events[] = $event;
150             }
151         }
152         return $events;
153     }
154
155     /**
156      * Removes a single event from the event queue.
157      *
158      * @param Phergie_Event_Command $event Event to remove
159      *
160      * @return Phergie_Event_Handler Provides a fluent interface
161      */
162     public function removeEvent(Phergie_Event_Command $event)
163     {
164         $key = array_search($event, $this->events);
165         if ($key !== false) {
166             unset($this->events[$key]);
167         }
168         return $this;
169     }
170
171     /**
172      * Returns an iterator for the current event queue.
173      *
174      * @return ArrayIterator
175      */
176     public function getIterator()
177     {
178         return new ArrayIterator($this->events);
179     }
180
181     /**
182      * Returns the number of events in the event queue
183      *
184      * @return int number of queued events
185      */
186     public function count()
187     {
188         return count($this->events);
189     }
190 }