Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: loophp/collection
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0.0.3
Choose a base ref
...
head repository: loophp/collection
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 0.0.4
Choose a head ref
  • 4 commits
  • 17 files changed
  • 1 contributor

Commits on Aug 20, 2019

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    89aa7a1 View commit details
  2. Add .gitattributes.

    drupol committed Aug 20, 2019
    Copy the full SHA
    a41491c View commit details
  3. Add Run operation.

    drupol committed Aug 20, 2019
    Copy the full SHA
    1bf34d4 View commit details
  4. Update README.

    drupol committed Aug 20, 2019
    Copy the full SHA
    b4050b5 View commit details
9 changes: 9 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/tests export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.scrutinizer.yml export-ignore
.travis.yml export-ignore
phpunit.xml.dist export-ignore
infection.json.dist export-ignore
grumphp.yml.dist export-ignore
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -205,7 +205,7 @@ the methods always return the same values for the same inputs.
| `proxy` | new Collection object | [Proxy.php](./src/Operation/Proxy.php)
| `rebase` | new Collection object | [Rebase.php](./src/Operation/Rebase.php)
| `reduce` | mixed | [Collection.php](./src/Collection.php)
| `run` | new Collection object | [Collection.php](./src/Collection.php)
| `run` | mixed | [Run.php](./src/Operation/Run.php)
| `skip` | new Collection object | [Skip.php](./src/Operation/Skip.php)
| `slice` | new Collection object | [Slice.php](./src/Operation/Slice.php)
| `walk` | new Collection object | [Walk.php](./src/Operation/Walk.php)
82 changes: 76 additions & 6 deletions spec/drupol/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
@@ -38,7 +38,23 @@ public function it_can_apply(): void

$this
->apply(static function ($item) {
return (bool) ($item % 2);
// do what you want here.

return true;
})
->shouldIterateAs(\range(1, 10));

$this
->apply(static function ($item) {
// do what you want here.

return false;
})
->shouldIterateAs(\range(1, 10));

$this
->apply(static function ($item): void {
$item %= 2;
})
->shouldIterateAs([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

@@ -108,6 +124,24 @@ public function it_can_apply(): void
})
->all()
->shouldReturn($context);

$context = [];

$applyCallback1 = static function ($item, $key) use (&$context) {
$context[] = $item;

return true;
};

$walkCallback2 = static function ($item) {
return $item;
};

$this::with(\range(1, 20))
->apply($applyCallback1)
->walk($walkCallback2)
->all()
->shouldReturn($context);
}

public function it_can_be_constructed_from_array(): void
@@ -651,12 +685,48 @@ static function ($carry, $item) {

public function it_can_run_an_operation(Operation $operation): void
{
$operation
->run($this)
->shouldBeCalledOnce();
$square = new class() extends \drupol\collection\Operation\Operation {
public function run(CollectionInterface $collection)
{
return Collection::with(
static function () use ($collection) {
foreach ($collection as $item) {
yield $item ** 2;
}
}
);
}
};

$this
->run($operation);
$sqrt = new class() extends \drupol\collection\Operation\Operation {
public function run(CollectionInterface $collection)
{
return Collection::with(
static function () use ($collection) {
foreach ($collection as $item) {
yield $item ** .5;
}
}
);
}
};

$map = new class() extends \drupol\collection\Operation\Operation {
public function run(CollectionInterface $collection)
{
return Collection::with(
static function () use ($collection) {
foreach ($collection as $item) {
yield (int) $item;
}
}
);
}
};

$this::with(\range(1, 5))
->run($square, $sqrt, $map)
->shouldIterateAs(\range(1, 5));
}

public function it_can_skip(): void
45 changes: 15 additions & 30 deletions src/Collection.php
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@
use drupol\collection\Operation\Range;
use drupol\collection\Operation\Rebase;
use drupol\collection\Operation\Reduce;
use drupol\collection\Operation\Run;
use drupol\collection\Operation\Skip;
use drupol\collection\Operation\Slice;
use drupol\collection\Operation\Walk;
@@ -63,15 +64,15 @@ public function all(): array
*/
public function append(...$items): CollectionInterface
{
return $this->run(Append::with(...$items));
return $this->run(Append::with($items));
}

/**
* {@inheritdoc}
*/
public function apply(callable $callback): CollectionInterface
public function apply(callable ...$callables): CollectionInterface
{
return $this->run(Apply::with($callback));
return $this->run(Apply::with($callables));
}

/**
@@ -95,7 +96,7 @@ public function collapse(): CollectionInterface
*/
public function combine($keys): CollectionInterface
{
return $this->run(Combine::with(...$keys));
return $this->run(Combine::with($keys));
}

/**
@@ -159,7 +160,7 @@ public function flip(): CollectionInterface
*/
public function forget(...$keys): CollectionInterface
{
return $this->run(Forget::with(...$keys));
return $this->run(Forget::with($keys));
}

/**
@@ -199,15 +200,15 @@ public function limit(int $limit): CollectionInterface
*/
public function map(callable ...$callbacks): CollectionInterface
{
return $this->run(Walk::with(...$callbacks), Normalize::with());
return $this->run(Walk::with($callbacks), Normalize::with());
}

/**
* {@inheritdoc}
*/
public function merge(...$sources): CollectionInterface
{
return $this->run(Merge::with(...\array_map([$this, 'makeIterator'], $sources)));
return $this->run(Merge::with(\array_map([$this, 'makeIterator'], $sources)));
}

/**
@@ -231,7 +232,7 @@ public function nth(int $step, int $offset = 0): CollectionInterface
*/
public function only(...$keys): CollectionInterface
{
return $this->run(Only::with(...$keys));
return $this->run(Only::with($keys));
}

/**
@@ -255,7 +256,7 @@ public function pluck($pluck, $default = null): CollectionInterface
*/
public function prepend(...$items): CollectionInterface
{
return $this->run(Prepend::with(...$items));
return $this->run(Prepend::with($items));
}

/**
@@ -301,15 +302,15 @@ public function reduce(callable $callback, $initial = null)
*/
public function run(Operation ...$operations)
{
return \array_reduce($operations, [$this, 'doRun'], $this);
return Run::with($operations)->run($this);
}

/**
* {@inheritdoc}
*/
public function skip(int ...$counts): CollectionInterface
{
return $this->run(Skip::with(...$counts));
return $this->run(Skip::with($counts));
}

/**
@@ -350,13 +351,13 @@ static function () use ($number) {
*/
public function walk(callable ...$callbacks): CollectionInterface
{
return $this->run(Walk::with(...$callbacks));
return $this->run(Walk::with($callbacks));
}

/**
* Create a new collection instance.
*
* @param null|array|callable|Closure|CollectionInterface $data
* @param null|array|callable|Closure|CollectionInterface|\Iterator $data
*
* @return \drupol\collection\Contract\Collection
*/
@@ -374,23 +375,7 @@ public static function with($data = []): CollectionInterface
*/
public function zip(...$items): CollectionInterface
{
return $this->run(Zip::with(...$items));
}

/**
* Run an operation on the collection.
*
* @param \drupol\collection\Contract\Collection $collection
* The collection.
* @param \drupol\collection\Contract\Operation $operation
* The operation.
*
* @return mixed
* The operation result.
*/
private function doRun(CollectionInterface $collection, Operation $operation)
{
return $operation->run($collection);
return $this->run(Zip::with($items));
}

/**
4 changes: 2 additions & 2 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
@@ -31,11 +31,11 @@ public function append(...$items): self;
/**
* Apply a callback to all the element of an array.
*
* @param callable $callable
* @param callable ...$callables
*
* @return \drupol\collection\Contract\Collection
*/
public function apply(callable $callable): self;
public function apply(callable ...$callables): self;

/**
* Chunk the collection into chunks of the given size.
2 changes: 1 addition & 1 deletion src/Operation/Append.php
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ final class Append extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
$items = $this->parameters;
[$items] = $this->parameters;

return Collection::with(
static function () use ($items, $collection) {
12 changes: 6 additions & 6 deletions src/Operation/Apply.php
Original file line number Diff line number Diff line change
@@ -17,13 +17,13 @@ final class Apply extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
[$callback] = $this->parameters;
[$callbacks] = $this->parameters;

$newCollection = Collection::with($collection);

foreach ($newCollection as $key => $item) {
if (false === $callback($item, $key)) {
break;
foreach ($callbacks as $callback) {
foreach (Collection::with($collection) as $key => $item) {
if (false === $callback($item, $key)) {
break;
}
}
}

2 changes: 1 addition & 1 deletion src/Operation/Combine.php
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ final class Combine extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
$keys = $this->parameters;
[$keys] = $this->parameters;

return Collection::with(
static function () use ($keys, $collection) {
2 changes: 1 addition & 1 deletion src/Operation/Forget.php
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ final class Forget extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
$keys = $this->parameters;
[$keys] = $this->parameters;

return Collection::with(
static function () use ($keys, $collection) {
2 changes: 1 addition & 1 deletion src/Operation/Merge.php
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ final class Merge extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
$sources = $this->parameters;
[$sources] = $this->parameters;

return Collection::with(
static function () use ($sources, $collection) {
2 changes: 1 addition & 1 deletion src/Operation/Only.php
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ final class Only extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
$keys = $this->parameters;
[$keys] = $this->parameters;

return Collection::with(
static function () use ($keys, $collection) {
2 changes: 1 addition & 1 deletion src/Operation/Prepend.php
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ final class Prepend extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
$items = $this->parameters;
[$items] = $this->parameters;

return Collection::with(
static function () use ($items, $collection) {
2 changes: 1 addition & 1 deletion src/Operation/Rebase.php
Original file line number Diff line number Diff line change
@@ -17,6 +17,6 @@ final class Rebase extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
return Collection::with($collection->all());
return Collection::with($collection->getIterator());
}
}
39 changes: 39 additions & 0 deletions src/Operation/Run.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace drupol\collection\Operation;

use drupol\collection\Contract\Collection as CollectionInterface;

/**
* Class Run.
*/
final class Run extends Operation
{
/**
* {@inheritdoc}
*/
public function run(CollectionInterface $collection)
{
[$operations] = $this->parameters;

return \array_reduce($operations, [$this, 'doRun'], $collection);
}

/**
* Run an operation on the collection.
*
* @param \drupol\collection\Contract\Collection $collection
* The collection.
* @param \drupol\collection\Operation\Operation $operation
* The operation.
*
* @return mixed
* The operation result.
*/
private function doRun(CollectionInterface $collection, Operation $operation)
{
return $operation->run($collection);
}
}
Loading