You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Application-Web/src/Api/ApiAction.php

46 lines
1.2 KiB

<?php
namespace IQBall\Api;
use IQBall\Core\Data\Account;
use IQBall\Core\Http\HttpResponse;
use IQBall\Core\Route\AbstractAction;
/**
* @extends AbstractAction<?Account>
*/
class ApiAction extends AbstractAction {
/**
* @param mixed[] $params
* @param ?Account $session
* @return HttpResponse
*/
public function run(array $params, $session): HttpResponse {
$params = array_values($params);
if ($this->isAuthRequired()) {
if ($session == null) {
throw new \Exception("action requires authorization.");
}
$params[] = $session;
}
return call_user_func_array($this->action, $params);
}
/**
* @param callable(mixed[]): HttpResponse $action
* @return ApiAction an action that does not require to have an authorization.
*/
public static function noAuth(callable $action): ApiAction {
return new ApiAction($action, false);
}
/**
* @param callable(mixed[]): HttpResponse $action
* @return ApiAction an action that does require to have an authorization.
*/
public static function auth(callable $action): ApiAction {
return new ApiAction($action, true);
}
}