From c36d6e8f456df81867e80ef56afcbbbef8d0d5a0 Mon Sep 17 00:00:00 2001 From: Tom Rochette Date: Mon, 4 May 2015 21:12:05 -0400 Subject: [PATCH] Add fluent interface to the Connection class. --- README.md | 19 ++++++++++--------- src/Connection.php | 27 +++++++++++++++++++++++++++ tests/ConnectionTest.php | 13 +++++++++++++ 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index fecf919..291a5b2 100644 --- a/README.md +++ b/README.md @@ -29,15 +29,16 @@ The recommended method of installation is [through composer](http://getcomposer. ```php $connection = new \Phergie\Irc\Connection(); -$connection->setServerHostname('hostname'); -$connection->setServerPort(6668); -$connection->setPassword('password'); -$connection->setNickname('nickname'); -$connection->setUsername('username'); -$connection->setHostname('hostname'); -$connection->setServername('servername'); -$connection->setRealname('realname'); -$connection->setOption('option', 'value'); +$connection + ->setServerHostname('hostname') + ->setServerPort(6668) + ->setPassword('password') + ->setNickname('nickname') + ->setUsername('username') + ->setHostname('hostname') + ->setServername('servername') + ->setRealname('realname') + ->setOption('option', 'value'); echo $connection->getServerHostname(); echo $connection->getServerPort(); diff --git a/src/Connection.php b/src/Connection.php index 1cedf01..564880d 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -105,11 +105,14 @@ public function __construct(array $config = array()) * Implements ConnectionInterface::setServerHostname(). * * @param string $hostname + * @return $this * @see \Phergie\Irc\ConnectionInterface::setServerHostname() */ public function setServerHostname($hostname) { $this->serverHostname = $hostname; + + return $this; } /** @@ -127,11 +130,14 @@ public function getServerHostname() * Implements ConnectionInterface::setServerPort(). * * @param int $port + * @return $this * @see \Phergie\Irc\ConnectionInterface::setServerPort() */ public function setServerPort($port) { $this->serverPort = $port; + + return $this; } /** @@ -149,11 +155,14 @@ public function getServerPort() * Implements ConnectionInterface::setPassword(). * * @param string $password + * @return $this * @see \Phergie\Irc\ConnectionInterface::setPassword() */ public function setPassword($password) { $this->password = $password; + + return $this; } /** @@ -171,11 +180,14 @@ public function getPassword() * Implements ConnectionInterface::setNickname(). * * @param string $nickname + * @return $this * @see \Phergie\Irc\ConnectionInterface::setNickname() */ public function setNickname($nickname) { $this->nickname = $nickname; + + return $this; } /** @@ -193,11 +205,14 @@ public function getNickname() * Implements ConnectionInterface::setUsername(). * * @param string $username + * @return $this * @see \Phergie\Irc\ConnectionInterface::setUsername() */ public function setUsername($username) { $this->username = $username; + + return $this; } /** @@ -215,11 +230,14 @@ public function getUsername() * Implements ConnectionInterface::setHostname(). * * @param string $hostname + * @return $this * @see \Phergie\Irc\ConnectionInterface::setHostname() */ public function setHostname($hostname) { $this->hostname = $hostname; + + return $this; } /** @@ -237,11 +255,14 @@ public function getHostname() * Implements ConnectionInterface::setServername(). * * @param string $servername + * @return $this * @see \Phergie\Irc\ConnectionInterface::setServername() */ public function setServername($servername) { $this->servername = $servername; + + return $this; } /** @@ -259,11 +280,14 @@ public function getServername() * Implements ConnectionInterface::setRealname(). * * @param string $realname + * @return $this * @see \Phergie\Irc\ConnectionInterface::setRealname() */ public function setRealname($realname) { $this->realname = $realname; + + return $this; } /** @@ -282,10 +306,13 @@ public function getRealname() * * @param string $name Option name * @param mixed $value Option value + * @return $this */ public function setOption($name, $value) { $this->options[$name] = $value; + + return $this; } /** diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 51cb463..d9ef295 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -229,4 +229,17 @@ public function testGetOption() $this->assertNull($this->connection->getOption('foo')); $this->assertNull($this->connection->getOption('bar')); } + + public function testFluentInterface() + { + $this->assertSame($this->connection, $this->connection->setServerHostname('hostname')); + $this->assertSame($this->connection, $this->connection->setServerPort(6668)); + $this->assertSame($this->connection, $this->connection->setPassword('password')); + $this->assertSame($this->connection, $this->connection->setNickname('nickname')); + $this->assertSame($this->connection, $this->connection->setUsername('username')); + $this->assertSame($this->connection, $this->connection->setHostname('hostname')); + $this->assertSame($this->connection, $this->connection->setServername('servername')); + $this->assertSame($this->connection, $this->connection->setRealname('realname')); + $this->assertSame($this->connection, $this->connection->setOption('foo', 'bar')); + } }