Skip to content

Commit

Permalink
Adds protected Discord property to AbstractRepository
Browse files Browse the repository at this point in the history
This is required to support the new method of creating Collections
  • Loading branch information
valzargaming committed Dec 24, 2024
1 parent dace8dd commit 5518bf7
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/Discord/Repository/AbstractRepositoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ trait AbstractRepositoryTrait
* @param Discord $discord
* @param array $vars An array of variables used for the endpoint.
*/
public function __construct(Discord $discord, array $vars = [])
public function __construct(protected Discord $discord, array $vars = [])
{
$this->http = $discord->getHttpClient();
$this->factory = $discord->getFactory();
Expand Down Expand Up @@ -609,7 +609,7 @@ public function has(...$keys): bool
*/
public function filter(callable $callback): CollectionInterface
{
$collection = new Collection([], $this->discrim, $this->class);
$collection = new ($this->discord->getCollectionClass())([], $this->discrim, $this->class);

foreach ($this->items as $offset => $item) {
if ($item instanceof WeakReference) {
Expand Down Expand Up @@ -791,10 +791,25 @@ public function __get(string $key)
}
}

public function __call($name, $arguments)
/**
* This method checks if a method with the name "__Collection__{$name}" exists
* within the class. If it does, it calls that method with the provided arguments.
* If the method does not exist, it throws a BadMethodCallException.
*
* Previously, this class utilized `parent::method` to call methods from the parent class.
* This was changed to use the `__Collection__method` naming convention to avoid conflicts
*
* @param string $name The name of the method being called.
* @param array $arguments The arguments passed to the method.
*
* @return mixed The result of the called method.
*
* @throws \BadMethodCallException If the method does not exist.
*/
public function __call($name, $arguments): mixed
{
if (method_exists(CollectionTrait::class, $name)) {
return (new CollectionTrait)->$name(...$arguments);
if (method_exists($this, "__Collection__{$name}")) {
return $this->{"__Collection__{$name}"}(...$arguments);
}

throw new \BadMethodCallException("Method {$name} does not exist.");
Expand Down

0 comments on commit 5518bf7

Please sign in to comment.