Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add methods to Collection #45839

Merged
merged 4 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,41 @@ public function intersect($items)
return new static(array_intersect($this->items, $this->getArrayableItems($items)));
}

/**
* Intersect the collection with the given items, using the callback.
*
* @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
* @param callable(TValue, TValue): int $callback
* @return static
*/
public function intersectUsing($items, callable $callback)
{
return new static(array_uintersect($this->items, $this->getArrayableItems($items), $callback));
}

/**
* Intersect the collection with the given items with additional index check.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
*/
public function intersectAssoc($items)
{
return new static(array_intersect_assoc($this->items, $this->getArrayableItems($items)));
}

/**
* Intersect the collection with the given items with additional index check, using the callback.
*
* @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
* @param callable(TValue, TValue): int $callback
* @return static
*/
public function intersectAssocUsing($items, callable $callback)
{
return new static(array_intersect_uassoc($this->items, $this->getArrayableItems($items), $callback));
}

/**
* Intersect the collection with the given items by key.
*
Expand Down
35 changes: 35 additions & 0 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,41 @@ public function intersect($items)
return $this->passthru('intersect', func_get_args());
}

/**
* Intersect the collection with the given items, using the callback.
*
* @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
* @param callable(TValue, TValue): int $callback
* @return static
*/
public function intersectUsing()
{
return $this->passthru('intersectUsing', func_get_args());
}

/**
* Intersect the collection with the given items with additional index check.
*
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
* @return static
*/
public function intersectAssoc($items)
{
return $this->passthru('intersectAssoc', func_get_args());
}

/**
* Intersect the collection with the given items with additional index check, using the callback.
*
* @param \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
* @param callable(TValue, TValue): int $callback
* @return static
*/
public function intersectAssocUsing($items, callable $callback)
{
return $this->passthru('intersectAssocUsing', func_get_args());
}

/**
* Intersect the collection with the given items by key.
*
Expand Down
62 changes: 62 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,68 @@ public function testIntersectCollection($collection)
$this->assertEquals(['first_word' => 'Hello'], $c->intersect(new $collection(['first_world' => 'Hello', 'last_word' => 'World']))->all());
}

/**
* @dataProvider collectionClassProvider
*/
public function testIntersectUsingWithNull($collection)
{
$collect = new $collection(['green', 'brown', 'blue']);

$this->assertEquals([], $collect->intersectUsing(null, 'strcasecmp')->all());
}

/**
* @dataProvider collectionClassProvider
*/
public function testIntersectUsingCollection($collection)
{
$collect = new $collection(['green', 'brown', 'blue']);

$this->assertEquals(['green', 'brown'], $collect->intersectUsing(new $collection(['GREEN', 'brown', 'yellow']), 'strcasecmp')->all());
}

/**
* @dataProvider collectionClassProvider
*/
public function testIntersectAssocWithNull($collection)
{
$array1 = new $collection(['a' => 'green', 'b' => 'brown', 'c' => 'blue', 'red']);

$this->assertEquals([], $array1->intersectAssoc(null)->all());
}

/**
* @dataProvider collectionClassProvider
*/
public function testIntersectAssocCollection($collection)
{
$array1 = new $collection(['a' => 'green', 'b' => 'brown', 'c' => 'blue', 'red']);
$array2 = new $collection(['a' => 'green', 'b' => 'yellow', 'blue', 'red']);

$this->assertEquals(['a' => 'green'], $array1->intersectAssoc($array2)->all());
}

/**
* @dataProvider collectionClassProvider
*/
public function testIntersectAssocUsingWithNull($collection)
{
$array1 = new $collection(['a' => 'green', 'b' => 'brown', 'c' => 'blue', 'red']);

$this->assertEquals([], $array1->intersectAssocUsing(null, 'strcasecmp')->all());
}

/**
* @dataProvider collectionClassProvider
*/
public function testIntersectAssocUsingCollection($collection)
{
$array1 = new $collection(['a' => 'green', 'b' => 'brown', 'c' => 'blue', 'red']);
$array2 = new $collection(['a' => 'GREEN', 'B' => 'brown', 'yellow', 'red']);

$this->assertEquals(['b' => 'brown'], $array1->intersectAssocUsing($array2, 'strcasecmp')->all());
}

/**
* @dataProvider collectionClassProvider
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/Support/SupportLazyCollectionIsLazyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,39 @@ public function testIntersectIsLazy()
});
}

public function testIntersectUsingIsLazy()
{
$this->assertDoesNotEnumerate(function ($collection) {
$collection->intersectUsing([1, 2], 'strcasecmp');
});

$this->assertEnumeratesOnce(function ($collection) {
$collection->intersectUsing([1, 2], 'strcasecmp')->all();
});
}

public function testIntersectAssocIsLazy()
{
$this->assertDoesNotEnumerate(function ($collection) {
$collection->intersectAssoc([1, 2]);
});

$this->assertEnumeratesOnce(function ($collection) {
$collection->intersectAssoc([1, 2])->all();
});
}

public function testIntersectAssocUsingIsLazy()
{
$this->assertDoesNotEnumerate(function ($collection) {
$collection->intersectAssocUsing([1, 2], 'strcasecmp');
});

$this->assertEnumeratesOnce(function ($collection) {
$collection->intersectAssocUsing([1, 2], 'strcasecmp')->all();
});
}

public function testIntersectByKeysIsLazy()
{
$this->assertDoesNotEnumerate(function ($collection) {
Expand Down