Skip to content

Commit

Permalink
Lorenzo's suggested changes to appendItem
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Ritter committed Mar 26, 2018
1 parent c7c4c35 commit 682fdad
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Collection/CollectionTrait.php
Expand Up @@ -411,6 +411,42 @@ public function append($items)
return new Collection($list);
}

/**
* {@inheritDoc}
*/
public function appendItem($item, $key = null)
{
if ($key !== null) {
$data = [$key => $item];
} else {
$data = [$item];
}

return $this->append($data);
}

/**
* {@inheritDoc}
*/
public function prepend($items)
{
return (new Collection($items))->append($this);
}

/**
* {@inheritDoc}
*/
public function prependItem($item, $key = null)
{
if ($key !== null) {
$data = [$key => $item];
} else {
$data = [$item];
}

return $this->prepend($data);
}

/**
* {@inheritDoc}
*/
Expand Down
59 changes: 59 additions & 0 deletions tests/TestCase/Collection/CollectionTest.php
Expand Up @@ -1149,6 +1149,65 @@ public function testAppend()
$this->assertEquals(['a' => 4, 'b' => 2, 'c' => 3], $combined->toArray());
}

/**
* Tests the appendItem method
*/
public function testAppendItem()
{
$collection = new Collection([1, 2, 3]);
$combined = $collection->appendItem(4);
$this->assertEquals([1, 2, 3, 4], $combined->toArray(false));

$collection = new Collection(['a' => 1, 'b' => 2]);
$combined = $collection->appendItem(3, 'c');
$combined = $combined->appendItem(4, 'a');
$this->assertEquals(['a' => 4, 'b' => 2, 'c' => 3], $combined->toArray());
}

/**
* Tests the prepend method
*/
public function testPrepend()
{
$collection = new Collection([1, 2, 3]);
$combined = $collection->prepend(['a']);
$this->assertEquals(['a', 1, 2, 3], $combined->toArray());

$collection = new Collection(['c' => 3, 'd' => 4]);
$combined = $collection->prepend(['a' => 1, 'b' => 2]);
$this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4], $combined->toArray());
}

/**
* Tests prependItem method
*/
public function testPrependItem()
{
$collection = new Collection([1, 2, 3]);
$combined = $collection->prependItem('a');
$this->assertEquals(['a', 1, 2, 3], $combined->toList());

$collection = new Collection(['c' => 3, 'd' => 4]);
$combined = $collection->prependItem(2, 'b');
$combined = $combined->prependItem(1, 'a');
$this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4], $combined->toArray());
}

/**
* Tests prependItem method
*/
public function testPrependItemPreserveKeys()
{
$collection = new Collection([1, 2, 3]);
$combined = $collection->prependItem('a');
$this->assertEquals(['a', 1, 2, 3], $combined->toList());

$collection = new Collection(['c' => 3, 'd' => 4]);
$combined = $collection->prependItem(2, 'b');
$combined = $combined->prependItem(1, 'a');
$this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4], $combined->toArray());
}

/**
* Tests the append method with iterator
*/
Expand Down

0 comments on commit 682fdad

Please sign in to comment.