fix suggestions
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
a7f5a71532
commit
bfb216bfaf
@ -0,0 +1,44 @@
|
||||
<?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());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace IQBall\App;
|
||||
|
||||
use IQBall\Core\Control;
|
||||
use IQBall\Core\ControlSchemaErrorResponseFactory;
|
||||
use IQBall\Core\Http\HttpCodes;
|
||||
use IQBall\Core\Http\HttpRequest;
|
||||
use IQBall\Core\Http\HttpResponse;
|
||||
use IQBall\Core\Validation\Validator;
|
||||
|
||||
class AppControl {
|
||||
private static function errorFactory(): ControlSchemaErrorResponseFactory {
|
||||
return new class () implements ControlSchemaErrorResponseFactory {
|
||||
public function apply(array $failures): HttpResponse {
|
||||
return ViewHttpResponse::twig("error.html.twig", ['failures' => $failures], HttpCodes::BAD_REQUEST);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 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());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace IQBall\Core;
|
||||
|
||||
use IQBall\Core\Http\HttpResponse;
|
||||
use IQBall\Core\Validation\ValidationFail;
|
||||
|
||||
interface ControlSchemaErrorResponseFactory {
|
||||
/**
|
||||
* @param ValidationFail[] $failures
|
||||
* @return HttpResponse
|
||||
*/
|
||||
public function apply(array $failures): HttpResponse;
|
||||
}
|
Loading…
Reference in new issue