summaryrefslogtreecommitdiff
path: root/plugins/Irc/extlib/phergie/Phergie/Connection/Handler.php
blob: e9aeddcd3ea7e2a5b1a8a49250785e41202c1b4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
 * Phergie 
 *
 * PHP version 5
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.
 * It is also available through the world-wide-web at this URL:
 * http://phergie.org/license
 *
 * @category  Phergie 
 * @package   Phergie
 * @author    Phergie Development Team <team@phergie.org>
 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
 * @license   http://phergie.org/license New BSD License
 * @link      http://pear.phergie.org/package/Phergie
 */

/**
 * Handles connections initiated by the bot.
 *
 * @category Phergie 
 * @package  Phergie
 * @author   Phergie Development Team <team@phergie.org>
 * @license  http://phergie.org/license New BSD License
 * @link     http://pear.phergie.org/package/Phergie
 */
class Phergie_Connection_Handler implements Countable, IteratorAggregate
{
    /**
     * Map of connections indexed by hostmask
     *
     * @var array
     */
    protected $connections;

    /**
     * Constructor to initialize storage for connections. 
     *
     * @return void
     */
    public function __construct()
    {
        $this->connections = array();
    }

    /**
     * Adds a connection to the connection list.
     *
     * @param Phergie_Connection $connection Connection to add
     *
     * @return Phergie_Connection_Handler Provides a fluent interface
     */
    public function addConnection(Phergie_Connection $connection)
    {
        $this->connections[(string) $connection->getHostmask()] = $connection;
        return $this;
    }

    /**
     * Removes a connection from the connection list.
     *
     * @param Phergie_Connection|string $connection Instance or hostmask for
     *        the connection to remove
     *
     * @return Phergie_Connection_Handler Provides a fluent interface
     */
    public function removeConnection($connection)
    {
        if ($connection instanceof Phergie_Connection) {
            $hostmask = (string) $connection->getHostmask(); 
        } elseif (is_string($connection) 
            && isset($this->connections[$connection])) {
            $hostmask = $connection;
        } else {
            return $this;
        }
        unset($this->connections[$hostmask]);
        return $this;
    }

    /**
     * Returns the number of connections in the list. 
     *
     * @return int Number of connections 
     */
    public function count()
    {
        return count($this->connections);
    }

    /**
     * Returns an iterator for the connection list. 
     *
     * @return ArrayIterator
     */
    public function getIterator()
    {
        return new ArrayIterator($this->connections);
    }

    /**
     * Returns a list of specified connection objects.
     *
     * @param array|string $keys One or more hostmasks identifying the 
     *        connections to return
     *
     * @return array List of Phergie_Connection objects corresponding to the 
     *         specified hostmask(s)
     */
    public function getConnections($keys)
    {
        $connections = array();

        if (!is_array($keys)) {
            $keys = array($keys);
        }
        
        foreach ($keys as $key) {
            if (isset($this->connections[$key])) {
                $connections[] = $this->connections[$key];
            }
        }

        return $connections;
    }
}