Skip to content

Commit

Permalink
search and splice
Browse files Browse the repository at this point in the history
  • Loading branch information
valzargaming committed Jan 19, 2025
1 parent 27d2bb7 commit 18c0bb9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/Discord/Builders/CommandAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@
*
* @since 7.1.0
*
* @property int $type The type of the command, defaults 1 if not set.
* @property string $name 1-32 character name of the command.
* @property ?string[]|null $name_localizations Localization dictionary for the name field. Values follow the same restrictions as name.
* @property ?string $description 1-100 character description for CHAT_INPUT commands, empty string for USER and MESSAGE commands.
* @property ?string[]|null $description_localizations Localization dictionary for the description field. Values follow the same restrictions as description.
* @property \Discord\Helpers\Collection|Option[]|null $options The parameters for the command, max 25. Only for Slash command (CHAT_INPUT).
* @property ?string $default_member_permissions Set of permissions represented as a bit set.
* @property bool|null $dm_permission Indicates whether the command is available in DMs with the app, only for globally-scoped commands. By default, commands are visible.
* @property ?bool $default_permission Whether the command is enabled by default when the app is added to a guild. SOON DEPRECATED.
* @property ?int $guild_id The optional guild ID this command is for. If not set, the command is global.
* @property bool|null $nsfw Indicates whether the command is age-restricted, defaults to `false`.
* @property int $type The type of the command, defaults 1 if not set.
* @property string $name 1-32 character name of the command.
* @property ?string[]|null $name_localizations Localization dictionary for the name field. Values follow the same restrictions as name.
* @property ?string $description 1-100 character description for CHAT_INPUT commands, empty string for USER and MESSAGE commands.
* @property ?string[]|null $description_localizations Localization dictionary for the description field. Values follow the same restrictions as description.
* @property \Discord\Helpers\CollectionInterface|Option[]|null $options The parameters for the command, max 25. Only for Slash command (CHAT_INPUT).
* @property ?string $default_member_permissions Set of permissions represented as a bit set.
* @property bool|null $dm_permission Indicates whether the command is available in DMs with the app, only for globally-scoped commands. By default, commands are visible.
* @property ?bool $default_permission Whether the command is enabled by default when the app is added to a guild. SOON DEPRECATED.
* @property ?int $guild_id The optional guild ID this command is for. If not set, the command is global.
* @property bool|null $nsfw Indicates whether the command is age-restricted, defaults to `false`.
*/
trait CommandAttributes
{
Expand Down Expand Up @@ -279,8 +279,8 @@ public function removeOption(Option $option): self
throw new \DomainException('Only CHAT_INPUT Command type can have option.');
}

if (isset($this->options) && ($idx = array_search($option, $this->options)) !== false) {
array_splice($this->options, $idx, 1);
if (isset($this->options) && ($idx = $this->options->search($option)) !== false) {
$this->options->splice($idx, 1);
}

return $this;
Expand Down
2 changes: 2 additions & 0 deletions src/Discord/Helpers/CollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ public function first();
public function last();
public function isset($offset): bool;
public function has(...$keys): bool;
public function search(mixed $needle, bool $strict = false): string|int|false;
public function filter(callable $callback);
public function find(callable $callback);
public function splice(int $offset, ?int $length, mixed $replacement = []);
public function clear(): void;
public function slice(int $offset, ?int $length = null, bool $preserve_keys = false);
public function sort(callable|int|null $callback);
Expand Down
29 changes: 29 additions & 0 deletions src/Discord/Helpers/CollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,19 @@ public function has(...$keys): bool
return true;
}

/**
* Searches for a given value within the collection and returns the corresponding key if successful.
*
* @param mixed $needle
* @param bool $strict [optional]
*
* @return string|int|false
*/
public function search(mixed $needle, bool $strict = false): string|int|false
{
return array_search($needle, $this->items, $strict);
}

/**
* Runs a filter callback over the collection and returns a new static
* based on the response of the callback.
Expand Down Expand Up @@ -312,6 +325,22 @@ public function find(callable $callback)
return null;
}

/**
* Splices the collection, removing a portion of the items and replacing them with the given replacement.
*
* @param int $offset
* @param ?int $length
* @param mixed $replacement
*
* @return self
*/
public function splice(int $offset, ?int $length, mixed $replacement = []): self
{
array_splice($this->items, $offset, $length, $replacement);

return $this;
}

/**
* Clears the collection.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Discord/Repository/AbstractRepositoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ trait AbstractRepositoryTrait
fill as fill;
push as push;
isset as isset;
search as search;
splice as splice;
slice as slice;
sort as sort;
map as map;
Expand Down

0 comments on commit 18c0bb9

Please sign in to comment.