Skip to content
Browse files

Merge pull request #6 from xi-project/add

Added Collection::add, which adds a value to collection and returns a new collection
  • Loading branch information...
2 parents 569e8cd + 933bcdd commit be2faf040ed560372e4c69f40b3fe803d5faf5cb @Ezku Ezku committed Dec 14, 2012
View
12 library/Xi/Collections/Collection.php
@@ -181,4 +181,14 @@ public function sortWith($comparator);
* @return Collection
*/
public function sortBy($metric);
-}
+
+ /**
+ * Get a new Collection with given value and optionally key appended.
+ * Maintains index associations.
+ *
+ * @param mixed $value
+ * @param mixed $key
+ * @return Collection
+ */
+ public function add($value, $key = null);
+}
View
18 library/Xi/Collections/Collection/AbstractCollection.php
@@ -146,4 +146,20 @@ public function sortBy($metric)
{
return $this->apply(Functions::sortBy($metric));
}
-}
+
+ /**
+ * {@inheritdoc}
+ */
+ public function add($value, $key = null)
+ {
+ $results = $this->toArray();
+
+ if ($key === null) {
+ $results[] = $value;
+ } else {
+ $results[$key] = $value;
+ }
+
+ return static::create($results);
+ }
+}
View
18 library/Xi/Collections/Collection/ArrayCollection.php
@@ -176,4 +176,20 @@ public function merge(Collection $other)
{
return static::create(array_merge($this->_elements, $other->toArray()));
}
-}
+
+ /**
+ * {@inheritdoc}
+ */
+ public function add($value, $key = null)
+ {
+ $results = $this->toArray();
+
+ if ($key === null) {
+ $results[] = $value;
+ } else {
+ $results[$key] = $value;
+ }
+
+ return static::create($results);
+ }
+}
View
10 library/Xi/Collections/Collection/OuterCollection.php
@@ -141,4 +141,12 @@ public function sortBy($metric)
{
return static::create($this->collection->sortBy($metric));
}
-}
+
+ /**
+ * {@inheritdoc}
+ */
+ public function add($value, $key = null)
+ {
+ return static::create($this->collection->add($value, $key));
+ }
+}
View
57 tests/Xi/Collections/Collection/AbstractCollectionTest.php
@@ -517,4 +517,59 @@ public function shouldBeAbleToSortByMetric($elements, $expected)
});
$this->assertEquals($expected, $result->toArray());
}
-}
+
+ /**
+ * @test
+ * @dataProvider addValue
+ *
+ * @param array $elements
+ * @param mixed $value
+ * @param array $expected
+ */
+ public function shouldBeAbleToAddValue(array $elements, $value, array $expected)
+ {
+ $collection = $this->getCollection($elements);
+ $result = $collection->add($value);
+
+ $this->assertEquals($expected, $result->toArray());
+ }
+
+ /**
+ * @return array
+ */
+ public function addValue()
+ {
+ return array(
+ array(array('a', 'b'), 'c', array('a', 'b', 'c')),
+ array(array(1, 3), 2, array(1, 3, 2)),
+ );
+ }
+
+ /**
+ * @test
+ * @dataProvider addKeyAndValue
+ *
+ * @param array $elements
+ * @param mixed $key
+ * @param mixed $value
+ * @param array $expected
+ */
+ public function shouldBeAbleToAddKeyAndValue(array $elements, $key, $value, array $expected)
+ {
+ $collection = $this->getCollection($elements);
+ $result = $collection->add($value, $key);
+
+ $this->assertEquals($expected, $result->toArray());
+ }
+
+ /**
+ * @return array
+ */
+ public function addKeyAndValue()
+ {
+ return array(
+ array(array('a' => 'b'), 'c', 'd', array('a' => 'b', 'c' => 'd')),
+ array(array(1 => 'a', 3 => 'b'), 2, 'c', array(1 => 'a', 3 => 'b', 2 => 'c')),
+ );
+ }
+}

0 comments on commit be2faf0

Please sign in to comment.
Something went wrong with that request. Please try again.