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

Allow custom endpoint params #3493

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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