Skip to content
This repository has been archived by the owner on Jun 20, 2018. It is now read-only.

Commit

Permalink
Now we can set a default value for tryGet method
Browse files Browse the repository at this point in the history
  • Loading branch information
italo authored and italo committed Nov 29, 2013
1 parent 1c6842e commit 12de375
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions CollectionArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ public function get($index)
/**
* {@inheritdoc}
*/
public function tryGet($index)
public function tryGet($index, $default = null)
{
if ($this->offsetExists($index) === false) {
return null;
return $default;
}

return $this->get($index);
Expand Down
21 changes: 21 additions & 0 deletions Test/DictionaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ public function testSetNullKey()
$this->coll->set(null, 'testing');
}

public function testTryGetSuccess()
{
$this->coll->add('key', 'testing');
$value = $this->coll->tryGet('key');
$this->assertEquals('testing', $value);
}

public function testTryGetError()
{
$this->coll->add('key', 'testing');
$value = $this->coll->tryGet('key2');
$this->assertNull($value);
}

public function testTryGetDefaultValue()
{
$this->coll->add('key', 'testing');
$value = $this->coll->tryGet('key2', 'testingValue');
$this->assertEquals('testingValue', $value);
}

/**
* @expectedException \InvalidArgumentException
*/
Expand Down

0 comments on commit 12de375

Please sign in to comment.