Skip to content

Commit 623de65

Browse files
committed
wip: refactor formatter
1 parent 4f9dc62 commit 623de65

File tree

9 files changed

+53
-97
lines changed

9 files changed

+53
-97
lines changed

src/Contracts/Format.php

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/Providers/LaravelServiceProvider.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,6 @@ public function register()
2020
$this->setupConfig();
2121
}
2222

23-
public function boot()
24-
{
25-
$formatter = $this->app['config']->get('response.format.class', \Jiannei\Response\Laravel\Support\Format::class);
26-
27-
if (is_string($formatter) && class_exists($formatter)) {
28-
$this->app->bind(\Jiannei\Response\Laravel\Contracts\Format::class, function () use ($formatter) {
29-
return new $formatter;
30-
});
31-
}
32-
}
33-
3423
protected function setupConfig()
3524
{
3625
$path = dirname(__DIR__, 2).'/config/response.php';

src/Response.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,9 @@
1111

1212
namespace Jiannei\Response\Laravel;
1313

14-
use Jiannei\Response\Laravel\Contracts\Format;
1514
use Jiannei\Response\Laravel\Support\Traits\JsonResponseTrait;
1615

1716
class Response
1817
{
1918
use JsonResponseTrait;
20-
21-
protected $formatter;
22-
23-
public function __construct(Format $format)
24-
{
25-
$this->formatter = $format;
26-
}
2719
}

src/Support/Facades/Format.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Jiannei\Response\Laravel\Support\Facades;
4+
5+
use Illuminate\Http\JsonResponse;
6+
use Illuminate\Http\Resources\Json\JsonResource;
7+
use Illuminate\Http\Resources\Json\ResourceCollection;
8+
use Illuminate\Pagination\AbstractCursorPaginator;
9+
use Illuminate\Pagination\AbstractPaginator;
10+
use Illuminate\Support\Facades\Facade as IlluminateFacade;
11+
12+
/**
13+
*
14+
* @method static JsonResponse response($data = [], int $status = 200, array $headers = [], int $options = 0)
15+
* @method static array data($data, ?string $message, int $code, $errors = null)
16+
* @method static array paginator(AbstractPaginator|AbstractCursorPaginator $resource)
17+
* @method static array resourceCollection(ResourceCollection $collection)
18+
* @method static array jsonResource(JsonResource $resource)
19+
*
20+
* @see \Jiannei\Response\Laravel\Support\Format
21+
*/
22+
class Format extends IlluminateFacade
23+
{
24+
protected static function getFacadeAccessor()
25+
{
26+
return config('response.format.class', \Jiannei\Response\Laravel\Support\Format::class);
27+
}
28+
}

src/Support/Facades/Response.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
use Illuminate\Support\Facades\Facade as IlluminateFacade;
1717

1818
/**
19-
* @method static JsonResponse|JsonResource accepted($data = null, string $message = '', string $location = '')
20-
* @method static JsonResponse|JsonResource created($data = null, string $message = '', string $location = '')
21-
* @method static noContent(string $message = '')
22-
* @method static JsonResponse|JsonResource localize(int $code = 200, array $headers = [], int $option = 0)
23-
* @method static JsonResponse|JsonResource ok(string $message = '', int $code = 200, array $headers = [], int $option = 0)
24-
* @method static JsonResponse|JsonResource success($data = null, string $message = '', int $code = 200, array $headers = [], int $option = 0)
19+
* @method static JsonResponse accepted($data = null, string $message = '', string $location = '')
20+
* @method static JsonResponse created($data = null, string $message = '', string $location = '')
21+
* @method static JsonResponse noContent(string $message = '')
22+
* @method static JsonResponse localize(int $code = 200, array $headers = [], int $option = 0)
23+
* @method static JsonResponse ok(string $message = '', int $code = 200, array $headers = [], int $option = 0)
24+
* @method static JsonResponse success($data = null, string $message = '', int $code = 200, array $headers = [], int $option = 0)
2525
* @method static void errorBadRequest(?string $message = '')
2626
* @method static void errorUnauthorized(string $message = '')
2727
* @method static void errorForbidden(string $message = '')

src/Support/Format.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
use League\Fractal\Serializer\DataArraySerializer;
2929
use Spatie\Fractal\Fractal;
3030

31-
class Format implements \Jiannei\Response\Laravel\Contracts\Format
31+
class Format
3232
{
3333
use Macroable;
3434

@@ -49,13 +49,13 @@ public function response($data = [], int $status = 200, array $headers = [], int
4949
/**
5050
* Format return data structure.
5151
*
52-
* @param array|null $data
52+
* @param JsonResource|array|mixed $data
5353
* @param string|null $message
5454
* @param int $code
5555
* @param null $errors
5656
* @return array
5757
*/
58-
public function format(?array $data, ?string $message, int $code, $errors = null): array
58+
public function data($data, ?string $message, int $code, $errors = null): array
5959
{
6060
return $this->formatDataFields([
6161
'status' => $this->formatStatus($code),

src/Support/Traits/JsonResponseTrait.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
use Illuminate\Http\Resources\Json\ResourceCollection;
1919
use Illuminate\Pagination\AbstractCursorPaginator;
2020
use Illuminate\Pagination\AbstractPaginator;
21-
use Illuminate\Pagination\LengthAwarePaginator;
2221
use Illuminate\Support\Arr;
2322
use Illuminate\Support\Facades\Config;
23+
use Jiannei\Response\Laravel\Support\Facades\Format;
2424

2525
trait JsonResponseTrait
2626
{
@@ -30,7 +30,7 @@ trait JsonResponseTrait
3030
* @param array $data
3131
* @param string $message
3232
* @param string $location
33-
* @return JsonResponse|JsonResource
33+
* @return JsonResponse
3434
*/
3535
public function accepted($data = [], string $message = '', string $location = '')
3636
{
@@ -48,7 +48,7 @@ public function accepted($data = [], string $message = '', string $location = ''
4848
* @param null $data
4949
* @param string $message
5050
* @param string $location
51-
* @return JsonResponse|JsonResource
51+
* @return JsonResponse
5252
*/
5353
public function created($data = [], string $message = '', string $location = '')
5454
{
@@ -64,7 +64,7 @@ public function created($data = [], string $message = '', string $location = '')
6464
* Respond with a no content response.
6565
*
6666
* @param string $message
67-
* @return JsonResponse|JsonResource
67+
* @return JsonResponse
6868
*/
6969
public function noContent(string $message = '')
7070
{
@@ -78,7 +78,7 @@ public function noContent(string $message = '')
7878
* @param int $code
7979
* @param array $headers
8080
* @param int $option
81-
* @return JsonResponse|JsonResource
81+
* @return JsonResponse
8282
*/
8383
public function ok(string $message = '', int $code = 200, array $headers = [], int $option = 0)
8484
{
@@ -92,7 +92,7 @@ public function ok(string $message = '', int $code = 200, array $headers = [], i
9292
* @param int $code
9393
* @param array $headers
9494
* @param int $option
95-
* @return JsonResponse|JsonResource
95+
* @return JsonResponse
9696
*/
9797
public function localize(int $code = 200, array $headers = [], int $option = 0)
9898
{
@@ -173,8 +173,8 @@ public function errorInternal(string $message = '')
173173
*/
174174
public function fail(string $message = '', int $code = 500, $errors = null, array $header = [], int $options = 0)
175175
{
176-
$response = $this->formatter->response(
177-
$this->formatter->format(null, $message, $code, $errors),
176+
$response = Format::response(
177+
Format::data(null, $message, $code, $errors),
178178
Config::get('response.error_code') ?: $code,
179179
$header,
180180
$options
@@ -195,18 +195,18 @@ public function fail(string $message = '', int $code = 500, $errors = null, arra
195195
* @param int $code
196196
* @param array $headers
197197
* @param int $option
198-
* @return JsonResponse|JsonResource
198+
* @return JsonResponse
199199
*/
200200
public function success($data = [], string $message = '', int $code = 200, array $headers = [], int $option = 0)
201201
{
202202
$data = match (true) {
203-
$data instanceof ResourceCollection => $this->formatter->resourceCollection($data),
204-
$data instanceof JsonResource => $this->formatter->jsonResource($data),
205-
$data instanceof AbstractPaginator || $data instanceof AbstractCursorPaginator => $this->formatter->paginator($data),
203+
$data instanceof ResourceCollection => Format::resourceCollection($data),
204+
$data instanceof JsonResource => Format::jsonResource($data),
205+
$data instanceof AbstractPaginator || $data instanceof AbstractCursorPaginator => Format::paginator($data),
206206
$data instanceof Arrayable || (is_object($data) && method_exists($data, 'toArray')) => $data->toArray(),
207207
default => Arr::wrap($data)
208208
};
209209

210-
return $this->formatter->response($this->formatter->format($data,$message,$code), $code, $headers, $option);
210+
return Format::response(Format::data($data,$message,$code), $code, $headers, $option);
211211
}
212212
}

tests/Support/Format.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class Format extends \Jiannei\Response\Laravel\Support\Format
1515
{
16-
public function format(?array $data, ?string $message, int $code, $errors = null): array
16+
public function data($data, ?string $message, int $code, $errors = null): array
1717
{
1818
return [
1919
'status' => $this->formatStatus($code),

tests/TestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Jiannei\Response\Laravel\Tests;
1313

14+
use Jiannei\Response\Laravel\Tests\Support\Format;
15+
1416
abstract class TestCase extends \Orchestra\Testbench\TestCase
1517
{
1618
protected function getPackageProviders($app)

0 commit comments

Comments
 (0)