Skip to content

Commit

Permalink
Update Last operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 15, 2020
1 parent e6e7116 commit ddde5cc
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,9 @@ public function keys(): CollectionInterface
return $this->run(new Keys());
}

public function last()
public function last(?callable $callback = null, $default = null)
{
return $this->transform(new Last());
return $this->transform(new Last($callback, $default));
}

public function limit(int $limit, int $offset = 0): CollectionInterface
Expand Down
2 changes: 1 addition & 1 deletion src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
* @template-extends Intersectkeysable<TKey, T>
* @template-extends Intersperseable<TKey, T>
* @template-extends Keysable<TKey, T>
* @template-extends Lastable<T>
* @template-extends Lastable<TKey, T>
* @template-extends Limitable<TKey, T>
* @template-extends Loopable<TKey, T>
* @template-extends Mapable<TKey, T>
Expand Down
5 changes: 2 additions & 3 deletions src/Contract/Transformation/Firstable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ interface Firstable
/**
* Get the first item from the collection passing the given truth test.
*
* @param callable $callback
* @psalm-param callable(T, TKey):(bool) $callback
* @psalm-param null|callable(T, TKey):(bool) $callback
*
* @param mixed $default
* @psalm-param T|null $default
*
* @return mixed
* @return mixed|null
* @psalm-return T|null
*/
public function first(?callable $callback = null, $default = null);
Expand Down
15 changes: 11 additions & 4 deletions src/Contract/Transformation/Lastable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
namespace loophp\collection\Contract\Transformation;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*/
interface Lastable
{
/**
* Get the last item.
* Get the last item from the collection passing the given truth test.
*
* @return mixed
* @psalm-return T
* @psalm-param null|callable(T, TKey):(bool) $callback
*
* @param mixed|null $default
* @psalm-param T|null $default
*
* @return mixed|null
* @psalm-return T|null
*/
public function last();
public function last(?callable $callback = null, $default = null);
}
67 changes: 51 additions & 16 deletions src/Transformation/Last.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Iterator;
use loophp\collection\Contract\Transformation;
use StdClass;

/**
* @psalm-template TKey
Expand All @@ -17,30 +18,64 @@
final class Last implements Transformation
{
/**
* @param Iterator<TKey, T> $collection
* @var callable
* @psalm-var callable(T, TKey):(bool)
*/
private $callback;

/**
* @var mixed|null
* @psalm-var T|null
*/
private $default;

/**
* @psalm-param callable(T, TKey):(bool)|null $callback
*
* @return mixed|null
* @psalm-return T|null
* @param mixed|null $default
*
* @psalm-param T|null $default
*/
public function __invoke(Iterator $collection)
public function __construct(?callable $callback = null, $default = null)
{
return (new FoldLeft(
$defaultCallback =
/**
* @param mixed $carry
* @psalm-param null|T $carry
*
* @param mixed $item
* @psalm-param T $item
*
* @param mixed $key
*
* @psalm-param TKey $key
*
* @return mixed
* @psalm-return T
* @param mixed $value
*
* @psalm-param T $value
*/
static function ($carry, $item, $key) {
return $item;
static function ($key, $value): bool {
return true;
};

$this->callback = $callback ?? $defaultCallback;
$this->default = $default;
}

/**
* @param Iterator<TKey, T> $collection
*
* @return mixed|null
* @psalm-return T|null
*/
public function __invoke(Iterator $collection)
{
$callback = $this->callback;
$default = $this->default;
$return = $nothing = new StdClass();

foreach ($collection as $key => $value) {
if (true === $callback($value, $key)) {
$return = $value;
}
))($collection);
}

return ($return !== $nothing) ?
$return :
$default;
}
}

0 comments on commit ddde5cc

Please sign in to comment.