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.
45 lines
2.0 KiB
45 lines
2.0 KiB
<?php
|
|
|
|
namespace IQBall\Api;
|
|
|
|
use IQBall\Core\Control;
|
|
use IQBall\Core\ControlSchemaErrorResponseFactory;
|
|
use IQBall\Core\Http\HttpRequest;
|
|
use IQBall\Core\Http\HttpResponse;
|
|
use IQBall\Core\Http\JsonHttpResponse;
|
|
use IQBall\Core\Validation\Validator;
|
|
|
|
class APIControl {
|
|
private static function errorFactory(): ControlSchemaErrorResponseFactory {
|
|
return new class () implements ControlSchemaErrorResponseFactory {
|
|
public function apply(array $failures): HttpResponse {
|
|
return new JsonHttpResponse($failures);
|
|
}
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Runs given callback, if the request's payload json validates the given schema.
|
|
* @param array<string, Validator[]> $schema an array of `fieldName => DefaultValidators` which represents the request object schema
|
|
* @param callable(HttpRequest): HttpResponse $run the callback to run if the request is valid according to the given schema.
|
|
* The callback must accept an HttpRequest, and return an HttpResponse object.
|
|
* @return HttpResponse
|
|
*/
|
|
public static function runChecked(array $schema, callable $run): HttpResponse {
|
|
return Control::runChecked($schema, $run, self::errorFactory());
|
|
}
|
|
|
|
/**
|
|
* Runs given callback, if the given request data array validates the given schema.
|
|
* @param array<string, mixed> $data the request's data array.
|
|
* @param array<string, Validator[]> $schema an array of `fieldName => DefaultValidators` which represents the request object schema
|
|
* @param callable(HttpRequest): HttpResponse $run the callback to run if the request is valid according to the given schema.
|
|
* The callback must accept an HttpRequest, and return an HttpResponse object.
|
|
* @return HttpResponse
|
|
*/
|
|
public static function runCheckedFrom(array $data, array $schema, callable $run): HttpResponse {
|
|
return Control::runCheckedFrom($data, $schema, $run, self::errorFactory());
|
|
}
|
|
|
|
}
|