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.1
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.2
Choose a head ref
  • 8 commits
  • 28 files changed
  • 1 contributor

Commits on Aug 12, 2019

  1. Copy the full SHA
    83a4e4d View commit details
  2. Copy the full SHA
    a1143c7 View commit details
  3. Increase code coverage.

    drupol committed Aug 12, 2019
    Copy the full SHA
    e73924a View commit details
  4. Update code.

    drupol committed Aug 12, 2019
    Copy the full SHA
    d95b896 View commit details
  5. Minor README file update.

    drupol committed Aug 12, 2019
    Copy the full SHA
    6659473 View commit details

Commits on Aug 13, 2019

  1. Copy the full SHA
    7677b5d View commit details
  2. Minor code changes.

    drupol committed Aug 13, 2019
    Copy the full SHA
    d645d47 View commit details
  3. Update composer.json.

    drupol committed Aug 13, 2019
    Copy the full SHA
    c9aff7b View commit details
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ This library has been inspired by the [Laravel Support Package](https://github.c
```php
<?php

declare(strict_types = 1);
declare(strict_types=1);

include 'vendor/autoload.php';

@@ -140,7 +140,7 @@ the `drupol\Collection\Contract\Operation` interface, then run it through the `C
```php
<?php

declare(strict_types = 1);
declare(strict_types=1);

include 'vendor/autoload.php';

5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "drupol/collection",
"type": "library",
"description": "A (memory) friendly and easy to use collection object.",
"description": "A (memory) friendly, easy, lazy and modular collection class.",
"keywords": [
"collection",
"generator",
@@ -19,12 +19,13 @@
"php": ">= 7.1.3"
},
"require-dev": {
"drupol/php-conventions": "^1.5.8",
"drupol/php-conventions": "^1.5.11",
"drupol/phpspec-annotation": "^1.2",
"drupol/phpspec-code-coverage": "^5",
"infection/infection": "^0.13",
"phpspec/phpspec": "^5",
"phpstan/phpstan": "^0.11",
"phpstan/phpstan-strict-rules": "^0.11.1",
"scrutinizer/ocular": "^1.6"
},
"config": {
64 changes: 63 additions & 1 deletion spec/drupol/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace spec\drupol\collection;

@@ -53,6 +53,57 @@ public function it_can_apply(): void
$this
->shouldThrow(\Exception::class)
->during('apply', [$callback]);

$context = [];

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

return 0;
}

if (3 <= $item && 6 > $item) {
$context[] = true;

return true;
}

if (9 < $item) {
$context[] = 'nine';

return false;
}

$context[] = $item;
};

$walkCallback2 = static function ($item) {
if (3 > $item) {
return 0;
}

if (3 <= $item && 6 > $item) {
return true;
}

if (10 === $item) {
return 'nine';
}

if (10 < $item) {
return false;
}

return $item;
};

$this::withArray(range(1, 20))
->apply($applyCallback1)
->walk($walkCallback2)
->filter(static function ($item) {return false !== $item; })
->all()
->shouldReturn($context);
}

public function it_can_be_constructed_from_array(): void
@@ -513,6 +564,13 @@ public function it_can_use_range(): void

$this::range(1, 10, 2)
->shouldIterateAs([1, 3, 5, 7, 9]);

$this::range(-5, 5, 2)
->shouldIterateAs([0 => -5, 1 => -3, 2 => -1, 3 => 1, 4 => 3]);

$this::range()
->limit(10)
->shouldIterateAs([0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9]);
}

public function it_can_use_range_with_value_1(): void
@@ -549,6 +607,10 @@ public function it_can_use_times_without_a_callback(): void
$this::times(-5)
->all()
->shouldReturn([]);

$this::times(1)
->all()
->shouldReturn([1]);
}

public function it_can_walk(): void
26 changes: 11 additions & 15 deletions src/Collection.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace drupol\collection;

@@ -279,7 +279,7 @@ public function prepend(...$items): CollectionInterface
*/
public static function range(int $start = 0, $end = INF, $step = 1): CollectionInterface
{
return (new static())->run(Range::with($start, $end, $step));
return self::withArray([])->run(Range::with($start, $end, $step));
}

/**
@@ -356,7 +356,7 @@ public function walk(callable ...$callbacks): CollectionInterface
/**
* Create a new collection instance.
*
* @param null|array|callable|Closure|CollectionInterface $data
* @param null|array|callable|Closure|Collection $data
*
* @return \drupol\collection\Contract\Collection
*/
@@ -370,33 +370,29 @@ public static function with($data = []): CollectionInterface
}

/**
* @param array $array
* @param array $data
*
* @return \drupol\collection\Contract\Collection
*/
public static function withArray(array $array): CollectionInterface
public static function withArray(array $data): CollectionInterface
{
$instance = new static();

$instance->source = static function () use ($array) {
yield from $array;
};

return $instance;
return self::withClosure(static function () use ($data) {
yield from $data;
});
}

/**
* Create a new collection instance.
*
* @param callable $callback
* @param callable $callable
*
* @return \drupol\collection\Contract\Collection
*/
public static function withClosure(callable $callback): CollectionInterface
public static function withClosure(callable $callable): CollectionInterface
{
$instance = new static();

$instance->source = $callback;
$instance->source = $callable;

return $instance;
}
33 changes: 11 additions & 22 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace drupol\collection\Contract;

@@ -185,6 +185,16 @@ public function nth(int $step, int $offset = 0): self;
*/
public function only(...$keys): self;

/**
* TODO: Pad.
*
* @param int $size
* @param mixed $value
*
* @return \drupol\collection\Contract\Collection
*/
public function pad(int $size, $value): self;

/**
* Push an item onto the beginning of the collection.
*
@@ -237,27 +247,6 @@ public function slice(int $offset, int $length = null): self;
*/
public function walk(callable ...$callbacks): self;

/**
* @param mixed $data
*
* @return \drupol\collection\Contract\Collection
*/
public static function with($data = []): self;

/**
* @param array $data
*
* @return \drupol\collection\Contract\Collection
*/
public static function withArray(array $data): self;

/**
* @param callable $callable
*
* @return \drupol\collection\Contract\Collection
*/
public static function withClosure(callable $callable): self;

/**
* Zip the collection together with one or more arrays.
*
2 changes: 1 addition & 1 deletion src/Contract/Operation.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace drupol\collection\Contract;

9 changes: 5 additions & 4 deletions src/Operation/Append.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace drupol\collection\Operation;

use drupol\collection\Contract\Collection;
use drupol\collection\Collection;
use drupol\collection\Contract\Collection as CollectionInterface;

/**
* Class Append.
@@ -14,11 +15,11 @@ final class Append extends Operation
/**
* {@inheritdoc}
*/
public function run(Collection $collection): Collection
public function run(CollectionInterface $collection): CollectionInterface
{
$items = $this->parameters;

return $collection::withClosure(
return Collection::withClosure(
static function () use ($items, $collection) {
foreach ($collection as $item) {
yield $item;
13 changes: 7 additions & 6 deletions src/Operation/Chunk.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace drupol\collection\Operation;

use drupol\collection\Contract\Collection;
use drupol\collection\Collection;
use drupol\collection\Contract\Collection as CollectionInterface;

/**
* Class Chunk.
@@ -14,15 +15,15 @@ final class Chunk extends Operation
/**
* {@inheritdoc}
*/
public function run(Collection $collection): Collection
public function run(CollectionInterface $collection): CollectionInterface
{
$size = $this->parameters[0];

if (0 >= $size) {
return $collection::with();
return Collection::with();
}

return $collection::withClosure(
return Collection::withClosure(
static function () use ($size, $collection) {
$iterator = $collection->getIterator();

@@ -33,7 +34,7 @@ static function () use ($size, $collection) {
$values[$iterator->key()] = $iterator->current();
}

yield $collection::withArray($values);
yield Collection::withArray($values);
}
}
);
11 changes: 6 additions & 5 deletions src/Operation/Collapse.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace drupol\collection\Operation;

use drupol\collection\Contract\Collection;
use drupol\collection\Collection;
use drupol\collection\Contract\Collection as CollectionInterface;

/**
* Class Collapse.
@@ -14,12 +15,12 @@ final class Collapse extends Operation
/**
* {@inheritdoc}
*/
public function run(Collection $collection): Collection
public function run(CollectionInterface $collection): CollectionInterface
{
return $collection::withClosure(
return Collection::withClosure(
static function () use ($collection) {
foreach ($collection as $values) {
if (\is_array($values) || $values instanceof Collection) {
if (\is_array($values) || $values instanceof CollectionInterface) {
yield from $values;
}
}
11 changes: 6 additions & 5 deletions src/Operation/Combine.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace drupol\collection\Operation;

use drupol\collection\Contract\Collection;
use drupol\collection\Collection;
use drupol\collection\Contract\Collection as CollectionInterface;

/**
* Class Combine.
@@ -14,14 +15,14 @@ final class Combine extends Operation
/**
* {@inheritdoc}
*/
public function run(Collection $collection): Collection
public function run(CollectionInterface $collection): CollectionInterface
{
$keys = $this->parameters;

return $collection::withClosure(
return Collection::withClosure(
static function () use ($keys, $collection) {
$original = $collection->getIterator();
$keysIterator = $collection::with($keys)->getIterator();
$keysIterator = Collection::with($keys)->getIterator();

for (; true === ($original->valid() && $keysIterator->valid()); $original->next(), $keysIterator->next()
) {
Loading