Skip to content

Commit

Permalink
Fix All operation for standalone usage (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandruGG authored Nov 7, 2021
1 parent a895d51 commit 8a7852e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
10 changes: 10 additions & 0 deletions docs/pages/code/operations/all.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@

namespace App;

use ArrayIterator;
use Generator;
use loophp\collection\Collection;
use loophp\collection\Operation\All;
use loophp\collection\Operation\Filter;
use loophp\collection\Operation\Pipe;

include __DIR__ . '/../../../../vendor/autoload.php';

Expand All @@ -36,3 +40,9 @@

print_r($collection->all(false)); // ['foo' => 1, 'bar' => 2]
print_r($collection->all()); // [1, 2]

// Example 3 -> standalone operation usage
$even = static fn (int $value): bool => $value % 2 === 0;

$piped = Pipe::of()(Filter::of()($even), All::of()(true))(new ArrayIterator([1, 2, 3, 4]));
print_r(iterator_to_array($piped)); // [2, 4]
2 changes: 1 addition & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private function __construct(callable $callable, iterable $parameters = [])

public function all(bool $normalize = true): array
{
return All::of()($normalize)($this->getIterator());
return iterator_to_array(All::of()($normalize)($this->getIterator()));
}

public function append(...$items): CollectionInterface
Expand Down
10 changes: 4 additions & 6 deletions src/Operation/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,20 @@ final class All extends AbstractOperation
/**
* @pure
*
* @return Closure(bool): Closure(Iterator<TKey, T>): list<T>|array<TKey, T>
* @return Closure(bool): Closure(Iterator<TKey, T>): Iterator<int, T>|Iterator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @return Closure(Iterator<TKey, T>): list<T>|array<TKey, T>
* @return Closure(Iterator<TKey, T>): Iterator<int, T>|Iterator<TKey, T>
*/
static fn (bool $normalize): Closure =>
/**
* @param Iterator<TKey, T> $iterator
*
* @return array<TKey, T>|list<T>
* @return Iterator<int, T>|Iterator<TKey, T>
*/
static fn (Iterator $iterator): array => $normalize
? iterator_to_array((new Normalize())()($iterator))
: iterator_to_array($iterator);
static fn (Iterator $iterator): Iterator => $normalize ? (new Normalize())()($iterator) : $iterator;
}
}

0 comments on commit 8a7852e

Please sign in to comment.