Skip to content

Commit

Permalink
Collection functions for find_key, any, and all
Browse files Browse the repository at this point in the history
Also fixes a potential fatal error of calling filter, from, or for on a repository.
  • Loading branch information
valzargaming committed Feb 12, 2025
1 parent 3dfe582 commit 5897e87
Showing 1 changed file with 62 additions and 11 deletions.
73 changes: 62 additions & 11 deletions src/Discord/Helpers/CollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
trait CollectionTrait
{
/**
* Create a new static.
* Create a new Collection.
*
* @param array $items
* @param ?string $discrim
Expand All @@ -34,11 +34,11 @@ public function __construct(array $items = [], ?string $discrim = 'id', ?string
* @param ?string $discrim
* @param ?string $class
*
* @return static
* @return CollectionInterface
*/
public static function from(array $items = [], ?string $discrim = 'id', ?string $class = null)
{
return new static($items, $discrim, $class);
return new Collection($items, $discrim, $class);
}

/**
Expand All @@ -47,11 +47,11 @@ public static function from(array $items = [], ?string $discrim = 'id', ?string
* @param string $class
* @param ?string $discrim
*
* @return static
* @return CollectionInterface
*/
public static function for(string $class, ?string $discrim = 'id')
{
return new static([], $discrim, $class);
return new Collection([], $discrim, $class);
}

/**
Expand Down Expand Up @@ -140,9 +140,9 @@ public function shift()
*/
public function fill($items): self
{
if ($items instanceof CollectionInterface) {
$items = $items->toArray();
}
$items = $items instanceof CollectionInterface
? $items->toArray()
: $items;
if (! is_array($items)) {
throw new \InvalidArgumentException('The fill method only accepts arrays or CollectionInterface instances.');
}
Expand Down Expand Up @@ -284,7 +284,7 @@ public function search(mixed $needle, bool $strict = false): string|int|false
}

/**
* Runs a filter callback over the collection and returns a new static
* Runs a filter callback over the collection and returns a new Collection
* based on the response of the callback.
*
* @param callable $callback
Expand All @@ -295,7 +295,7 @@ public function search(mixed $needle, bool $strict = false): string|int|false
*/
public function filter(callable $callback)
{
$collection = new static([], $this->discrim, $this->class);
$collection = new Collection([], $this->discrim, $this->class);

foreach ($this->items as $item) {
if ($callback($item)) {
Expand Down Expand Up @@ -325,6 +325,57 @@ public function find(callable $callback)
return null;
}

/**
* Finds the key of the first item that matches the callback.
*
* @param callable $callback
*
* @return mixed
*/
public function find_key(callable $callback)
{
foreach ($this->items as $key => $item) {
if ($callback($item)) {
return $key;
}
}
return null;
}

/**
* Checks if any item matches the callback.
*
* @param callable $callback
*
* @return bool
*/
public function any(callable $callback): bool
{
foreach ($this->items as $item) {
if ($callback($item)) {
return true;
}
}
return false;
}

/**
* Checks if all items match the callback.
*
* @param callable $callback
*
* @return bool
*/
public function all(callable $callback): bool
{
foreach ($this->items as $item) {
if (! $callback($item)) {
return false;
}
}
return true;
}

/**
* Splices the collection, removing a portion of the items and replacing them with the given replacement.
*
Expand Down Expand Up @@ -468,7 +519,7 @@ public function reduce(callable $callback, $initial = null)
}

/**
* Runs a callback over the collection and creates a new static.
* Runs a callback over the collection and creates a new Collection.
*
* @param callable $callback
*
Expand Down

0 comments on commit 5897e87

Please sign in to comment.