Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Common/AbstractAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@ final public function __construct(array $items = [])
$this->items = $items;
}

/**
* {@inheritDoc}
*/
final public function has($key): bool
{
return $this->offsetExists($key);
}

/**
* {@inheritDoc}
*/
final public function get($key, $default = null)
{
if ($this->offsetExists($key)) {
Expand All @@ -38,36 +44,57 @@ final public function get($key, $default = null)
return $default;
}

/**
* {@inheritDoc}
*/
final public function set($key, $value)
{
$this->offsetSet($key, $value);
}

/**
* {@inheritDoc}
*/
public function remove($key)
{
$this->offsetUnset($key);
}

/**
* {@inheritDoc}
*/
final public function offsetExists($key)
{
return array_key_exists($key, $this->items);
}

/**
* {@inheritDoc}
*/
final public function offsetGet($key)
{
return $this->items[$key];
}

/**
* {@inheritDoc}
*/
final public function offsetSet($key, $value)
{
$this->items[$key] = $value;
}

/**
* {@inheritDoc}
*/
final public function offsetUnset($key)
{
unset($this->items[$key]);
}

/**
* {@inheritDoc}
*/
final public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->items);
Expand Down
54 changes: 51 additions & 3 deletions src/Common/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,86 +4,134 @@

final class Collection extends AbstractAggregate implements CollectionInterface
{
/**
* {@inheritDoc}
*/
public static function make(array $items = []): CollectionInterface
{
return new static($items);
}

/**
* {@inheritDoc}
*/
public function all(): array
{
return $this->items;
}

/**
* {@inheritDoc}
*/
public function first()
{
return $this->items[0];
}

/**
* {@inheritDoc}
*/
public function last()
{
$array = array_reverse($this->items);
return $array[0];
$count = count($this->items);
return $this->items[$count-1];
}

/**
* {@inheritDoc}
*/
public function random()
{
$key = array_rand($this->items);
return $this->get($key);
}

public function sortBy(string $fieldName): CollectionInterface
/**
* {@inheritDoc}
*/
public function sortBy(string $fieldName, bool $reverse = false): CollectionInterface
{
$results = $this->items;
usort($results, function ($item1, $item2) use ($fieldName) {
return $item1[$fieldName] <=> $item2[$fieldName];
});
if ($reverse) {
$results = array_reverse($results);
}
return new static($results);
}

/**
* {@inheritDoc}
*/
public function count(): int
{
return count($this->items);
}

/**
* {@inheritDoc}
*/
public function isEmpty(): bool
{
return empty($this->items);
}

/**
* {@inheritDoc}
*/
public function shuffle(): CollectionInterface
{
$items = $this->items;
shuffle($items);
return new static($items);
}

/**
* {@inheritDoc}
*/
public function limit(int $offset = 0, int $limit = null): CollectionInterface
{
return new static(array_slice($this->items, $offset, $limit));
}

/**
* {@inheritDoc}
*/
public function filter(callable $callback): CollectionInterface
{
return new static(array_filter($this->items, $callback));
}

/**
* {@inheritDoc}
*/
public function map(callable $callback): CollectionInterface
{
return new static(array_map($callback, $this->items));
}

/**
* {@inheritDoc}
*/
public function toArray(): array
{
return array_map(function ($value) {
return $value instanceof ArrayableInterface ? $value->toArray() : $value;
}, $this->items);
}

/**
* {@inheritDoc}
*/
public function toJson(): string
{
return json_encode($this->toArray());
}

/**
* {@inheritDoc}
*/
public function __toString(): string
{
return $this->toJson();
Expand Down
6 changes: 4 additions & 2 deletions src/Common/CollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ public function shuffle(): CollectionInterface;
public function limit(int $offset = 0, int $limit = null): CollectionInterface;

/**
* Returns a collection sorted by the provided field name.
* Returns a collection sorted by the provided field name. Optionally returns the
* sorted collection in reverse order.
*
* @param string $fieldName The field name to use when grouping results.
* @param boolean $reverse OPTIONAL Whether or not to return the sort in reverse order.
* @return CollectionInterface
*/
public function sortBy(string $fieldName): CollectionInterface;
public function sortBy(string $fieldName, bool $reverse = false): CollectionInterface;

/**
* Returns a collection filtered by the provided callback.
Expand Down
8 changes: 7 additions & 1 deletion tests/Common/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Test\Common;

use PHPUnit\Framework\TestCase;
use Guillermoandrae\Common\Collection;
use Guillermoandrae\Common\CollectionInterface;
use PHPUnit\Framework\TestCase;

class CollectionTest extends TestCase
{
Expand Down Expand Up @@ -108,6 +108,12 @@ public function testSortBy()
$this->assertTrue($sorted->first()['age'] < $sorted->last()['age']);
}

public function testSortByReverse()
{
$sorted = $this->collection->sortBy('age', true);
$this->assertTrue($sorted->first()['age'] > $sorted->last()['age']);
}

public function testFilter()
{
$collection = new Collection([1, 2, 3]);
Expand Down