Permalink
Browse files

Added Map::pop() (similar to #68)

  • Loading branch information...
1 parent a9a3a49 commit 325eef585fd500f7598a840491b25860d1f9048e @jmalloc jmalloc committed Mar 4, 2014
Showing with 35 additions and 1 deletion.
  1. +13 −0 src/Map.php
  2. +1 −1 src/Set.php
  3. +21 −0 test/suite/MapTest.php
View
@@ -790,6 +790,19 @@ public function tryRemove($key, &$value = null)
}
/**
+ * Remove and return an element from the map.
+ *
+ * There is no guarantee as to which element will be returned.
+ *
+ * @return tuple<mixed, mixed> The element.
+ * @throws Exception\EmptyCollectionException if the collection is empty.
+ */
+ public function pop()
+ {
+ return $this->elements->popBack();
+ }
+
+ /**
* Add the elements from one or more other collections to this collection.
*
* Any existing keys are overwritten from left to right.
View
@@ -506,7 +506,7 @@ public function removeMany($elements)
*
* There is no guarantee as to which element will be returned.
*
- * @return mixed The element.
+ * @return mixed The element.
* @throws Exception\EmptyCollectionException if the collection is empty.
*/
public function pop()
View
@@ -635,6 +635,27 @@ public function testTryRemove()
$this->assertFalse($this->collection->hasKey('a'));
}
+ public function testPop()
+ {
+ $source = new Map(
+ array('a' => 1, 'b' => 2, 'c' => 3)
+ );
+
+ $this->collection->mergeInPlace($source);
+
+ list($key, $value) = $this->collection->pop();
+
+ $this->assertSame($source->get($key), $value);
+ $this->assertFalse($this->collection->hasKey($key));
+ }
+
+ public function testPopWithEmptySet()
+ {
+ $this->setExpectedException('Icecave\Collections\Exception\EmptyCollectionException');
+
+ $this->collection->pop();
+ }
+
public function testMergeInPlace()
{
$this->collection->set(10, 1);

0 comments on commit 325eef5

Please sign in to comment.