Skip to content

Commit

Permalink
add checker of key existance
Browse files Browse the repository at this point in the history
  • Loading branch information
sokil committed Nov 28, 2014
1 parent b5ac91a commit b735972
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 46 deletions.
43 changes: 24 additions & 19 deletions src/PriorityList.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
namespace Sokil\DataType;

class PriorityList implements \Iterator, \Countable
{
{
private $lastSequence = 0;

private $list;

const ORDER_ASC = 'asc';
const ORDER_DESC = 'desc';

private $order = self::ORDER_DESC;

/**
*
*
* @param string $key name
* @param string $value value
* @param string $priority priority
Expand All @@ -26,32 +26,37 @@ public function set($key, $value, $priority = 0)
$this->list[$key]->value = $value;
$this->list[$key]->priority = (int) $priority;
$this->list[$key]->sequence = $this->lastSequence++;

return $this->list[$key];
}

public function get($name)
{
return isset($this->list[$name]) ? $this->list[$name]->value : null;
}


public function has($name)
{
return isset($this->list[$name]);
}

public function count()
{
return count($this->list);
}

public function setAscOrder()
{
$this->order = self::ORDER_ASC;
return $this;
}

public function setDescOrder()
{
$this->order = self::ORDER_DESC;
return $this;
}

private function ascSortStrategy($declaration1, $declaration2)
{
if($declaration1->priority === $declaration2->priority) {
Expand All @@ -60,7 +65,7 @@ private function ascSortStrategy($declaration1, $declaration2)

return $declaration1->priority > $declaration2->priority ? 1 : -1;
}

private function descSortStrategy($declaration1, $declaration2)
{
if($declaration1->priority === $declaration2->priority) {
Expand All @@ -69,34 +74,34 @@ private function descSortStrategy($declaration1, $declaration2)

return $declaration1->priority < $declaration2->priority ? 1 : -1;
}

public function rewind()
{
uasort($this->list, array($this, $this->order . 'SortStrategy'));
reset($this->list);
}

public function current()
{
$item = current($this->list);
return $item->value;
}

public function key()
{
return key($this->list);
}

public function next()
{
next($this->list);
}

public function valid()
{
return null !== $this->key();
}

public function toArray()
{
return iterator_to_array($this);
Expand Down
64 changes: 37 additions & 27 deletions tests/PriorityListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,118 +7,128 @@ class PriorityListTest extends \PHPUnit_Framework_TestCase
public function testToArrayDesc()
{
$list = new PriorityList();

$this->assertEquals(array('a' => 'a', 'b' => 'b'), array('b' => 'b', 'a' => 'a'));

$list->set('k1', 'v1', 2);
$list->set('k2', 'v2', 8);
$list->set('k3', 'v3', 16);
$list->set('k4', 'v4', 1);
$list->set('k5', 'v5', 4);

$expectedArray = array(
'k3' => 'v3',
'k3' => 'v3',
'k2' => 'v2',
'k5' => 'v5',
'k1' => 'v1',
'k4' => 'v4',
);

foreach($list as $key => $value) {
$this->assertEquals(key($expectedArray), $key);
$this->assertEquals(current($expectedArray), $value);

next($expectedArray);
}

if(key($expectedArray)) {
$this->fail('Actual list less than expected');
}
}

public function testToArrayAsc()
{
$list = new PriorityList();

$list->setAscOrder();

$list->set('k1', 'v1', 2);
$list->set('k2', 'v2', 8);
$list->set('k3', 'v3', 16);
$list->set('k4', 'v4', 1);
$list->set('k5', 'v5', 4);

$expectedArray = array(
'k4' => 'v4',
'k1' => 'v1',
'k5' => 'v5',
'k2' => 'v2',
'k3' => 'v3',
);

foreach($list as $key => $value) {
$this->assertEquals(key($expectedArray), $key);
$this->assertEquals(current($expectedArray), $value);

next($expectedArray);
}

if(key($expectedArray)) {
$this->fail('Actual list less than expected');
}
}

public function testGet()
{
$list = new PriorityList();

$list->set('k1', 'v1', 2);
$list->set('k2', 'v2', 8);
$list->set('k3', 'v3', 16);
$list->set('k4', 'v4', 1);
$list->set('k5', 'v5', 4);

$this->assertEquals('v5', $list->get('k5'));
}


public function testHas()
{
$list = new PriorityList();

$list->set('k1', 'v1', 2);

$this->assertTrue($list->has('k1'));
$this->assertFalse($list->has('UNKNOWN_KEY'));
}

public function testGet_KeyNotExists()
{
$list = new PriorityList();

$list->set('k1', 'v1', 2);
$list->set('k2', 'v2', 8);
$list->set('k3', 'v3', 16);
$list->set('k4', 'v4', 1);
$list->set('k5', 'v5', 4);

$this->assertEquals(null, $list->get('KEY_NOT_EXISTS'));
}

public function testToArray()
{
$list = new PriorityList();

$list->set('k1', 'v1', 2);
$list->set('k2', 'v2', 8);
$list->set('k3', 'v3', 16);
$list->set('k4', 'v4', 1);
$list->set('k5', 'v5', 4);

$expectedArray = array(
'k3' => 'v3',
'k3' => 'v3',
'k2' => 'v2',
'k5' => 'v5',
'k1' => 'v1',
'k4' => 'v4',
);

foreach($list->toArray() as $key => $value) {
$this->assertEquals(key($expectedArray), $key);
$this->assertEquals(current($expectedArray), $value);

next($expectedArray);
}

if(key($expectedArray)) {
$this->fail('Actual list less than expected');
}
Expand Down

0 comments on commit b735972

Please sign in to comment.