Skip to content

Commit

Permalink
New Intersect and IntersectKeys operation.
Browse files Browse the repository at this point in the history
drupol committed Jul 22, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 1cac685 commit 3bc2006
Showing 7 changed files with 148 additions and 0 deletions.
24 changes: 24 additions & 0 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
@@ -998,6 +998,30 @@ public function it_can_implode(): void
->shouldReturn('ABC');
}

public function it_can_intersect(): void
{
$this::fromIterable(range(1, 5))
->intersect(1, 2, 3, 9)
->shouldIterateAs([0 => 1, 1 => 2, 2 => 3]);

$this::fromIterable(range(1, 5))
->intersect()
->shouldIterateAs([]);
}

public function it_can_intersectKeys(): void
{
$input = array_combine(range('a', 'e'), range(1, 5));

$this::fromIterable($input)
->intersectKeys('b', 'd')
->shouldIterateAs(['b' => 2, 'd' => 4]);

$this::fromIterable($input)
->intersectKeys()
->shouldIterateAs([]);
}

public function it_can_intersperse(): void
{
$this::fromIterable(range('A', 'F'))
12 changes: 12 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
@@ -31,6 +31,8 @@
use loophp\collection\Operation\Forget;
use loophp\collection\Operation\Frequency;
use loophp\collection\Operation\Group;
use loophp\collection\Operation\Intersect;
use loophp\collection\Operation\IntersectKeys;
use loophp\collection\Operation\Intersperse;
use loophp\collection\Operation\Iterate;
use loophp\collection\Operation\Keys;
@@ -272,6 +274,16 @@ public function implode(string $glue = ''): string
return $this->transform(new Implode($glue));
}

public function intersect(...$values): BaseInterface
{
return $this->run(new Intersect(...$values));
}

public function intersectKeys(...$values): BaseInterface
{
return $this->run(new IntersectKeys(...$values));
}

public function intersperse($element, int $every = 1, int $startAt = 0): BaseInterface
{
return $this->run(new Intersperse($element, $every, $startAt));
4 changes: 4 additions & 0 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
@@ -25,6 +25,8 @@
use loophp\collection\Contract\Operation\Forgetable;
use loophp\collection\Contract\Operation\Frequencyable;
use loophp\collection\Contract\Operation\Groupable;
use loophp\collection\Contract\Operation\Intersectable;
use loophp\collection\Contract\Operation\Intersectkeysable;
use loophp\collection\Contract\Operation\Intersperseable;
use loophp\collection\Contract\Operation\Iterateable;
use loophp\collection\Contract\Operation\Keysable;
@@ -104,6 +106,8 @@ interface Collection extends
Getable,
Groupable,
Implodeable,
Intersectable,
Intersectkeysable,
Intersperseable,
Iterateable,
JsonSerializable,
17 changes: 17 additions & 0 deletions src/Contract/Operation/Intersectable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace loophp\collection\Contract\Operation;

use loophp\collection\Contract\Base;

interface Intersectable
{
/**
* @param mixed ...$values
*
* @return \loophp\collection\Base|\loophp\collection\Contract\Collection
*/
public function intersect(...$values): Base;
}
17 changes: 17 additions & 0 deletions src/Contract/Operation/Intersectkeysable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace loophp\collection\Contract\Operation;

use loophp\collection\Contract\Base;

interface Intersectkeysable
{
/**
* @param mixed ...$values
*
* @return \loophp\collection\Base|\loophp\collection\Contract\Collection
*/
public function intersectKeys(...$values): Base;
}
37 changes: 37 additions & 0 deletions src/Operation/Intersect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace loophp\collection\Operation;

use Closure;
use Generator;
use loophp\collection\Contract\Operation;

use function in_array;

final class Intersect extends AbstractOperation implements Operation
{
/**
* @param mixed ...$values
*/
public function __construct(...$values)
{
$this->storage['values'] = $values;
}

public function __invoke(): Closure
{
return
/**
* @param array<int, mixed> $values
*/
static function (iterable $collection, $values): Generator {
foreach ($collection as $key => $value) {
if (true === in_array($value, $values, true)) {
yield $key => $value;
}
}
};
}
}
37 changes: 37 additions & 0 deletions src/Operation/IntersectKeys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace loophp\collection\Operation;

use Closure;
use Generator;
use loophp\collection\Contract\Operation;

use function in_array;

final class IntersectKeys extends AbstractOperation implements Operation
{
/**
* @param mixed ...$values
*/
public function __construct(...$values)
{
$this->storage['values'] = $values;
}

public function __invoke(): Closure
{
return
/**
* @param array<int, mixed> $values
*/
static function (iterable $collection, $values): Generator {
foreach ($collection as $key => $value) {
if (true === in_array($key, $values, true)) {
yield $key => $value;
}
}
};
}
}

0 comments on commit 3bc2006

Please sign in to comment.