Skip to content

Commit

Permalink
Fix EntityCollection lists() method for 5.0.x backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
RemiCollin committed Sep 8, 2015
1 parent fa42d95 commit d5fc83e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
74 changes: 70 additions & 4 deletions src/EntityCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -333,13 +333,79 @@ 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.
*
* @return \Illuminate\Support\Collection
*/
public function toBase()
{
return new BaseCollection($this->items);
return new Collection($this->items);
}
}
}

0 comments on commit d5fc83e

Please sign in to comment.