Skip to content

Commit

Permalink
style: code pint
Browse files Browse the repository at this point in the history
  • Loading branch information
jiannei committed Oct 14, 2023
1 parent 91fb374 commit d019490
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 111 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"require-dev": {
"orchestra/testbench": "^8.0",
"pestphp/pest": "^2.23",
"jiannei/laravel-enum": "dev-main"
"jiannei/laravel-enum": "dev-main",
"laravel/pint": "^1.13"
},
"autoload": {
"psr-4": {
Expand All @@ -38,7 +39,8 @@
}
},
"scripts": {
"test": "vendor/bin/pest"
"test": "vendor/bin/pest",
"style": "vendor/bin/pint"
},
"minimum-stability": "dev",
"prefer-stable" : true,
Expand Down
3 changes: 3 additions & 0 deletions pint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"preset": "laravel"
}
18 changes: 0 additions & 18 deletions src/Contract/ResponseFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,32 @@

interface ResponseFormat
{
/**
* @return JsonResponse
*/
public function response(): JsonResponse;

/**
* Get formatted data.
*
* @return array|null
*/
public function get(): ?array;

/**
* Format data structures.
*
* @param mixed|null $data
* @param string $message
* @param int|\BackedEnum $code
* @param $error
* @return $this
*/
public function data(mixed $data = null, string $message = '', int|\BackedEnum $code = 200, $error = null): static;

/**
* Format paginator data.
*
* @param AbstractPaginator|AbstractCursorPaginator|Paginator $resource
* @return array
*/
public function paginator(AbstractPaginator|AbstractCursorPaginator|Paginator $resource): array;

/**
* Format collection resource data.
*
* @param ResourceCollection $collection
* @return array
*/
public function resourceCollection(ResourceCollection $collection): array;

/**
* Format JsonResource Data.
*
* @param JsonResource $resource
* @return array
*/
public function jsonResource(JsonResource $resource): array;
}
3 changes: 0 additions & 3 deletions src/Http/Middleware/SetAcceptHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ class SetAcceptHeader
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string $type
* @return \Illuminate\Http\Response
*/
public function handle(Request $request, Closure $next, string $type = 'json')
Expand Down
3 changes: 0 additions & 3 deletions src/Http/Middleware/ThrottleRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class ThrottleRequests
/**
* Create a new request throttler.
*
* @param \Illuminate\Cache\RateLimiter $limiter
* @return void
*/
public function __construct(RateLimiter $limiter)
Expand All @@ -44,7 +43,6 @@ public function __construct(RateLimiter $limiter)
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param int|string $maxAttempts
* @param float|int $decayMinutes
* @param string $prefix
Expand Down Expand Up @@ -145,7 +143,6 @@ protected function getTimeUntilNextRetry($key)
/**
* Add the limit header information to the given response.
*
* @param \Symfony\Component\HttpFoundation\Response $response
* @param int $maxAttempts
* @param int $remainingAttempts
* @param int|null $retryAfter
Expand Down
46 changes: 2 additions & 44 deletions src/Support/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Format implements ResponseFormat
use Macroable;

protected ?array $data = null;

protected int $statusCode = 200;

public function __construct(protected array $config = [])
Expand All @@ -39,8 +40,6 @@ public function __construct(protected array $config = [])

/**
* Return a new JSON response from the application.
*
* @return JsonResponse
*/
public function response(): JsonResponse
{
Expand All @@ -52,8 +51,6 @@ public function response(): JsonResponse

/**
* Get formatted data.
*
* @return array|null
*/
public function get(): ?array
{
Expand All @@ -64,8 +61,6 @@ public function get(): ?array
* Core format.
*
* @param null $data
* @param string $message
* @param int|\BackedEnum $code
* @param null $error
* @return Format
*/
Expand All @@ -86,9 +81,6 @@ public function data(mixed $data = null, string $message = '', int|\BackedEnum $

/**
* Format paginator data.
*
* @param AbstractPaginator|AbstractCursorPaginator|Paginator $resource
* @return array
*/
public function paginator(AbstractPaginator|AbstractCursorPaginator|Paginator $resource): array
{
Expand All @@ -100,9 +92,6 @@ public function paginator(AbstractPaginator|AbstractCursorPaginator|Paginator $r

/**
* Format collection resource data.
*
* @param ResourceCollection $collection
* @return array
*/
public function resourceCollection(ResourceCollection $collection): array
{
Expand All @@ -114,9 +103,6 @@ public function resourceCollection(ResourceCollection $collection): array

/**
* Format JsonResource Data.
*
* @param JsonResource $resource
* @return array
*/
public function jsonResource(JsonResource $resource): array
{
Expand All @@ -125,9 +111,6 @@ public function jsonResource(JsonResource $resource): array

/**
* Format data.
*
* @param $data
* @return array|object
*/
protected function formatData($data): array|object
{
Expand All @@ -143,14 +126,10 @@ protected function formatData($data): array|object

/**
* Format return message.
*
* @param int $code
* @param string $message
* @return string|null
*/
protected function formatMessage(int $code, string $message = ''): ?string
{
$localizationKey = join('.', [Config::get('response.locale', 'enums'), $code]);
$localizationKey = implode('.', [Config::get('response.locale', 'enums'), $code]);

return match (true) {
! $message && Lang::has($localizationKey) => Lang::get($localizationKey),
Expand All @@ -160,9 +139,6 @@ protected function formatMessage(int $code, string $message = ''): ?string

/**
* Format business code.
*
* @param int|\BackedEnum $code
* @return int
*/
protected function formatBusinessCode(int|\BackedEnum $code): int
{
Expand All @@ -171,9 +147,6 @@ protected function formatBusinessCode(int|\BackedEnum $code): int

/**
* Format http status description.
*
* @param int $statusCode
* @return string
*/
protected function formatStatus(int $statusCode): string
{
Expand All @@ -186,10 +159,6 @@ protected function formatStatus(int $statusCode): string

/**
* Http status code.
*
* @param int $code
* @param $oriData
* @return int
*/
protected function formatStatusCode(int $code, $oriData): int
{
Expand All @@ -198,8 +167,6 @@ protected function formatStatusCode(int $code, $oriData): int

/**
* Get JsonResource resource data.
*
* @return \Closure
*/
protected function formatJsonResource(): \Closure
{
Expand All @@ -212,9 +179,6 @@ protected function formatJsonResource(): \Closure

/**
* Format paginator data.
*
* @param $collection
* @return array
*/
protected function formatMeta($collection): array
{
Expand Down Expand Up @@ -257,9 +221,6 @@ protected function formatMeta($collection): array

/**
* Format error.
*
* @param array|null $error
* @return array|object
*/
protected function formatError(?array $error): object|array
{
Expand All @@ -268,9 +229,6 @@ protected function formatError(?array $error): object|array

/**
* Format response data fields.
*
* @param array $data
* @return array
*/
protected function formatDataFields(array $data): array
{
Expand Down
5 changes: 0 additions & 5 deletions src/Support/Traits/ExceptionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ trait ExceptionTrait
/**
* Custom Normal Exception response.
*
* @param $request
* @param Throwable|Exception $e
* @return JsonResponse
*/
Expand All @@ -53,8 +52,6 @@ protected function prepareJsonResponse($request, $e)
/**
* Custom Failed Validation Response for Lumen.
*
* @param Request $request
* @param array $errors
* @return mixed
*
* @throws HttpResponseException
Expand All @@ -78,7 +75,6 @@ protected function buildFailedValidationResponse(Request $request, array $errors
* Custom Failed Validation Response for Laravel.
*
* @param Request $request
* @param ValidationException $exception
* @return JsonResponse
*/
protected function invalidJson($request, ValidationException $exception)
Expand All @@ -94,7 +90,6 @@ protected function invalidJson($request, ValidationException $exception)
* Custom Failed Authentication Response for Laravel.
*
* @param Request $request
* @param AuthenticationException $exception
* @return \Illuminate\Http\RedirectResponse | JsonResponse
*/
protected function unauthenticated($request, AuthenticationException $exception)
Expand Down
Loading

0 comments on commit d019490

Please sign in to comment.