From b735972901d88fe1796414277e7b47999f3f8478 Mon Sep 17 00:00:00 2001 From: Dmytro Sokil Date: Fri, 28 Nov 2014 22:14:19 +0200 Subject: [PATCH] add checker of key existance --- src/PriorityList.php | 43 ++++++++++++++----------- tests/PriorityListTest.php | 64 ++++++++++++++++++++++---------------- 2 files changed, 61 insertions(+), 46 deletions(-) diff --git a/src/PriorityList.php b/src/PriorityList.php index a5fe349..fdbdf64 100644 --- a/src/PriorityList.php +++ b/src/PriorityList.php @@ -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 @@ -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) { @@ -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) { @@ -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); diff --git a/tests/PriorityListTest.php b/tests/PriorityListTest.php index 29ee6b2..15ea9b1 100644 --- a/tests/PriorityListTest.php +++ b/tests/PriorityListTest.php @@ -7,47 +7,47 @@ 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', @@ -55,70 +55,80 @@ public function testToArrayAsc() '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'); }