From 065d27d8d58f7459701911842bd166f3f9fa6211 Mon Sep 17 00:00:00 2001 From: Valithor Obsidion Date: Fri, 17 Jan 2025 15:16:53 -0500 Subject: [PATCH] uintersect --- src/Discord/Helpers/CollectionTrait.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Discord/Helpers/CollectionTrait.php b/src/Discord/Helpers/CollectionTrait.php index 073966827..dc9863da7 100644 --- a/src/Discord/Helpers/CollectionTrait.php +++ b/src/Discord/Helpers/CollectionTrait.php @@ -332,13 +332,13 @@ public function sort(callable|int|null $callback) } /** - * Computes the difference between the current collection and the given array. + * Gets the difference between the items. * * If a callback is provided and is callable, it uses `array_udiff_assoc` to compute the difference. * Otherwise, it uses `array_diff`. * * @param CollectionInterface|array $array - * @param ?callable $callback + * @param ?callable $callback * * @return CollectionInterface */ @@ -358,17 +358,23 @@ public function diff($items, ?callable $callback) /** * Gets the intersection of the items. * + * If a callback is provided and is callable, it uses `array_uintersect_assoc` to compute the intersection. + * Otherwise, it uses `array_intersect`. + * * @param CollectionInterface|array $array + * @param ?callable $callback * * @return CollectionInterface */ - public function intersect($items) + public function intersect($items, ?callable $callback) { $items = $items instanceof CollectionInterface ? $items->toArray() : $items; - $diff = array_intersect($this->items, $items); + $diff = $callback && is_callable($callback) + ? array_uintersect_assoc($this->items, $items, $callback) + : array_intersect($this->items, $items); return new Collection($diff, $this->discrim, $this->class); }