Skip to content

Commit 18c0bb9

Browse files
committed
search and splice
1 parent 27d2bb7 commit 18c0bb9

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

src/Discord/Builders/CommandAttributes.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@
2424
*
2525
* @since 7.1.0
2626
*
27-
* @property int $type The type of the command, defaults 1 if not set.
28-
* @property string $name 1-32 character name of the command.
29-
* @property ?string[]|null $name_localizations Localization dictionary for the name field. Values follow the same restrictions as name.
30-
* @property ?string $description 1-100 character description for CHAT_INPUT commands, empty string for USER and MESSAGE commands.
31-
* @property ?string[]|null $description_localizations Localization dictionary for the description field. Values follow the same restrictions as description.
32-
* @property \Discord\Helpers\Collection|Option[]|null $options The parameters for the command, max 25. Only for Slash command (CHAT_INPUT).
33-
* @property ?string $default_member_permissions Set of permissions represented as a bit set.
34-
* @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.
35-
* @property ?bool $default_permission Whether the command is enabled by default when the app is added to a guild. SOON DEPRECATED.
36-
* @property ?int $guild_id The optional guild ID this command is for. If not set, the command is global.
37-
* @property bool|null $nsfw Indicates whether the command is age-restricted, defaults to `false`.
27+
* @property int $type The type of the command, defaults 1 if not set.
28+
* @property string $name 1-32 character name of the command.
29+
* @property ?string[]|null $name_localizations Localization dictionary for the name field. Values follow the same restrictions as name.
30+
* @property ?string $description 1-100 character description for CHAT_INPUT commands, empty string for USER and MESSAGE commands.
31+
* @property ?string[]|null $description_localizations Localization dictionary for the description field. Values follow the same restrictions as description.
32+
* @property \Discord\Helpers\CollectionInterface|Option[]|null $options The parameters for the command, max 25. Only for Slash command (CHAT_INPUT).
33+
* @property ?string $default_member_permissions Set of permissions represented as a bit set.
34+
* @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.
35+
* @property ?bool $default_permission Whether the command is enabled by default when the app is added to a guild. SOON DEPRECATED.
36+
* @property ?int $guild_id The optional guild ID this command is for. If not set, the command is global.
37+
* @property bool|null $nsfw Indicates whether the command is age-restricted, defaults to `false`.
3838
*/
3939
trait CommandAttributes
4040
{
@@ -279,8 +279,8 @@ public function removeOption(Option $option): self
279279
throw new \DomainException('Only CHAT_INPUT Command type can have option.');
280280
}
281281

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

286286
return $this;

src/Discord/Helpers/CollectionInterface.php

+2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ public function first();
3131
public function last();
3232
public function isset($offset): bool;
3333
public function has(...$keys): bool;
34+
public function search(mixed $needle, bool $strict = false): string|int|false;
3435
public function filter(callable $callback);
3536
public function find(callable $callback);
37+
public function splice(int $offset, ?int $length, mixed $replacement = []);
3638
public function clear(): void;
3739
public function slice(int $offset, ?int $length = null, bool $preserve_keys = false);
3840
public function sort(callable|int|null $callback);

src/Discord/Helpers/CollectionTrait.php

+29
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,19 @@ public function has(...$keys): bool
270270
return true;
271271
}
272272

273+
/**
274+
* Searches for a given value within the collection and returns the corresponding key if successful.
275+
*
276+
* @param mixed $needle
277+
* @param bool $strict [optional]
278+
*
279+
* @return string|int|false
280+
*/
281+
public function search(mixed $needle, bool $strict = false): string|int|false
282+
{
283+
return array_search($needle, $this->items, $strict);
284+
}
285+
273286
/**
274287
* Runs a filter callback over the collection and returns a new static
275288
* based on the response of the callback.
@@ -312,6 +325,22 @@ public function find(callable $callback)
312325
return null;
313326
}
314327

328+
/**
329+
* Splices the collection, removing a portion of the items and replacing them with the given replacement.
330+
*
331+
* @param int $offset
332+
* @param ?int $length
333+
* @param mixed $replacement
334+
*
335+
* @return self
336+
*/
337+
public function splice(int $offset, ?int $length, mixed $replacement = []): self
338+
{
339+
array_splice($this->items, $offset, $length, $replacement);
340+
341+
return $this;
342+
}
343+
315344
/**
316345
* Clears the collection.
317346
*/

src/Discord/Repository/AbstractRepositoryTrait.php

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ trait AbstractRepositoryTrait
3737
fill as fill;
3838
push as push;
3939
isset as isset;
40+
search as search;
41+
splice as splice;
4042
slice as slice;
4143
sort as sort;
4244
map as map;

0 commit comments

Comments
 (0)