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
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
23 * Data structure for connection metadata.
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
31 class Phergie_Connection
34 * Host to which the client will connect
41 * Port on which the client will connect, defaults to the standard IRC
49 * Transport for the connection, defaults to tcp but can be set to ssl
50 * or variations thereof to connect over SSL
57 * Nick that the client will use
64 * Username that the client will use
71 * Realname that the client will use
78 * Password that the client will use
85 * Hostmask for the connection
87 * @var Phergie_Hostmask
92 * Constructor to initialize instance properties.
94 * @param array $options Optional associative array of property values
99 public function __construct(array $options = array())
101 $this->transport = 'tcp';
103 $this->setOptions($options);
107 * Emits an error related to a required connection setting does not have
110 * @param string $setting Name of the setting
114 protected function checkSetting($setting)
116 if (empty($this->$setting)) {
117 throw new Phergie_Connection_Exception(
118 'Required connection setting "' . $setting . '" missing',
119 Phergie_Connection_Exception::ERR_REQUIRED_SETTING_MISSING
125 * Returns a hostmask that uniquely identifies the connection.
129 public function getHostmask()
131 if (empty($this->hostmask)) {
132 $this->hostmask = new Phergie_Hostmask(
139 return $this->hostmask;
143 * Sets the host to which the client will connect.
145 * @param string $host Hostname
147 * @return Phergie_Connection Provides a fluent interface
149 public function setHost($host)
151 if (empty($this->host)) {
152 $this->host = (string) $host;
159 * Returns the host to which the client will connect if it is set or
160 * emits an error if it is not set.
164 public function getHost()
166 $this->checkSetting('host');
172 * Sets the port on which the client will connect.
174 * @param int $port Port
176 * @return Phergie_Connection Provides a fluent interface
178 public function setPort($port)
180 if (empty($this->port)) {
181 $this->port = (int) $port;
188 * Returns the port on which the client will connect.
192 public function getPort()
194 if (empty($this->port)) {
202 * Sets the transport for the connection to use.
204 * @param string $transport Transport (ex: tcp, ssl, etc.)
206 * @return Phergie_Connection Provides a fluent interface
208 public function setTransport($transport)
210 $this->transport = (string) $transport;
212 if (!in_array($this->transport, stream_get_transports())) {
213 throw new Phergie_Connection_Exception(
214 'Transport ' . $this->transport . ' is not supported',
215 Phergie_Connection_Exception::TRANSPORT_NOT_SUPPORTED
223 * Returns the transport in use by the connection.
225 * @return string Transport (ex: tcp, ssl, etc.)
227 public function getTransport()
229 return $this->transport;
233 * Sets the nick that the client will use.
235 * @param string $nick Nickname
237 * @return Phergie_Connection Provides a fluent interface
239 public function setNick($nick)
241 if (empty($this->nick)) {
242 $this->nick = (string) $nick;
249 * Returns the nick that the client will use.
253 public function getNick()
255 $this->checkSetting('nick');
261 * Sets the username that the client will use.
263 * @param string $username Username
265 * @return Phergie_Connection Provides a fluent interface
267 public function setUsername($username)
269 if (empty($this->username)) {
270 $this->username = (string) $username;
277 * Returns the username that the client will use.
281 public function getUsername()
283 $this->checkSetting('username');
285 return $this->username;
289 * Sets the realname that the client will use.
291 * @param string $realname Real name
293 * @return Phergie_Connection Provides a fluent interface
295 public function setRealname($realname)
297 if (empty($this->realname)) {
298 $this->realname = (string) $realname;
305 * Returns the realname that the client will use.
309 public function getRealname()
311 $this->checkSetting('realname');
313 return $this->realname;
317 * Sets the password that the client will use.
319 * @param string $password Password
321 * @return Phergie_Connection Provides a fluent interface
323 public function setPassword($password)
325 if (empty($this->password)) {
326 $this->password = (string) $password;
333 * Returns the password that the client will use.
337 public function getPassword()
339 return $this->password;
343 * Sets multiple connection settings using an array.
345 * @param array $options Associative array of setting names mapped to
346 * corresponding values
348 * @return Phergie_Connection Provides a fluent interface
350 public function setOptions(array $options)
352 foreach ($options as $option => $value) {
353 $method = 'set' . ucfirst($option);
354 if (method_exists($this, $method)) {
355 $this->$method($value);