]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Connection/Handler.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Connection / 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 connections initiated by the bot.
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_Connection_Handler implements Countable, IteratorAggregate
32 {
33     /**
34      * Map of connections indexed by hostmask
35      *
36      * @var array
37      */
38     protected $connections;
39
40     /**
41      * Constructor to initialize storage for connections. 
42      *
43      * @return void
44      */
45     public function __construct()
46     {
47         $this->connections = array();
48     }
49
50     /**
51      * Adds a connection to the connection list.
52      *
53      * @param Phergie_Connection $connection Connection to add
54      *
55      * @return Phergie_Connection_Handler Provides a fluent interface
56      */
57     public function addConnection(Phergie_Connection $connection)
58     {
59         $this->connections[(string) $connection->getHostmask()] = $connection;
60         return $this;
61     }
62
63     /**
64      * Removes a connection from the connection list.
65      *
66      * @param Phergie_Connection|string $connection Instance or hostmask for
67      *        the connection to remove
68      *
69      * @return Phergie_Connection_Handler Provides a fluent interface
70      */
71     public function removeConnection($connection)
72     {
73         if ($connection instanceof Phergie_Connection) {
74             $hostmask = (string) $connection->getHostmask(); 
75         } elseif (is_string($connection) 
76             && isset($this->connections[$connection])) {
77             $hostmask = $connection;
78         } else {
79             return $this;
80         }
81         unset($this->connections[$hostmask]);
82         return $this;
83     }
84
85     /**
86      * Returns the number of connections in the list. 
87      *
88      * @return int Number of connections 
89      */
90     public function count()
91     {
92         return count($this->connections);
93     }
94
95     /**
96      * Returns an iterator for the connection list. 
97      *
98      * @return ArrayIterator
99      */
100     public function getIterator()
101     {
102         return new ArrayIterator($this->connections);
103     }
104
105     /**
106      * Returns a list of specified connection objects.
107      *
108      * @param array|string $keys One or more hostmasks identifying the 
109      *        connections to return
110      *
111      * @return array List of Phergie_Connection objects corresponding to the 
112      *         specified hostmask(s)
113      */
114     public function getConnections($keys)
115     {
116         $connections = array();
117
118         if (!is_array($keys)) {
119             $keys = array($keys);
120         }
121         
122         foreach ($keys as $key) {
123             if (isset($this->connections[$key])) {
124                 $connections[] = $this->connections[$key];
125             }
126         }
127
128         return $connections;
129     }
130 }