From 9ea4a19d7572bcb1e81f4f470ed9b4191edd2144 Mon Sep 17 00:00:00 2001 From: Valithor Obsidion Date: Fri, 17 Jan 2025 14:30:05 -0500 Subject: [PATCH] walk() --- src/Discord/Helpers/CollectionInterface.php | 1 + src/Discord/Helpers/CollectionTrait.php | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Discord/Helpers/CollectionInterface.php b/src/Discord/Helpers/CollectionInterface.php index ee21184b0..cbb3f53f9 100644 --- a/src/Discord/Helpers/CollectionInterface.php +++ b/src/Discord/Helpers/CollectionInterface.php @@ -35,6 +35,7 @@ public function find(callable $callback); public function clear(): void; public function slice(int $offset, ?int $length, bool $preserve_keys = false); public function sort(callable|int|null $callback); + public function walk(callable $callback, mixed $arg); public function reduce(callable $callback, $initial = null); public function map(callable $callback); public function merge($collection): self; diff --git a/src/Discord/Helpers/CollectionTrait.php b/src/Discord/Helpers/CollectionTrait.php index 6f78f34b3..908dc3cbe 100644 --- a/src/Discord/Helpers/CollectionTrait.php +++ b/src/Discord/Helpers/CollectionTrait.php @@ -331,6 +331,23 @@ public function sort(callable|int|null $callback) return new Collection($items, $this->discrim, $this->class); } + /** + * Applies the given callback function to each item in the collection. + * + * @param callable $callback + * @param mixed $arg + * + * @return CollectionInterface + */ + public function walk(callable $callback, mixed $arg) + { + $items = $this->items; + + array_walk($items, $callback, $arg); + + return new Collection($items, $this->discrim, $this->class); + } + /** * Reduces the collection to a single value using a callback function. *