Skip to content

Commit

Permalink
Allow custom endpoint params
Browse files Browse the repository at this point in the history
  • Loading branch information
partydragen committed Mar 9, 2024
1 parent 0c80570 commit f1aec3c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 10 additions & 0 deletions core/classes/Endpoints/EndpointBase.php
Expand Up @@ -87,4 +87,14 @@ final public function getAuthType(): string
* @return bool
*/
abstract public function isAuthorised(Nameless2API $api): bool;

/**
* Allow auth methods to add custom params to endpoint.
*
* @return array Custom endpoint params
*/
public function customParams(): array
{
return [];
}
}
8 changes: 4 additions & 4 deletions core/classes/Endpoints/Endpoints.php
Expand Up @@ -63,15 +63,15 @@ public function handle(string $route, string $method, Nameless2API $api): void
}

$reflection = new ReflectionMethod($endpoint, 'execute');
if ($reflection->getNumberOfParameters() !== (count($vars) + 1)) {
throw new InvalidArgumentException("Endpoint's 'execute()' method must take " . (count($vars) + 1) . ' arguments. Endpoint: ' . $endpoint->getRoute());
if ($reflection->getNumberOfParameters() !== (count($endpoint->customParams()) + count($vars) + 1)) {
throw new InvalidArgumentException("Endpoint's 'execute()' method must take " . (count($endpoint->customParams()) + count($vars) + 1) . ' arguments. Endpoint: ' . $endpoint->getRoute());
}

$endpoint->execute(
$api,
...array_map(function ($type, $value) use ($api) {
...array_merge($endpoint->customParams(), array_map(function ($type, $value) use ($api) {
return $this::transform($api, $type, $value);
}, array_keys($vars), $vars)
}, array_keys($vars), $vars))
);

return;
Expand Down

0 comments on commit f1aec3c

Please sign in to comment.