Skip to content

Commit

Permalink
refactor: rename
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Jun 8, 2024
1 parent 6174709 commit d410a81
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/Facades/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Spatie\Fractal\Facades\Fractal;

/**
* @method static \Apiato\Core\Services\ResponseTransformer createFrom(mixed $data = null, callable|TransformerAbstract|null $transformer = null, SerializerAbstract|null $serializer = null)
* @method static \Apiato\Core\Services\Response createFrom(mixed $data = null, callable|TransformerAbstract|null $transformer = null, SerializerAbstract|null $serializer = null)
* @method static JsonResponse ok()
* @method static JsonResponse created()
* @method static JsonResponse noContent()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Apiato\Core\Macros\ResponseTransformer;
namespace Apiato\Core\Macros\Response;

use Apiato\Core\Abstracts\Transformers\Transformer;
use Illuminate\Http\JsonResponse;
use Apiato\Core\Services\ResponseTransformer;
use Apiato\Core\Services\Response;

class Accepted {
public function __invoke(): callable
Expand All @@ -14,7 +14,7 @@ public function __invoke(): callable
* Returns a 202 Accepted response.
*/
function (): JsonResponse {
/** @var ResponseTransformer $this */
/** @var Response $this */
if (is_null($this->getTransformer())) {
$this->transformWith(Transformer::empty());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Apiato\Core\Macros\ResponseTransformer;
namespace Apiato\Core\Macros\Response;

use Apiato\Core\Services\ResponseTransformer;
use Apiato\Core\Services\Response;
use League\Fractal\Serializer\SerializerAbstract;
use League\Fractal\TransformerAbstract;

Expand All @@ -13,8 +13,8 @@ public function __invoke(): callable
/**
* Create a new Response instance.
*/
function (mixed $data = null, callable|TransformerAbstract|null $transformer = null, SerializerAbstract|null $serializer = null): ResponseTransformer {
return ResponseTransformer::create($data, $transformer, $serializer);
function (mixed $data = null, callable|TransformerAbstract|null $transformer = null, SerializerAbstract|null $serializer = null): Response {
return Response::create($data, $transformer, $serializer);
};
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Apiato\Core\Macros\ResponseTransformer;
namespace Apiato\Core\Macros\Response;

use Apiato\Core\Abstracts\Transformers\Transformer;
use Illuminate\Http\JsonResponse;
use Apiato\Core\Services\ResponseTransformer;
use Apiato\Core\Services\Response;

class Created {
public function __invoke(): callable
Expand All @@ -14,7 +14,7 @@ public function __invoke(): callable
* Returns a 201 Created response.
*/
function (): JsonResponse {
/** @var ResponseTransformer $this */
/** @var Response $this */
if (is_null($this->getTransformer())) {
$this->transformWith(Transformer::empty());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Apiato\Core\Macros\ResponseTransformer;
namespace Apiato\Core\Macros\Response;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Request;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php

namespace Apiato\Core\Macros\ResponseTransformer;
namespace Apiato\Core\Macros\Response;

use Apiato\Core\Services\ResponseTransformer;
use Apiato\Core\Services\Response;
use League\Fractal\TransformerAbstract;

class GetTransformer
{
public function __invoke(): callable
{
return function (): string|callable|TransformerAbstract|null {
/** @var ResponseTransformer $this */
/** @var Response $this */
// The warning is false positive. We will be in the context of Fractal class when this is called.
return $this->transformer;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Apiato\Core\Macros\ResponseTransformer;
namespace Apiato\Core\Macros\Response;

use Apiato\Core\Abstracts\Transformers\Transformer;
use Illuminate\Http\JsonResponse;
use Apiato\Core\Services\ResponseTransformer;
use Apiato\Core\Services\Response;

class NoContent {
public function __invoke(): callable
Expand All @@ -14,7 +14,7 @@ public function __invoke(): callable
* Returns a 204 No Content response.
*/
function (): JsonResponse {
/** @var ResponseTransformer $this */
/** @var Response $this */
if (is_null($this->getTransformer())) {
$this->transformWith(Transformer::empty());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Apiato\Core\Macros\ResponseTransformer;
namespace Apiato\Core\Macros\Response;

use Apiato\Core\Abstracts\Transformers\Transformer;
use Illuminate\Http\JsonResponse;
use Apiato\Core\Services\ResponseTransformer;
use Apiato\Core\Services\Response;

class Ok {
public function __invoke(): callable
Expand All @@ -14,7 +14,7 @@ public function __invoke(): callable
* Returns a 200 OK response.
*/
function (): JsonResponse {
/** @var ResponseTransformer $this */
/** @var Response $this */
if (is_null($this->getTransformer())) {
$this->transformWith(Transformer::empty());
}
Expand Down
4 changes: 2 additions & 2 deletions src/Providers/ApiatoServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Apiato\Core\Foundation\Apiato;
use Apiato\Core\Loaders\AutoLoaderTrait;
use Apiato\Core\Providers\MacroProviders\ConfigMacroServiceProvider;
use Apiato\Core\Providers\MacroProviders\ResponseTransformerMacroServiceProvider;
use Apiato\Core\Providers\MacroProviders\ResponseMacroServiceProvider;
use Apiato\Core\Traits\ValidationTrait;
use Illuminate\Support\Facades\Schema;

Expand All @@ -17,7 +17,7 @@ class ApiatoServiceProvider extends AbstractMainServiceProvider

public array $serviceProviders = [
ConfigMacroServiceProvider::class,
ResponseTransformerMacroServiceProvider::class,
ResponseMacroServiceProvider::class,
];

public function register(): void
Expand Down
38 changes: 38 additions & 0 deletions src/Providers/MacroProviders/ResponseMacroServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Apiato\Core\Providers\MacroProviders;

use Apiato\Core\Macros\Response\GetRequestedIncludes;
use Apiato\Core\Services\Response;
use Apiato\Core\Macros\Response\Accepted;
use Apiato\Core\Macros\Response\Created;
use Apiato\Core\Macros\Response\CreateFrom;
use Apiato\Core\Macros\Response\GetTransformer;
use Apiato\Core\Macros\Response\NoContent;
use Apiato\Core\Macros\Response\Ok;
use Apiato\Core\Abstracts\Providers\MainServiceProvider as AbstractMainServiceProvider;
use Illuminate\Support\Collection;

final class ResponseMacroServiceProvider extends AbstractMainServiceProvider {
public function boot(): void
{
parent::boot();

Collection::make($this->macros())
->reject(static fn ($class, $macro) => Response::hasMacro($macro))
->each(static fn ($class, $macro) => Response::macro($macro, app($class)()));
}

private function macros(): array
{
return [
'ok' => Ok::class,
'created' => Created::class,
'noContent' => NoContent::class,
'accepted' => Accepted::class,
'createFrom' => CreateFrom::class,
'getTransformer' => GetTransformer::class,
'getRequestedIncludes' => GetRequestedIncludes::class,
];
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @see \Spatie\Fractal\Fractal
*/
class ResponseTransformer extends SpatieFractal
class Response extends SpatieFractal
{
public function createData(): Scope
{
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/CanEagerLoadTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Apiato\Core\Traits;

use Apiato\Core\Services\ResponseTransformer;
use Apiato\Core\Services\Response;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Request;
Expand All @@ -20,7 +20,7 @@ protected function eagerLoadRequestedRelations(): void
$this->scopeQuery(function (Builder|Model $model) {
if (Request::has(config('apiato.requests.params.include', 'include'))) {
$validIncludes = [];
foreach (ResponseTransformer::getRequestedIncludes() as $includeName) {
foreach (Response::getRequestedIncludes() as $includeName) {
$relationParts = explode('.', $includeName);
$camelCasedIncludeName = $this->validateNestedRelations($this->model, $relationParts);
if ($camelCasedIncludeName) {
Expand Down

0 comments on commit d410a81

Please sign in to comment.