Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

Commit

Permalink
Merge pull request #7 from Renegade334/devel-connection-data
Browse files Browse the repository at this point in the history
Adds getData(), setData() and clearData() methods.
  • Loading branch information
elazar committed May 26, 2015
2 parents ba19274 + 98691e8 commit e2d45d0
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ class Connection implements ConnectionInterface
*/
protected $options = array();

/**
* Runtime connection data.
*
* @var array
*/
protected $data = array();

/**
* Constructor to accept property values.
*
Expand Down Expand Up @@ -326,6 +333,39 @@ public function getOption($name)
return isset($this->options[$name]) ? $this->options[$name] : null;
}

/**
* Implements ConnectionInterface::setData().
*
* @param string $name Data key
* @param mixed $value Data value
* @return $this
*/
public function setData($name, $value)
{
$this->data[$name] = $value;

return $this;
}

/**
* Implements ConnectionInterface::getData().
*
* @param string $name Data key
* @return mixed
*/
public function getData($name)
{
return isset($this->data[$name]) ? $this->data[$name] : null;
}

/**
* Implements ConnectionInterface::clearData().
*/
public function clearData()
{
$this->data = array();
}

/**
* Returns a formatted connection "mask", in the format
* nick!user@server
Expand Down
21 changes: 21 additions & 0 deletions src/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,27 @@ public function setOption($name, $value);
*/
public function getOption($name);

/**
* Sets a runtime connection data field.
*
* @param string $name Data key
* @param mixed $value Data value
*/
public function setData($name, $value);

/**
* Returns a runtime connection data field.
*
* @param string $name Data key
* @return mixed
*/
public function getData($name);

/**
* Clears all runtime connection data.
*/
public function clearData();

/**
* Returns a formatted connection "mask".
*
Expand Down
23 changes: 23 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,28 @@ public function testGetOption()
$this->assertNull($this->connection->getOption('bar'));
}

/**
* Tests setData(), getData() and clearData().
*/
public function testDataFunctions()
{
$this->assertNull($this->connection->getOption('foo'));
$this->assertNull($this->connection->getOption('bar'));

$this->connection->setData('foo', 'bar');
$this->assertSame('bar', $this->connection->getData('foo'));

$this->connection->setData('foo', 'baz');
$this->assertSame('baz', $this->connection->getData('foo'));

$this->connection->setData('bar', 'quux');
$this->assertSame('quux', $this->connection->getData('bar'));

$this->connection->clearData();
$this->assertNull($this->connection->getData('foo'));
$this->assertNull($this->connection->getData('bar'));
}

public function testFluentInterface()
{
$this->assertSame($this->connection, $this->connection->setServerHostname('hostname'));
Expand All @@ -253,5 +275,6 @@ public function testFluentInterface()
$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'));
$this->assertSame($this->connection, $this->connection->setData('foo', 'bar'));
}
}

0 comments on commit e2d45d0

Please sign in to comment.