From d5fc83ecb79cbd6eed52bd02af6b1691cbbaf807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Collin?= Date: Tue, 8 Sep 2015 17:16:02 +0200 Subject: [PATCH] Fix EntityCollection lists() method for 5.0.x backward compatibility --- composer.json | 3 +- src/EntityCollection.php | 74 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 439296e..af759ef 100755 --- a/composer.json +++ b/composer.json @@ -15,8 +15,7 @@ "php": ">=5.5.0", "illuminate/database": "~5.0", "illuminate/events": "~5.0", - "illuminate/pagination": "~5.0", - "analogue/support": "~5.0" + "illuminate/pagination": "~5.0" }, "require-dev": { "phpunit/phpunit": "~4.0" diff --git a/src/EntityCollection.php b/src/EntityCollection.php index 4f5003d..8a0d154 100755 --- a/src/EntityCollection.php +++ b/src/EntityCollection.php @@ -4,8 +4,8 @@ use InvalidArgumentException; use Analogue\ORM\Mappable; use Analogue\ORM\System\Manager; -use Analogue\Support\EntityCollection as Collection; -use Illuminate\Support\Collection as BaseCollection; +use Illuminate\Support\Collection; +use Illuminate\Support\Arr; class EntityCollection extends Collection { @@ -333,6 +333,72 @@ protected function getEntityKey(Mappable $entity) return $entity->getEntityAttribute($keyName); } + /** + * Get the max value of a given key. + * + * @param string $key + * @return mixed + */ + public function max($key = null) + { + return $this->reduce(function($result, $item) use ($key) + { + return (is_null($result) || $item->getEntityAttribute($key) > $result) ? + $item->getEntityAttribute($key) : $result; + }); + } + + /** + * Get the min value of a given key. + * + * @param string $key + * @return mixed + */ + public function min($key = null) + { + return $this->reduce(function($result, $item) use ($key) + { + return (is_null($result) || $item->getEntityAttribute($key) < $result) + ? $item->getEntityAttribute($key) : $result; + }); + } + + /** + * Get an array with the values of a given key. + * + * @param string $value + * @param string $key + * @return static + */ + public function pluck($value, $key = null) + { + return new Collection(Arr::pluck($this->items, $value, $key)); + } + + /** + * Alias for the "pluck" method. + * + * @param string $value + * @param string $key + * @return static + */ + public function lists($value, $key = null) + { + return $this->pluck($value, $key); + } + + /** + * Return only unique items from the collection. + * + * @return static + */ + public function unique($key = null) + { + $dictionary = $this->getDictionary(); + + return new static(array_values($dictionary)); + } + /** * Get a base Support collection instance from this collection. * @@ -340,6 +406,6 @@ protected function getEntityKey(Mappable $entity) */ public function toBase() { - return new BaseCollection($this->items); + return new Collection($this->items); } -} \ No newline at end of file +}