Skip to content

Commit

Permalink
Add Current operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 7, 2020
1 parent 24fa777 commit ed246ed
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
17 changes: 17 additions & 0 deletions docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,22 @@ contains

Interface: `Containsable`_

current
~~~~~~~

Get the value of an item in the collection given a numeric index, default index is 0.

Interface: `Currentable`_

Signature: ``Collection::current(int $index = 0);``

.. code-block:: php
Collection::fromIterable(['a', 'b', 'c', 'd'])->current(); // Return 'a'
Collection::fromIterable(['a', 'b', 'c', 'd'])->current(0); // Return 'a'
Collection::fromIterable(['a', 'b', 'c', 'd'])->current(1); // Return 'b'
Collection::fromIterable(['a', 'b', 'c', 'd'])->current(10); // Return null
cycle
~~~~~

Expand Down Expand Up @@ -1537,6 +1553,7 @@ Signature: ``Collection::zip(iterable ...$iterables);``
.. _Combineable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Combineable.php
.. _Compactable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Compactable.php
.. _Containsable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Containsable.php
.. _Currentable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Currentable.php
.. _Cycleable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Cycleable.php
.. _Diffable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Diffable.php
.. _Diffkeysable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Diffkeysable.php
Expand Down
17 changes: 17 additions & 0 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,23 @@ public function it_can_get_an_iterator(): void
->shouldImplement(Iterator::class);
}

public function it_can_get_current()
{
$input = array_combine(range('A', 'E'), range('A', 'E'));

$this::fromIterable($input)
->current()
->shouldReturn('A');

$this::fromIterable($input)
->current(1)
->shouldReturn('B');

$this::fromIterable($input)
->current(10)
->shouldReturn(null);
}

public function it_can_get_items_with_only_specific_keys(): void
{
$this::fromIterable(range('A', 'E'))
Expand Down
6 changes: 6 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use loophp\collection\Operation\Compact;
use loophp\collection\Operation\Compose;
use loophp\collection\Operation\Contains;
use loophp\collection\Operation\Current;
use loophp\collection\Operation\Cycle;
use loophp\collection\Operation\Diff;
use loophp\collection\Operation\DiffKeys;
Expand Down Expand Up @@ -298,6 +299,11 @@ public function count(): int
return iterator_count($this);
}

public function current(int $index = 0)
{
return Current::of()($index)($this->getIterator());
}

public function cycle(): CollectionInterface
{
return $this->run(Cycle::of());
Expand Down
2 changes: 2 additions & 0 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use loophp\collection\Contract\Operation\Combineable;
use loophp\collection\Contract\Operation\Compactable;
use loophp\collection\Contract\Operation\Containsable;
use loophp\collection\Contract\Operation\Currentable;
use loophp\collection\Contract\Operation\Cycleable;
use loophp\collection\Contract\Operation\Diffable;
use loophp\collection\Contract\Operation\Diffkeysable;
Expand Down Expand Up @@ -179,6 +180,7 @@ interface Collection extends
Combineable,
Compactable,
Containsable,
Currentable,
Cycleable,
Diffable,
Diffkeysable,
Expand Down
18 changes: 18 additions & 0 deletions src/Contract/Operation/Currentable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace loophp\collection\Contract\Operation;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*/
interface Currentable
{
/**
* @psalm-return T
*/
public function current(int $index = 0);
}
43 changes: 43 additions & 0 deletions src/Operation/Current.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace loophp\collection\Operation;

use Closure;
use Iterator;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*/
final class Current extends AbstractOperation
{
/**
* @psalm-return Closure(int): Closure(Iterator<TKey, T>): T
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param int $index
*
* @psalm-return Closure(Iterator<TKey, T>): T
*/
static function (int $index): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return T
*/
static function (Iterator $iterator) use ($index) {
for ($i = 0; $i < $index; $i++, $iterator->next()) {
}

return $iterator->current();
};
};
}
}

0 comments on commit ed246ed

Please sign in to comment.