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

Add missing imports #1278

Closed
wants to merge 20 commits into from
Closed
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
13 changes: 11 additions & 2 deletions src/Discord/Helpers/CollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ interface CollectionInterface extends ArrayAccess, JsonSerializable, IteratorAgg
public function get(string $discrim, $key);
public function set($offset, $value);
public function pull($key, $default = null);
public function fill(array $items): self;
public function shift();
public function fill($items): self;
public function push(...$items): self;
public function pushItem($item): self;
public function count(): int;
Expand All @@ -35,9 +36,17 @@ 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;
public function toArray();
public function collect();
public function keys(): array;
public function values(): array;
public function diff($items, ?callable $callback);
public function intersect($items, ?callable $callback);
public function unique(int $flags = SORT_STRING);
public function offsetExists($offset): bool;
#[\ReturnTypeWillChange]
public function offsetGet($offset);
Expand All @@ -46,7 +55,7 @@ public function offsetUnset($offset): void;
public function serialize(int $flags = 0, ?int $depth = 512): string;
public function __serialize(): array;
public function unserialize(string $serialized): void;
public function __unserialize(array $data): void;
public function __unserialize($data): void;
public function jsonSerialize(): array;
public function getIterator(): Traversable;
public function __debugInfo(): array;
Expand Down
171 changes: 162 additions & 9 deletions src/Discord/Helpers/CollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ trait CollectionTrait
*
* @param array $items
* @param ?string $discrim
* @param string|null $class
* @param ?string $class
*/
public function __construct(array $items = [], ?string $discrim = 'id', ?string $class = null)
{
Expand All @@ -32,7 +32,7 @@ public function __construct(array $items = [], ?string $discrim = 'id', ?string
*
* @param array $items
* @param ?string $discrim
* @param string|null $class
* @param ?string $class
*
* @return static
*/
Expand Down Expand Up @@ -113,15 +113,40 @@ public function pull($key, $default = null)
return $default;
}

/**
* Shifts an item from the collection.
*
* @return mixed
*/
public function shift()
{
if (empty($this->items)) {
return null;
}

reset($this->items);
$key = key($this->items);
$value = array_shift($this->items);

return [$key => $value];
}

/**
* Fills an array of items into the collection.
*
* @param array $items
* @param CollectionInterface|array $items
*
* @return self
*/
public function fill(array $items): self
public function fill($items): self
{
if ($items instanceof CollectionInterface) {
$items = $items->toArray();
}
if (! is_array($items)) {
throw new \InvalidArgumentException('The fill method only accepts arrays or CollectionInterface instances.');
}

foreach ($items as $item) {
$this->pushItem($item);
}
Expand Down Expand Up @@ -298,8 +323,8 @@ public function clear(): void
/**
* Slices the collection.
*
* @param int $offset
* @param null|int $length
* @param int $offset
* @param ?int $length
* @param bool $preserve_keys
*
* @return CollectionInterface
Expand Down Expand Up @@ -331,6 +356,88 @@ public function sort(callable|int|null $callback)
return new Collection($items, $this->discrim, $this->class);
}

/**
* 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
*
* @return CollectionInterface
*/
public function diff($items, ?callable $callback)
{
$items = $items instanceof CollectionInterface
? $items->toArray()
: $items;

$diff = $callback && is_callable($callback)
? array_udiff_assoc($this->items, $items, $callback)
: array_diff($this->items, $items);

return new Collection($diff, $this->discrim, $this->class);
}

/**
* 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, ?callable $callback)
{
$items = $items instanceof CollectionInterface
? $items->toArray()
: $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);
}

/**
* 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.
*
* @param callable $callback
* @param ?mixed $initial
*
* @return CollectionInterface
*/
public function reduce(callable $callback, $initial = null)
{
$items = $this->items;

$items = array_reduce($items, $callback, $initial);

return new Collection($items, $this->discrim, $this->class);
}

/**
* Runs a callback over the collection and creates a new static.
*
Expand All @@ -346,6 +453,18 @@ public function map(callable $callback)
return new Collection(array_combine($keys, $values), $this->discrim, $this->class);
}

/**
* Returns unique items.
*
* @param int $flags
*
* @return CollectionInterface
*/
public function unique(int $flags = SORT_STRING)
{
return new Collection(array_unique($this->items, $flags), $this->discrim, $this->class);
}

/**
* Merges another collection into this collection.
*
Expand All @@ -355,7 +474,11 @@ public function map(callable $callback)
*/
public function merge($collection): self
{
$this->items = array_merge($this->items, $collection->toArray());
$items = $collection instanceof CollectionInterface
? $collection->toArray()
: $collection;

$this->items = array_merge($this->items, $items);

return $this;
}
Expand All @@ -370,6 +493,16 @@ public function toArray()
return $this->items;
}

/**
* Converts the items into a new collection.
*
* @return CollectionInterface
*/
public function collect()
{
return new Collection($this->items, $this->discrim, $this->class);
}

/**
* @since 11.0.0
*
Expand All @@ -382,6 +515,16 @@ public function keys(): array
return array_keys($this->items);
}

/**
* Get the values of the items.
*
* @return array
*/
public function values(): array
{
return array_values($this->items);
}

/**
* If the collection has an offset.
*
Expand Down Expand Up @@ -431,6 +574,9 @@ public function offsetUnset($offset): void
/**
* Returns the string representation of the collection.
*
* @param int $flags
* @param ?int $depth
*
* @return string
*/
public function serialize(int $flags = 0, ?int $depth = 512): string
Expand Down Expand Up @@ -461,10 +607,17 @@ public function unserialize(string $serialized): void
/**
* Unserializes the collection.
*
* @param array $data
* @param CollectionInterface|array $data
*/
public function __unserialize(array $data): void
public function __unserialize($data): void
{
if ($data instanceof CollectionInterface) {
$data = $data->toArray();
}
if (! is_array($data)) {
throw new \InvalidArgumentException('The __unserialize method only accepts arrays or CollectionInterface instances.');
}

$this->items = $data;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Discord/Parts/Channel/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Discord\Builders\MessageBuilder;
use Discord\Exceptions\InvalidOverwriteException;
use Discord\Helpers\Collection;
use Discord\Helpers\CollectionInterface;
use Discord\Parts\Embed\Embed;
use Discord\Parts\Guild\Guild;
use Discord\Parts\Guild\Role;
Expand Down Expand Up @@ -253,7 +254,7 @@ protected function getRecipientIdAttribute(): ?string
*
* @return Collection A collection of recipients.
*/
protected function getRecipientsAttribute(): Collection
protected function getRecipientsAttribute(): CollectionInterface
{
$recipients = Collection::for(User::class);

Expand Down Expand Up @@ -999,7 +1000,7 @@ protected function getPermissionOverwritesAttribute(): ?array
*
* @since 7.4.0
*/
protected function getAvailableTagsAttribute(): Collection
protected function getAvailableTagsAttribute(): CollectionInterface
{
$available_tags = Collection::for(Tag::class);

Expand Down
11 changes: 6 additions & 5 deletions src/Discord/Parts/Channel/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Carbon\Carbon;
use Discord\Builders\MessageBuilder;
use Discord\Helpers\Collection;
use Discord\Helpers\CollectionInterface;
use Discord\Parts\Channel\Poll;
use Discord\Parts\Embed\Embed;
use Discord\Parts\Guild\Emoji;
Expand Down Expand Up @@ -336,7 +337,7 @@ protected function getSuppressNotificationsAttribute(): bool
*
* @return Collection|Channel[]
*/
protected function getMentionChannelsAttribute(): Collection
protected function getMentionChannelsAttribute(): CollectionInterface
{
$collection = Collection::for(Channel::class);

Expand All @@ -360,7 +361,7 @@ protected function getMentionChannelsAttribute(): Collection
*
* @return Collection|Attachment[] Attachment objects.
*/
protected function getAttachmentsAttribute(): Collection
protected function getAttachmentsAttribute(): CollectionInterface
{
$attachments = Collection::for(Attachment::class);

Expand Down Expand Up @@ -492,7 +493,7 @@ protected function getGuildAttribute(): ?Guild
*
* @return Collection<?Role> The roles that were mentioned. null role only contains the ID in the collection.
*/
protected function getMentionRolesAttribute(): Collection
protected function getMentionRolesAttribute(): CollectionInterface
{
$roles = new Collection();

Expand All @@ -516,7 +517,7 @@ protected function getMentionRolesAttribute(): Collection
*
* @return Collection|User[] The users that were mentioned.
*/
protected function getMentionsAttribute(): Collection
protected function getMentionsAttribute(): CollectionInterface
{
$users = Collection::for(User::class);

Expand Down Expand Up @@ -583,7 +584,7 @@ protected function getMemberAttribute(): ?Member
*
* @return Collection<Embed> A collection of embeds.
*/
protected function getEmbedsAttribute(): Collection
protected function getEmbedsAttribute(): CollectionInterface
{
$embeds = new Collection([], null);

Expand Down
3 changes: 2 additions & 1 deletion src/Discord/Parts/Embed/Embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Carbon\Carbon;
use Discord\Helpers\Collection;
use Discord\Helpers\CollectionInterface;
use Discord\Parts\Channel\Attachment;
use Discord\Parts\Part;
use function Discord\poly_strlen;
Expand Down Expand Up @@ -137,7 +138,7 @@ protected function getAuthorAttribute(): Author
*
* @return Collection|Field[]
*/
protected function getFieldsAttribute(): Collection
protected function getFieldsAttribute(): CollectionInterface
{
$fields = Collection::for(Field::class, null);

Expand Down
Loading