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

Collection enhancements #1277

Merged
merged 19 commits into from
Jan 18, 2025
Merged

Collection enhancements #1277

merged 19 commits into from
Jan 18, 2025

Conversation

valzargaming
Copy link
Member

@valzargaming valzargaming commented Jan 17, 2025

This pull request includes several changes to the CollectionInterface and CollectionTrait in the Discord/Helpers directory, as well as updates to various attributes in the Channel, Message, Embed, and AuditLog classes. The primary focus is on enhancing the functionality of the collection interface and traits, and updating the return types of attributes to use CollectionInterface instead of Collection.

Collection Interface Enhancements:

  • Added new methods shift(), walk(callable $callback, mixed $arg), reduce(callable $callback, $initial = null), collect(), keys(), values(), diff($items, ?callable $callback), intersect($items, ?callable $callback), and unique(int $flags = SORT_STRING) to CollectionInterface. [1] [2] [3]

Collection Trait Enhancements:

  • Implemented the shift() method to remove and return the first item of the collection.
  • Updated the fill() method to accept both arrays and CollectionInterface instances.
  • Added methods diff(), intersect(), walk(), reduce(), unique(), collect(), keys(), and values() to the CollectionTrait. [1] [2] [3] [4] [5] [6]
  • Modified the __unserialize() method to accept both arrays and CollectionInterface instances.

Attribute Return Type Updates:

@valzargaming
Copy link
Member Author

valzargaming commented Jan 17, 2025

Some sneak previews!

Before reduce() and shift():

if (! $members_array = $members->toArray()) return $message->react(":x:"); // No members to process
$promise = array_shift($members_array)->addRole($this->civ13->role_ids['Verified']);
if (! $members_array) return $promise->then(static fn() => $message->react(":thumbsup:")); // There was only one member to process
return array_reduce($members_array, fn(PromiseInterface $carry_promise, Member $member) =>
    $carry_promise->then(fn() => $member->addRole($this->civ13->role_ids['Verified'])),
    $promise)
        ->then(static fn() => $message->react(":thumbsup:"));

After reduce() and shift():

return $message->react("⏱️")
    ->then(static fn() => $members->reduce(fn(PromiseInterface $carry_promise, Member $member): PromiseInterface =>
        $carry_promise->then(fn() => $member->addRole($this->civ13->role_ids['Verified'])),
        $members->shift()->addRole($this->civ13->role_ids['Verified'])))
    ->then(static fn() => $message->react("👍"));

Before reduce() and shift():

function getHighestRole(Collection $roles): ?Role
{
    return array_reduce($roles->toArray(), fn($prev, $role) =>
        ($prev === null
            ? $role
            : ($this->comparePositionTo($role, $prev) > 0
                ? $role
                : $prev))
    );
}

After reduce() and shift():

function getHighestRole(Collection $roles): ?Role
{
    return $roles->reduce(fn($prev, $role) =>
        ($prev === null
            ? $role
            : ($this->comparePositionTo($role, $prev) > 0
                ? $role
                : $prev)))
        ->shift();
}

Before diff() and merge():

blic function setRoles(
    Member $member,
    Collection|array|Role|string|int $add_roles = [],
    Collection|array|Role|string|int $remove_roles = []
): PromiseInterface
{
    if (! $add_roles    = self::__rolesToIdArray($add_roles   )) return resolve($member);
    if (! $remove_roles = self::__rolesToIdArray($remove_roles)) return resolve($member);

    foreach ($add_roles    as &$role_id)   if (  $member->roles->has($role_id))  unset($role_id);
    foreach ($remove_roles as &$role_id)   if (! $member->roles->has($role_id))  unset($role_id);

    return ($updated_roles = 
        array_diff(
            array_merge(
                array_values(
                    $member->roles
                        ->map(fn($role) => $role->id)->toArray()
                ), $add_roles),
            $remove_roles
        )
    ) ? $member->setRoles($updated_roles);
      : resolve($member);
}

After diff() and merge():

public function setRoles(
    Member $member,
    Collection|array|Role|string|int $add_roles = [],
    Collection|array|Role|string|int $remove_roles = []
): PromiseInterface
{
    if (! $add_roles    = self::__rolesToIdArray($add_roles   )) return resolve($member);
    if (! $remove_roles = self::__rolesToIdArray($remove_roles)) return resolve($member);

    foreach ($add_roles    as &$role_id)   if (  $member->roles->has($role_id))  unset($role_id);
    foreach ($remove_roles as &$role_id)   if (! $member->roles->has($role_id))  unset($role_id);

    return ($updated_roles = $member->roles
        ->map(static fn($role) => $role->id)
        //->values()
        ->merge($add_roles)
        ->diff($remove_roles))
            ? $member->setRoles($updated_roles)
            : resolve($member);
}

@valzargaming valzargaming marked this pull request as ready for review January 18, 2025 00:06
@valzargaming
Copy link
Member Author

I'm merging this now as this PR has been thoroughly tested.

@valzargaming valzargaming merged commit 42f2d69 into master Jan 18, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant