Permalink
Browse files

Merge branch 'master' of https://github.com/dwightwatson/framework in…

…to dwightwatson-master
  • Loading branch information...
taylorotwell committed Nov 26, 2013
2 parents bb02c65 + e7598f4 commit 2f5402411717232f9cb9bacfd0bc3aee4f353716
@@ -113,4 +113,65 @@ public function modelKeys()
return array_map(function($m) { return $m->getKey(); }, $this->items);
}
/**
* Merge collection with another collection.
*
* @param \Illuminate\Support\Collection $collection
* @return \Illuminate\Support\Collection
*/
public function merge($collection)
{
foreach ($collection as $item)
{
if ( ! $this->contains($item->getKey()))
{
$this->add($item);
}
}
return $this;
}
/**
* Diff collection with another collection.
*
* @param \Illuminate\Support\Collection $collection
* @return \Illuminate\Support\Collection
*/
public function diff($collection)
{
$diff = new static;
foreach ($this->items as $item)
{
if ( ! $collection->contains($item->getKey()))
{
$diff->add($item);
}
}
return $diff;
}
/**
* Intersect collection with another collection.
*
* @param \Illuminate\Support\Collection $collection
* @return \Illuminate\Support\Collection
*/
public function intersect($collection)
{
$intersect = new static;
foreach ($this->items as $item)
{
if ($collection->contains($item->getKey()))
{
$intersect->add($item);
}
}
return $intersect;
}
}
@@ -341,20 +341,43 @@ public function collapse()
*/
public function merge($items)
{
if ($items instanceof Collection)
{
$items = $items->all();
}
elseif ($items instanceof ArrayableInterface)
{
$items = $items->toArray();
}
$items = $this->getArrayableItems($items);
$results = array_merge($this->items, $items);
return new static($results);
}
/**
* Diff items with the collection items.
*
* @param \Illuminate\Support\Collection|\Illuminate\Support\Contracts\ArrayableInterface|array $items
* @return \Illuminate\Support\Collection
*/
public function diff($items)
{
$items = $this->getArrayableItems($items);
$results = array_diff($this->items, $items);
return new static($results);
}
/**
* Intersect items with the collection items.
*
* @param \Illuminate\Support\Collection|\Illuminate\Support\Contracts\ArrayableInterface|array $items
* @return \Illuminate\Support\Collection
*/
public function intersect($items)
{
$items = $this->getArrayableItems($items);
$results = array_intersect($this->items, $items);
return new static($results);
}
/**
* Slice the underlying collection array.
*
@@ -547,4 +570,24 @@ public function __toString()
return $this->toJson();
}
/**
* Results array of items from Collection or ArrayableInterface.
*
* @param \Illuminate\Support\Collection|\Illuminate\Support\Contracts\ArrayableInterface|array $items
* @return array
*/
private function getArrayableItems($items)
{
if ($items instanceof Collection)
{
$items = $items->all();
}
elseif ($items instanceof ArrayableInterface)
{
$tiems = $items->toArray();
}
return $items;
}
}
@@ -90,6 +90,60 @@ public function testCollectionDictionaryReturnsModelKeys()
}
public function testCollectionMergesWithGivenCollection()
{
$one = m::mock('Illuminate\Database\Eloquent\Model');
$one->shouldReceive('getKey')->andReturn(1);
$two = m::mock('Illuminate\Database\Eloquent\Model');
$two->shouldReceive('getKey')->andReturn(2);
$three = m::mock('Illuminate\Database\Eloquent\Model');
$three->shouldReceive('getKey')->andReturn(3);
$c1 = new Collection(array($one, $two));
$c2 = new Collection(array($two, $three));
$this->assertEquals(new Collection(array($one, $two, $three)), $c1->merge($c2));
}
public function testCollectionDiffsWithGivenCollection()
{
$one = m::mock('Illuminate\Database\Eloquent\Model');
$one->shouldReceive('getKey')->andReturn(1);
$two = m::mock('Illuminate\Database\Eloquent\Model');
$two->shouldReceive('getKey')->andReturn(2);
$three = m::mock('Illuminate\Database\Eloquent\Model');
$three->shouldReceive('getKey')->andReturn(3);
$c1 = new Collection(array($one, $two));
$c2 = new Collection(array($two, $three));
$this->assertEquals(new Collection(array($one)), $c1->diff($c2));
}
public function testCollectionIntersectsWithGivenCollection()
{
$one = m::mock('Illuminate\Database\Eloquent\Model');
$one->shouldReceive('getKey')->andReturn(1);
$two = m::mock('Illuminate\Database\Eloquent\Model');
$two->shouldReceive('getKey')->andReturn(2);
$three = m::mock('Illuminate\Database\Eloquent\Model');
$three->shouldReceive('getKey')->andReturn(3);
$c1 = new Collection(array($one, $two));
$c2 = new Collection(array($two, $three));
$this->assertEquals(new Collection(array($two)), $c1->intersect($c2));
}
public function testLists()
{
$data = new Collection(array((object) array('name' => 'taylor', 'email' => 'foo'), (object) array('name' => 'dayle', 'email' => 'bar')));
@@ -154,6 +154,20 @@ public function testMergeCollection()
}
public function testDiffCollection()
{
$c = new Collection(array('id' => 1, 'first_word' => 'Hello'));
$this->assertEquals(array('id' => 1), $c->diff(new Collection(array('first_word' => 'Hello', 'last_word' => 'World')))->all());
}
public function testIntersectCollection()
{
$c = new Collection(array('id' => 1, 'first_word' => 'Hello'));
$this->assertEquals(array('first_word' => 'Hello'), $c->intersect(new Collection(array('first_world' => 'Hello', 'last_word' => 'World')))->all());
}
public function testCollapse()
{
$data = new Collection(array(array($object1 = new StdClass), array($object2 = new StdClass)));

0 comments on commit 2f54024

Please sign in to comment.