Skip to content

Commit

Permalink
Update Append, Merge and Prepend operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jul 1, 2020
1 parent 02dd5be commit 9b77e0e
Show file tree
Hide file tree
Showing 50 changed files with 84 additions and 97 deletions.
78 changes: 72 additions & 6 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,46 @@ public function it_can_append_items(): void
$this
->beConstructedThrough('with', [['1', '2', '3']]);

$generator = static function (): Generator {
yield 0 => '1';

yield 1 => '2';

yield 2 => '3';

yield 0 => '4';
};

$this
->append('4')
->shouldIterateAs(['1', '2', '3', '4']);
->shouldIterateAs($generator());

$generator = static function (): Generator {
yield 0 => '1';

yield 1 => '2';

yield 2 => '3';

yield 0 => '5';

yield 1 => '6';
};

$this
->append('5', '6')
->shouldIterateAs(['1', '2', '3', '5', '6']);
->shouldIterateAs($generator());

$generator = static function (): Generator {
yield 0 => '1';

yield 1 => '2';

yield 2 => '3';
};

$this
->shouldIterateAs(['1', '2', '3']);
->shouldIterateAs($generator());
}

public function it_can_apply(): void
Expand Down Expand Up @@ -1001,12 +1031,34 @@ public function it_can_merge(): void
->beConstructedThrough('with', [range('A', 'E')]);

$collection = Collection::with(static function () {
yield from range('F', 'J');
return yield from range('F', 'J');
});

$generator = static function (): Generator {
yield 0 => 'A';

yield 1 => 'B';

yield 2 => 'C';

yield 3 => 'D';

yield 4 => 'E';

yield 0 => 'F';

yield 1 => 'G';

yield 2 => 'H';

yield 3 => 'I';

yield 4 => 'J';
};

$this
->merge($collection->all())
->shouldIterateAs(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']);
->shouldIterateAs($generator());
}

public function it_can_nth(): void
Expand Down Expand Up @@ -1171,9 +1223,23 @@ public function it_can_prepend(): void
$this
->beConstructedThrough('with', [range('D', 'F')]);

$generator = static function (): Generator {
yield 0 => 'A';

yield 1 => 'B';

yield 2 => 'C';

yield 0 => 'D';

yield 1 => 'E';

yield 2 => 'F';
};

$this
->prepend('A', 'B', 'C')
->shouldIterateAs(['A', 'B', 'C', 'D', 'E', 'F']);
->shouldIterateAs($generator());
}

public function it_can_reduce(): void
Expand Down
1 change: 0 additions & 1 deletion src/Contract/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public function __invoke(): Closure;

/**
* @param mixed|null $default
* @param string $key
*
* @return mixed|null
*/
Expand Down
2 changes: 0 additions & 2 deletions src/Contract/Operation/Cycleable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
interface Cycleable
{
/**
* @param int $length
*
* @return \loophp\collection\Base|\loophp\collection\Contract\Collection
*/
public function cycle(int $length = 0): Base;
Expand Down
2 changes: 0 additions & 2 deletions src/Contract/Operation/Flattenable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ interface Flattenable
/**
* Flatten a collection of items into a simple flat collection.
*
* @param int $depth
*
* @return \loophp\collection\Base|\loophp\collection\Contract\Collection
*/
public function flatten(int $depth = PHP_INT_MAX): Base;
Expand Down
2 changes: 0 additions & 2 deletions src/Contract/Operation/Intersperseable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ interface Intersperseable
* Indices are not preserved.
*
* @param mixed $element
* @param int $every
* @param int $startAt
*
* @return \loophp\collection\Base|\loophp\collection\Contract\Collection
*/
Expand Down
1 change: 0 additions & 1 deletion src/Contract/Operation/Iterateable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ interface Iterateable
{
/**
* @param mixed ...$parameters
* @param callable $callback
*
* @return \loophp\collection\Base|\loophp\collection\Contract\Collection
*/
Expand Down
2 changes: 0 additions & 2 deletions src/Contract/Operation/Limitable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ interface Limitable
/**
* Limit the first {$limit} items.
*
* @param int $limit
*
* @return \loophp\collection\Base|\loophp\collection\Contract\Collection
*/
public function limit(int $limit): Base;
Expand Down
3 changes: 0 additions & 3 deletions src/Contract/Operation/Nthable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ interface Nthable
/**
* Get every n-th element of a collection.
*
* @param int $step
* @param int $offset
*
* @return \loophp\collection\Base|\loophp\collection\Contract\Collection
*/
public function nth(int $step, int $offset = 0): Base;
Expand Down
1 change: 0 additions & 1 deletion src/Contract/Operation/Padable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ interface Padable
* Pad a collection to the given length with a given value.
*
* @param mixed $value
* @param int $size
*
* @return \loophp\collection\Base|\loophp\collection\Contract\Collection
*/
Expand Down
4 changes: 0 additions & 4 deletions src/Contract/Operation/Rangeable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ interface Rangeable
/**
* Create a new Collection with a range of number.
*
* @param float $start
* @param float $end
* @param float $step
*
* @return \loophp\collection\Base|\loophp\collection\Contract\Collection
*/
public static function range(float $start = 0.0, float $end = INF, float $step = 1.0): Base;
Expand Down
1 change: 0 additions & 1 deletion src/Contract/Operation/Reductionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ interface Reductionable
* Reduce a collection of items through a given callback.
*
* @param mixed $initial
* @param callable $callback
*
* @return \loophp\collection\Base|\loophp\collection\Contract\Collection
*/
Expand Down
2 changes: 0 additions & 2 deletions src/Contract/Operation/Scaleable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ interface Scaleable
/**
* Scale/normalize values.
*
* @param float $lowerBound
* @param float $upperBound
* @param ?float $wantedLowerBound
* @param ?float $wantedUpperBound
* @param ?float $base
Expand Down
1 change: 0 additions & 1 deletion src/Contract/Operation/Sliceable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ interface Sliceable
/**
* Get a slice of a collection.
*
* @param int $offset
* @param ?int $length
*
* @return \loophp\collection\Base|\loophp\collection\Contract\Collection
Expand Down
2 changes: 0 additions & 2 deletions src/Contract/Operation/Sortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ interface Sortable
/**
* Sort a collection using a callback.
*
* @param callable|null $callable
*
* @return \loophp\collection\Base|\loophp\collection\Contract\Collection
*/
public function sort(?callable $callable = null): Base;
Expand Down
2 changes: 0 additions & 2 deletions src/Contract/Operation/Tailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ interface Tailable
/**
* Get last collection items of a collection.
*
* @param int $length
*
* @return \loophp\collection\Base|\loophp\collection\Contract\Collection
*/
public function tail(int $length = 1): Base;
Expand Down
3 changes: 0 additions & 3 deletions src/Contract/Operation/Timesable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ interface Timesable
/**
* Create a new instance by invoking the callback a given amount of times.
*
* @param int $number
* @param callable|null $callback
*
* @return \loophp\collection\Base|\loophp\collection\Contract\Collection
*/
public static function times(int $number = 0, ?callable $callback = null): Base;
Expand Down
1 change: 0 additions & 1 deletion src/Contract/Transformation/Firstable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ interface Firstable
/**
* Get the first item from the collection passing the given truth test.
*
* @param callable|null $callback
* @param mixed $default
*
* @return mixed
Expand Down
1 change: 0 additions & 1 deletion src/Contract/Transformation/FoldLeftable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ interface FoldLeftable
* Fold the collection from the left to the right.
*
* @param mixed $initial
* @param callable $callback
*
* @return mixed
*/
Expand Down
1 change: 0 additions & 1 deletion src/Contract/Transformation/FoldRightable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ interface FoldRightable
* Fold the collection from the right to the left.
*
* @param mixed $initial
* @param callable $callback
*
* @return mixed
*/
Expand Down
1 change: 0 additions & 1 deletion src/Contract/Transformation/Reduceable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ interface Reduceable
* Reduce the collection to a single value.
*
* @param mixed $initial
* @param callable $callback
*
* @return mixed
*/
Expand Down
1 change: 0 additions & 1 deletion src/Iterator/ClosureIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ final class ClosureIterator implements Iterator
* ClosureIterator constructor.
*
* @param mixed ...$arguments
* @param callable $callable
*/
public function __construct(callable $callable, ...$arguments)
{
Expand Down
1 change: 0 additions & 1 deletion src/Iterator/SortableIterableIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ final class SortableIterableIterator implements IteratorAggregate
* SortableIterator constructor.
*
* @param iterable<mixed> $iterable
* @param callable $callable
*/
public function __construct(iterable $iterable, callable $callable)
{
Expand Down
1 change: 0 additions & 1 deletion src/Operation/AbstractOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ abstract class AbstractOperation

/**
* @param mixed|null $default
* @param string $key
*
* @return mixed|null
*/
Expand Down
9 changes: 4 additions & 5 deletions src/Operation/Append.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ public function __invoke(): Closure
return
/**
* @param array<int, mixed> $items
* @param iterable $collection
*/
static function (iterable $collection, array $items): Generator {
foreach ($collection as $value) {
yield $value;
foreach ($collection as $key => $value) {
yield $key => $value;
}

foreach ($items as $item) {
yield $item;
foreach ($items as $key => $item) {
yield $key => $item;
}
};
}
Expand Down
1 change: 0 additions & 1 deletion src/Operation/Apply.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public function __invoke(): Closure
return
/**
* @param array<int, callable> $callbacks
* @param iterable $collection
*/
static function (iterable $collection, array $callbacks): Generator {
foreach ($collection as $key => $value) {
Expand Down
1 change: 0 additions & 1 deletion src/Operation/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public function __invoke(): Closure
return
/**
* @param array<int, int> $sizes
* @param iterable $collection
*/
static function (iterable $collection, array $sizes): Generator {
$sizes = new IterableIterator(
Expand Down
1 change: 0 additions & 1 deletion src/Operation/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public function __invoke(): Closure
return
/**
* @param int|string $column
* @param iterable $collection
*/
static function (iterable $collection, $column): Generator {
foreach ((new Run((new Transpose())))($collection) as $key => $value) {
Expand Down
1 change: 0 additions & 1 deletion src/Operation/Combinate.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public function __invoke(): Closure

/**
* @param array<mixed> $dataset
* @param int $length
*
* @return Generator<array<mixed>>
*/
Expand Down
1 change: 0 additions & 1 deletion src/Operation/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function __invoke(): Closure
return
/**
* @param array<int, callable> $callbacks
* @param iterable $collection
*/
static function (iterable $collection, array $callbacks): Generator {
$iterator = new IterableIterator($collection);
Expand Down
1 change: 0 additions & 1 deletion src/Operation/Forget.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public function __invoke(): Closure
return
/**
* @param array<int, mixed> $keys
* @param iterable $collection
*/
static function (iterable $collection, array $keys): Generator {
$keys = array_flip($keys);
Expand Down
5 changes: 0 additions & 5 deletions src/Operation/Intersperse.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ final class Intersperse extends AbstractOperation implements Operation
* Intersperse constructor.
*
* @param mixed $element
* @param int $atEvery
* @param int $startAt
*/
public function __construct($element, int $atEvery = 1, int $startAt = 0)
{
Expand All @@ -49,9 +47,6 @@ public function __invoke(): Closure
return
/**
* @param mixed $element
* @param iterable $collection
* @param int $every
* @param int $startAt
*/
static function (iterable $collection, $element, int $every, int $startAt): Generator {
foreach ($collection as $value) {
Expand Down
Loading

0 comments on commit 9b77e0e

Please sign in to comment.