diff --git a/src/Auth/ApiGuard.php b/src/Auth/ApiGuard.php index becd9b8..22a615c 100644 --- a/src/Auth/ApiGuard.php +++ b/src/Auth/ApiGuard.php @@ -7,14 +7,13 @@ use Illuminate\Http\Request; /** - * Class ApiGuard - * @package Napp\Core\Api\Auth + * Class ApiGuard. */ class ApiGuard extends TokenGuard { /** * @param UserProvider $provider - * @param Request $request + * @param Request $request */ public function __construct(UserProvider $provider, Request $request) { @@ -25,6 +24,7 @@ public function __construct(UserProvider $provider, Request $request) /** * @param array $credentials + * * @return bool */ public function attempt(array $credentials = []): bool diff --git a/src/Auth/AuthServiceProvider.php b/src/Auth/AuthServiceProvider.php index bb8cb31..e3c1c7c 100644 --- a/src/Auth/AuthServiceProvider.php +++ b/src/Auth/AuthServiceProvider.php @@ -6,8 +6,7 @@ use Illuminate\Support\ServiceProvider; /** - * Class AuthServiceProvider - * @package Napp\Core\Api\Auth + * Class AuthServiceProvider. */ class AuthServiceProvider extends ServiceProvider { diff --git a/src/Auth/NappHttpHeaders.php b/src/Auth/NappHttpHeaders.php index bc8c763..fa04138 100644 --- a/src/Auth/NappHttpHeaders.php +++ b/src/Auth/NappHttpHeaders.php @@ -3,8 +3,7 @@ namespace Napp\Core\Api\Auth; /** - * Class NappHttpHeaders - * @package Napp\Core\Api\Auth + * Class NappHttpHeaders. */ class NappHttpHeaders { diff --git a/src/Controllers/ApiController.php b/src/Controllers/ApiController.php index bde6bd4..0d4fbc6 100644 --- a/src/Controllers/ApiController.php +++ b/src/Controllers/ApiController.php @@ -7,8 +7,7 @@ use Illuminate\Support\Facades\Response; /** - * Class ApiController - * @package Napp\Core\Api\Controllers + * Class ApiController. */ class ApiController extends BaseController { @@ -40,6 +39,7 @@ public function getStatusCode() /** * @param int $code + * * @return $this */ public function setStatusCode($code) @@ -51,6 +51,7 @@ public function setStatusCode($code) /** * @param int $code + * * @return $this */ public function setResponseCode($code) @@ -62,6 +63,7 @@ public function setResponseCode($code) /** * @param string $currentETag + * * @return JsonResponse */ public function checkETag($currentETag): ?JsonResponse @@ -76,6 +78,7 @@ public function checkETag($currentETag): ?JsonResponse /** * @param array $data * @param array $headers + * * @return JsonResponse */ public function respond(array $data, array $headers = []): JsonResponse @@ -86,6 +89,7 @@ public function respond(array $data, array $headers = []): JsonResponse /** * @param array $data * @param array $headers + * * @return JsonResponse */ public function respondWithSingleObject(array $data, array $headers = []): JsonResponse @@ -95,6 +99,7 @@ public function respondWithSingleObject(array $data, array $headers = []): JsonR /** * @param string $message + * * @return JsonResponse */ public function respondWithError(string $message): JsonResponse @@ -102,14 +107,15 @@ public function respondWithError(string $message): JsonResponse return $this->respond([ 'error' => [ 'message' => $message, - 'code' => $this->getStatusCode() - ] + 'code' => $this->getStatusCode(), + ], ]); } /** * @param array $data * @param array $headers + * * @return JsonResponse */ public function responseCreated(array $data, array $headers = []): JsonResponse @@ -120,7 +126,8 @@ public function responseCreated(array $data, array $headers = []): JsonResponse /** * @param string $message - * @param array $headers + * @param array $headers + * * @return JsonResponse */ public function responseNoContent( @@ -130,17 +137,17 @@ public function responseNoContent( return $this->setResponseCode(204) ->respond(['message' => $message], $headers); } - - /** + + /** * @param string $message - * @param array $headers + * @param array $headers + * * @return JsonResponse */ public function responseAccepted( string $message = 'The request was accepted for processing', array $headers = [] ): JsonResponse { - return $this->setResponseCode(202) ->setStatusCode(224) ->respond(['message' => $message], $headers); @@ -148,6 +155,7 @@ public function responseAccepted( /** * @param array $headers + * * @return JsonResponse */ public function responseNotModified(array $headers = []): JsonResponse @@ -158,6 +166,7 @@ public function responseNotModified(array $headers = []): JsonResponse /** * @param string $message + * * @return \Illuminate\Http\JsonResponse */ public function responseBadRequest(string $message = 'The request failed due to an application error.'): JsonResponse @@ -169,6 +178,7 @@ public function responseBadRequest(string $message = 'The request failed due to /** * @param string $message + * * @return JsonResponse */ public function responseValidationFailed(string $message = 'Validation error.'): JsonResponse @@ -180,6 +190,7 @@ public function responseValidationFailed(string $message = 'Validation error.'): /** * @param string $message + * * @return JsonResponse */ public function responseUnprocessableEntity(string $message = 'The request was well-formed but was unable to be followed due to semantic errors.'): JsonResponse @@ -191,6 +202,7 @@ public function responseUnprocessableEntity(string $message = 'The request was w /** * @param string $message + * * @return JsonResponse */ public function responseUnauthorized(string $message = 'Authentication credentials were missing or incorrect.'): JsonResponse @@ -202,6 +214,7 @@ public function responseUnauthorized(string $message = 'Authentication credentia /** * @param string $message + * * @return JsonResponse */ public function responseUnauthorization(string $message = 'Authorization error. Requested resource is restricted.'): JsonResponse @@ -213,6 +226,7 @@ public function responseUnauthorization(string $message = 'Authorization error. /** * @param string $message + * * @return JsonResponse */ public function responseForbidden(string $message = 'Forbidden.'): JsonResponse @@ -224,6 +238,7 @@ public function responseForbidden(string $message = 'Forbidden.'): JsonResponse /** * @param string $message + * * @return JsonResponse */ public function responseNotFound(string $message = 'Not found.'): JsonResponse @@ -235,6 +250,7 @@ public function responseNotFound(string $message = 'Not found.'): JsonResponse /** * @param string $message + * * @return JsonResponse */ public function responseClientNotFound(string $message = 'Client not found.'): JsonResponse @@ -246,6 +262,7 @@ public function responseClientNotFound(string $message = 'Client not found.'): J /** * @param string $message + * * @return JsonResponse */ public function responseInternalServerError(string $message = 'Internal Server Error.'): JsonResponse @@ -257,6 +274,7 @@ public function responseInternalServerError(string $message = 'Internal Server E /** * @param string $message + * * @return JsonResponse */ public function responseNotImplemented(string $message = 'The request has not been implemented.'): JsonResponse @@ -268,6 +286,7 @@ public function responseNotImplemented(string $message = 'The request has not be /** * @param string $message + * * @return JsonResponse */ public function responseServiceUnavailable(string $message = 'Service Unavailable.'): JsonResponse @@ -279,6 +298,7 @@ public function responseServiceUnavailable(string $message = 'Service Unavailabl /** * @param string $message + * * @return JsonResponse */ public function responseHTTPVersionNotSupported(string $message = 'This is returned if you use the HTTP protocol.'): JsonResponse @@ -290,6 +310,7 @@ public function responseHTTPVersionNotSupported(string $message = 'This is retur /** * @param Request $request + * * @return int */ public function getLimit(Request $request): int @@ -305,6 +326,7 @@ public function getLimit(Request $request): int /** * @param Request $request + * * @return int */ protected function getOffset(Request $request): int @@ -313,9 +335,10 @@ protected function getOffset(Request $request): int } /** - * Get the page for pagination + * Get the page for pagination. * * @param Request $request + * * @return int */ public function getPage(Request $request): int @@ -324,9 +347,10 @@ public function getPage(Request $request): int } /** - * Get the page size for pagination + * Get the page size for pagination. * * @param Request $request + * * @return int */ public function getPageSize(Request $request): int diff --git a/src/Controllers/ApiInternalController.php b/src/Controllers/ApiInternalController.php index 9697a1d..98bca19 100644 --- a/src/Controllers/ApiInternalController.php +++ b/src/Controllers/ApiInternalController.php @@ -5,13 +5,12 @@ use Illuminate\Auth\AuthManager; use Illuminate\Http\JsonResponse; use Illuminate\Http\Response; +use Napp\Core\Api\Auth\NappHttpHeaders; use Napp\Core\Api\Exceptions\Exceptions\ApiInternalCallException; use Napp\Core\Api\Router\Router; -use Napp\Core\Api\Auth\NappHttpHeaders; /** - * Class ApiInternalController - * @package Napp\Core\Api\Controllers + * Class ApiInternalController. */ class ApiInternalController extends BaseController { @@ -27,7 +26,7 @@ class ApiInternalController extends BaseController /** * @param AuthManager $auth - * @param Router $router + * @param Router $router */ public function __construct(AuthManager $auth, Router $router) { @@ -45,7 +44,8 @@ public function getInternalRouter(): Router /** * @param string $uri - * @param array $headers + * @param array $headers + * * @return array */ public function get(string $uri, array $headers = []): array @@ -57,8 +57,9 @@ public function get(string $uri, array $headers = []): array /** * @param string $uri - * @param array $data - * @param array $headers + * @param array $data + * @param array $headers + * * @return array */ public function post(string $uri, array $data, array $headers = []): array @@ -70,8 +71,9 @@ public function post(string $uri, array $data, array $headers = []): array /** * @param string $uri - * @param array $data - * @param array $headers + * @param array $data + * @param array $headers + * * @return array */ public function put(string $uri, array $data, array $headers = []): array @@ -83,7 +85,8 @@ public function put(string $uri, array $data, array $headers = []): array /** * @param string $uri - * @param array $headers + * @param array $headers + * * @return array */ public function delete(string $uri, array $headers = []): array @@ -95,20 +98,23 @@ public function delete(string $uri, array $headers = []): array /** * @param array $headers + * * @return array */ protected function getInternalCallHeaders(array $headers): array { return array_merge($headers, [ - NappHttpHeaders::NAPP_API_CALL_TYPE => 'internal', + NappHttpHeaders::NAPP_API_CALL_TYPE => 'internal', NappHttpHeaders::NAPP_AUTH_GLOBAL_USER_ID => $this->auth->guard()->id(), ]); } /** * @param Response|JsonResponse $response - * @return array + * * @throws ApiInternalCallException + * + * @return array */ protected function formatResponse($response): array { diff --git a/src/Controllers/ApiProxyController.php b/src/Controllers/ApiProxyController.php index 34b5b14..e0419da 100644 --- a/src/Controllers/ApiProxyController.php +++ b/src/Controllers/ApiProxyController.php @@ -6,14 +6,14 @@ use Illuminate\Http\Request; /** - * Class ApiProxyController - * @package Napp\Core\Api\Controllers + * Class ApiProxyController. */ class ApiProxyController extends ApiInternalController { /** - * @param string $endpoint + * @param string $endpoint * @param Request $request + * * @return JsonResponse */ public function __invoke(string $endpoint, Request $request): JsonResponse @@ -24,8 +24,9 @@ public function __invoke(string $endpoint, Request $request): JsonResponse } /** - * @param string $endpoint + * @param string $endpoint * @param Request $request + * * @return mixed */ private function callApi(string $endpoint, Request $request) @@ -44,7 +45,7 @@ private function callApi(string $endpoint, Request $request) } if (false === empty($arguments)) { - $callArguments = [$url . '?' . http_build_query($arguments)]; + $callArguments = [$url.'?'.http_build_query($arguments)]; } return $this->{$requestMethod}(...$callArguments); diff --git a/src/Controllers/BaseController.php b/src/Controllers/BaseController.php index 373d914..4d02603 100644 --- a/src/Controllers/BaseController.php +++ b/src/Controllers/BaseController.php @@ -8,8 +8,7 @@ use Illuminate\Routing\Controller; /** - * Class BaseController - * @package Napp\Core\Api\Controllers + * Class BaseController. */ class BaseController extends Controller { diff --git a/src/Exceptions/ApiHandler.php b/src/Exceptions/ApiHandler.php index b1fdd17..972ea70 100644 --- a/src/Exceptions/ApiHandler.php +++ b/src/Exceptions/ApiHandler.php @@ -8,33 +8,34 @@ use Napp\Core\Api\Exceptions\Exceptions\ApiInternalCallValidationException; /** - * Class ApiHandler - * @package Napp\Core\Api\Exceptions + * Class ApiHandler. */ class ApiHandler extends ExceptionHandler { /** * @param \Illuminate\Http\Request $request - * @param \Exception $e - * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse + * @param \Exception $e + * * @throws \ReflectionException + * + * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse */ public function render($request, Exception $e) { if (true === app()->isDownForMaintenance()) { return response()->json([ 'error' => [ - 'code' => 503, - 'message' => 'Service is down for scheduled maintenance. Be right back!' - ] + 'code' => 503, + 'message' => 'Service is down for scheduled maintenance. Be right back!', + ], ], 503); } if (true === $e instanceof ApiInternalCallValidationException) { $response = response([ 'error' => [ - 'code' => 215, - 'message' => 'Validation failed'] + 'code' => 215, + 'message' => 'Validation failed', ], ], 400); return $response->withException($e); @@ -43,9 +44,9 @@ public function render($request, Exception $e) if ($e instanceof AuthenticationException) { return response()->json([ 'error' => [ - 'code' => 64, - 'message' => 'Forbidden' - ] + 'code' => 64, + 'message' => 'Forbidden', + ], ], 403); } diff --git a/src/Exceptions/Exceptions/ActionNotAllowedException.php b/src/Exceptions/Exceptions/ActionNotAllowedException.php index 0f6d053..e6ca422 100644 --- a/src/Exceptions/Exceptions/ActionNotAllowedException.php +++ b/src/Exceptions/Exceptions/ActionNotAllowedException.php @@ -3,8 +3,7 @@ namespace Napp\Core\Api\Exceptions\Exceptions; /** - * Class ActionNotAllowedException - * @package Napp\Core\Api\Exceptions\Exceptions + * Class ActionNotAllowedException. */ class ActionNotAllowedException extends Exception { diff --git a/src/Exceptions/Exceptions/ApiInternalCallException.php b/src/Exceptions/Exceptions/ApiInternalCallException.php index f739517..8437d3b 100644 --- a/src/Exceptions/Exceptions/ApiInternalCallException.php +++ b/src/Exceptions/Exceptions/ApiInternalCallException.php @@ -5,8 +5,7 @@ use Symfony\Component\HttpFoundation\Response; /** - * Class ApiInternalCallException - * @package Napp\Core\Api\Exceptions\Exceptions + * Class ApiInternalCallException. */ class ApiInternalCallException extends \RuntimeException { @@ -27,7 +26,7 @@ class ApiInternalCallException extends \RuntimeException /** * @param Response $response - * @param string $message + * @param string $message */ public function __construct(Response $response, $message = 'There was an error while processing your request') { @@ -61,4 +60,3 @@ public function getPreviousException() return $this->previousException; } } - diff --git a/src/Exceptions/Exceptions/ApiInternalCallValidationException.php b/src/Exceptions/Exceptions/ApiInternalCallValidationException.php index 73806a0..a6f2080 100644 --- a/src/Exceptions/Exceptions/ApiInternalCallValidationException.php +++ b/src/Exceptions/Exceptions/ApiInternalCallValidationException.php @@ -3,8 +3,7 @@ namespace Napp\Core\Api\Exceptions\Exceptions; /** - * Class ApiInternalCallValidationException - * @package Napp\Core\Api\Exceptions\Exceptions + * Class ApiInternalCallValidationException. */ class ApiInternalCallValidationException extends \Exception { diff --git a/src/Exceptions/Exceptions/AuthenticationIncorrectException.php b/src/Exceptions/Exceptions/AuthenticationIncorrectException.php index 810bd06..fd6ca91 100644 --- a/src/Exceptions/Exceptions/AuthenticationIncorrectException.php +++ b/src/Exceptions/Exceptions/AuthenticationIncorrectException.php @@ -1,10 +1,9 @@ - [ - 'code' => $this->statusCode, - 'message' => $this->statusMessage, - 'validation' => $this->validation - ] + 'code' => $this->statusCode, + 'message' => $this->statusMessage, + 'validation' => $this->validation, + ], ]; } } diff --git a/src/Exceptions/NappApiHandler.php b/src/Exceptions/NappApiHandler.php index 62487cc..c4ea623 100644 --- a/src/Exceptions/NappApiHandler.php +++ b/src/Exceptions/NappApiHandler.php @@ -2,15 +2,14 @@ namespace Napp\Core\Api\Exceptions; +use Illuminate\Http\JsonResponse; use Napp\Core\Api\Exceptions\Exceptions\Exception as NappException; use Napp\Core\Api\Exceptions\Renderer\DebugRenderer; use Napp\Core\Api\Exceptions\Renderer\Renderer; use Napp\Core\Api\Exceptions\Renderer\RendererInterface; -use Illuminate\Http\JsonResponse; /** - * Class NappApiHandler - * @package Napp\Core\Api\Exceptions + * Class NappApiHandler. */ class NappApiHandler { @@ -21,6 +20,7 @@ class NappApiHandler /** * @param \Exception $e + * * @throws \ReflectionException */ public function __construct(\Exception $e) @@ -47,19 +47,20 @@ public function render(): JsonResponse /** * @param \Exception $e + * * @return int */ protected function getResponseCode(\Exception $e): ?int { if (true === $e instanceof NappException) { - /** @var NappException $e */ + /* @var NappException $e */ return $e->getResponseCode(); } if (true === \method_exists($e, 'getStatusCode')) { return $e->getStatusCode(); } - + if (\property_exists($e, 'status')) { return $e->status; } @@ -74,12 +75,13 @@ protected function getResponseCode(\Exception $e): ?int /** * @param \Exception $e + * * @return int */ protected function getStatusCode(\Exception $e): ?int { if (true === $e instanceof NappException) { - /** @var NappException $e */ + /* @var NappException $e */ return $e->getStatusCode(); } @@ -105,13 +107,15 @@ protected function getStatusCode(\Exception $e): ?int /** * @param \Exception $e - * @return string + * * @throws \ReflectionException + * + * @return string */ protected function getStatusMessage(\Exception $e): string { if (true === $e instanceof NappException) { - /** @var NappException $e */ + /* @var NappException $e */ return $e->getStatusMessage(); } diff --git a/src/Exceptions/Renderer/DebugRenderer.php b/src/Exceptions/Renderer/DebugRenderer.php index b540c13..099f1f9 100644 --- a/src/Exceptions/Renderer/DebugRenderer.php +++ b/src/Exceptions/Renderer/DebugRenderer.php @@ -5,8 +5,7 @@ use Illuminate\Http\JsonResponse; /** - * Class DebugRenderer - * @package Napp\Core\Api\Exceptions\Renderer + * Class DebugRenderer. */ class DebugRenderer implements RendererInterface { @@ -40,11 +39,11 @@ public function render(): JsonResponse $this->exception->jsonSerialize(), [ 'error' => [ - 'type' => \get_class($this->exception), - 'file' => $this->exception->getFile(), - 'line' => $this->exception->getLine(), - 'trace' => $this->formatTrace($this->exception->getTrace()) - ] + 'type' => \get_class($this->exception), + 'file' => $this->exception->getFile(), + 'line' => $this->exception->getLine(), + 'trace' => $this->formatTrace($this->exception->getTrace()), + ], ]), $this->responseCode); } @@ -52,19 +51,20 @@ public function render(): JsonResponse return response()->json( [ 'error' => [ - 'code' => $this->statusCode, + 'code' => $this->statusCode, 'message' => $this->statusMessage, - 'type' => \get_class($this->exception), - 'file' => $this->exception->getFile(), - 'line' => $this->exception->getLine(), - 'trace' => $this->formatTrace($this->exception->getTrace()) - ]], + 'type' => \get_class($this->exception), + 'file' => $this->exception->getFile(), + 'line' => $this->exception->getLine(), + 'trace' => $this->formatTrace($this->exception->getTrace()), + ], ], $this->responseCode ); } /** * @param \Exception $e + * * @return void */ public function setException(\Exception $e) @@ -74,6 +74,7 @@ public function setException(\Exception $e) /** * @param int $responseCode + * * @return void */ public function setResponseCode($responseCode) @@ -83,6 +84,7 @@ public function setResponseCode($responseCode) /** * @param int $statusCode + * * @return void */ public function setStatusCode($statusCode) @@ -92,6 +94,7 @@ public function setStatusCode($statusCode) /** * @param string $statusMessage + * * @return void */ public function setStatusMessage($statusMessage) @@ -103,6 +106,7 @@ public function setStatusMessage($statusMessage) * Remove the args property from the trace array objects. * * @param array $trace + * * @return array */ protected function formatTrace(array $trace) diff --git a/src/Exceptions/Renderer/Renderer.php b/src/Exceptions/Renderer/Renderer.php index 8069005..4b3d416 100644 --- a/src/Exceptions/Renderer/Renderer.php +++ b/src/Exceptions/Renderer/Renderer.php @@ -6,8 +6,7 @@ use Napp\Core\Api\Exceptions\Exceptions\Exception as NappException; /** - * Class Renderer - * @package Napp\Core\Api\Exceptions\Renderer + * Class Renderer. */ class Renderer implements RendererInterface { @@ -44,9 +43,9 @@ public function render(): JsonResponse return response()->json( [ 'error' => [ - 'code' => $this->statusCode, + 'code' => $this->statusCode, 'message' => $this->statusMessage, - ]], + ], ], $this->responseCode ); } @@ -56,54 +55,54 @@ public function render(): JsonResponse return response()->json( [ 'error' => [ - 'code' => $this->statusCode, - 'message' => 'Unprocessable Entity' - ]], + 'code' => $this->statusCode, + 'message' => 'Unprocessable Entity', + ], ], $this->responseCode ); case 401: return response()->json( [ 'error' => [ - 'code' => $this->statusCode, - 'message' => 'Authentication credentials were missing or incorrect' - ]], + 'code' => $this->statusCode, + 'message' => 'Authentication credentials were missing or incorrect', + ], ], $this->responseCode ); case 403: return response()->json( [ 'error' => [ - 'code' => $this->statusCode, - 'message' => 'Forbidden' - ]], + 'code' => $this->statusCode, + 'message' => 'Forbidden', + ], ], $this->responseCode ); case 404: return response()->json( [ 'error' => [ - 'code' => $this->statusCode, - 'message' => 'Not Found' - ]], + 'code' => $this->statusCode, + 'message' => 'Not Found', + ], ], $this->responseCode ); case 405: return response()->json( [ 'error' => [ - 'code' => $this->statusCode, - 'message' => 'Method Not Allowed' - ]], + 'code' => $this->statusCode, + 'message' => 'Method Not Allowed', + ], ], $this->responseCode ); default: return response()->json( [ 'error' => [ - 'code' => $this->statusCode, - 'message' => 'Internal Server Error' - ]], + 'code' => $this->statusCode, + 'message' => 'Internal Server Error', + ], ], 500 ); } @@ -111,6 +110,7 @@ public function render(): JsonResponse /** * @param \Exception $e + * * @return void */ public function setException(\Exception $e) @@ -120,6 +120,7 @@ public function setException(\Exception $e) /** * @param int $responseCode + * * @return void */ public function setResponseCode($responseCode) @@ -129,6 +130,7 @@ public function setResponseCode($responseCode) /** * @param int $statusCode + * * @return void */ public function setStatusCode($statusCode) @@ -138,6 +140,7 @@ public function setStatusCode($statusCode) /** * @param string $statusMessage + * * @return void */ public function setStatusMessage($statusMessage) diff --git a/src/Exceptions/Renderer/RendererInterface.php b/src/Exceptions/Renderer/RendererInterface.php index 48054c2..9a5ed7a 100644 --- a/src/Exceptions/Renderer/RendererInterface.php +++ b/src/Exceptions/Renderer/RendererInterface.php @@ -5,8 +5,7 @@ use Illuminate\Http\JsonResponse; /** - * Interface RendererInterface - * @package Napp\Core\Api\Exceptions\Renderer + * Interface RendererInterface. */ interface RendererInterface { @@ -17,24 +16,28 @@ public function render(): JsonResponse; /** * @param \Exception $e + * * @return void */ public function setException(\Exception $e); /** * @param int $responseCode + * * @return void */ public function setResponseCode($responseCode); /** * @param int $statusCode + * * @return void */ public function setStatusCode($statusCode); /** * @param string $statusMessage + * * @return void */ public function setStatusMessage($statusMessage); diff --git a/src/Middleware/ETag.php b/src/Middleware/ETag.php index 53294bb..b328e03 100644 --- a/src/Middleware/ETag.php +++ b/src/Middleware/ETag.php @@ -5,16 +5,16 @@ use Closure; /** - * Class ETag - * @package Napp\Core\Api\Middleware + * Class ETag. */ class ETag { /** * Implement ETag support. * - * @param \Illuminate\Http\Request $request - * @param \Closure $next + * @param \Illuminate\Http\Request $request + * @param \Closure $next + * * @return mixed */ public function handle($request, Closure $next) diff --git a/src/Requests/ApiRequest.php b/src/Requests/ApiRequest.php index 10a1c88..e5034f4 100644 --- a/src/Requests/ApiRequest.php +++ b/src/Requests/ApiRequest.php @@ -11,14 +11,14 @@ use Napp\Core\Api\Transformers\TransformerInterface; /** - * Class ApiRequest - * @package Napp\Core\Api\Requests + * Class ApiRequest. */ abstract class ApiRequest extends FormRequest { /** - * @return Validator * @throws \Napp\Core\Api\Exceptions\Exceptions\InvalidFieldException + * + * @return Validator */ protected function getValidatorInstance() { @@ -30,10 +30,12 @@ protected function getValidatorInstance() /** * @param Validator $validator - * @return void + * * @throws \Illuminate\Validation\ValidationException * @throws \Napp\Core\Api\Exceptions\Exceptions\ApiInternalCallValidationException * @throws \Napp\Core\Api\Exceptions\Exceptions\ValidationException + * + * @return void */ protected function failedValidation(Validator $validator) { @@ -45,16 +47,17 @@ protected function failedValidation(Validator $validator) } /** - * @return void * @throws InvalidFieldException + * + * @return void */ protected function validateInputFields(): void { $input = $this->input(); $rules = $this->rules(); if (false === empty(array_diff_key($input, $rules))) { - $exception = new InvalidFieldException; - $exception->statusMessage = $exception->statusMessage . ': ' . implode(',', array_keys(array_diff_key($input, $rules))); + $exception = new InvalidFieldException(); + $exception->statusMessage = $exception->statusMessage.': '.implode(',', array_keys(array_diff_key($input, $rules))); throw $exception; } @@ -85,6 +88,7 @@ protected function getTransformer(): TransformerInterface /** * @see AppServiceProvider + * * @return bool */ protected function isApiInternalCall(): bool @@ -99,15 +103,17 @@ protected function isApiInternalCall(): bool /** * @param Validator $validator - * @return void + * * @throws ValidationException * @throws \Illuminate\Validation\ValidationException + * + * @return void */ protected function handleApiCallFailedValidation(Validator $validator) { $message = $validator->messages()->first(); $exception = new ValidationException(); - $exception->statusMessage = $exception->statusMessage . ': ' . $message; + $exception->statusMessage = $exception->statusMessage.': '.$message; $exception->validation = $this->transformValidationOutput($validator); throw $exception; @@ -115,8 +121,10 @@ protected function handleApiCallFailedValidation(Validator $validator) /** * @param Validator $validator - * @return void + * * @throws ApiInternalCallValidationException + * + * @return void */ protected function handleApiInternalCallFailedValidation(Validator $validator): void { @@ -127,17 +135,19 @@ protected function handleApiInternalCallFailedValidation(Validator $validator): } /** - * @param array $input + * @param array $input * @param string $key + * * @return bool */ protected function isValueSet(array $input, string $key): bool { - return (true === isset($input[$key]) && false === empty($input[$key])); + return true === isset($input[$key]) && false === empty($input[$key]); } /** * @param Validator $validator + * * @return array */ protected function transformValidationOutput($validator): array @@ -148,4 +158,4 @@ protected function transformValidationOutput($validator): array }) ->toArray(); } -} \ No newline at end of file +} diff --git a/src/Requests/Provider/RequestServiceProvider.php b/src/Requests/Provider/RequestServiceProvider.php index 028c675..7d80d04 100644 --- a/src/Requests/Provider/RequestServiceProvider.php +++ b/src/Requests/Provider/RequestServiceProvider.php @@ -7,8 +7,7 @@ use Napp\Core\Api\Auth\NappHttpHeaders; /** - * Class RequestServiceProvider - * @package Napp\Core\Api\Requests\Provider + * Class RequestServiceProvider. */ class RequestServiceProvider extends ServiceProvider { diff --git a/src/Router/Provider/RouterServiceProvider.php b/src/Router/Provider/RouterServiceProvider.php index 6358c7f..a99c371 100644 --- a/src/Router/Provider/RouterServiceProvider.php +++ b/src/Router/Provider/RouterServiceProvider.php @@ -6,8 +6,7 @@ use Napp\Core\Api\Router\Router; /** - * Class RouterServiceProvider - * @package Napp\Core\Api\Router\Provider + * Class RouterServiceProvider. */ class RouterServiceProvider extends BaseProvider { diff --git a/src/Router/Router.php b/src/Router/Router.php index 1cf2866..77690da 100644 --- a/src/Router/Router.php +++ b/src/Router/Router.php @@ -7,8 +7,7 @@ use Illuminate\Routing\Router as LaravelRouter; /** - * Class Router - * @package Napp\Core\Api\Router + * Class Router. */ class Router { @@ -22,8 +21,8 @@ class Router /** * @param \Illuminate\Foundation\Application $app - * @param \Illuminate\Http\Request $request, - * @param \Illuminate\Routing\Router $router + * @param \Illuminate\Http\Request $request, + * @param \Illuminate\Routing\Router $router */ public function __construct(Application $app, Request $request, LaravelRouter $router) { @@ -33,10 +32,11 @@ public function __construct(Application $app, Request $request, LaravelRouter $r } /** - * @param string $uri - * @param array $data - * @param array $headers - * @param string $content + * @param string $uri + * @param array $data + * @param array $headers + * @param string $content + * * @return \Illuminate\Http\Response */ public function get() @@ -45,10 +45,11 @@ public function get() } /** - * @param string $uri - * @param array $data - * @param array $headers - * @param string $content + * @param string $uri + * @param array $data + * @param array $headers + * @param string $content + * * @return \Illuminate\Http\Response */ public function post() @@ -57,10 +58,11 @@ public function post() } /** - * @param string $uri - * @param array $data - * @param array $headers - * @param string $content + * @param string $uri + * @param array $data + * @param array $headers + * @param string $content + * * @return \Illuminate\Http\Response */ public function put() @@ -69,10 +71,11 @@ public function put() } /** - * @param string $uri - * @param array $data - * @param array $headers - * @param string $content + * @param string $uri + * @param array $data + * @param array $headers + * @param string $content + * * @return \Illuminate\Http\Response */ public function delete() @@ -81,7 +84,8 @@ public function delete() } /** - * @param array $requests An array of requests + * @param array $requests An array of requests + * * @return array */ public function batchRequest(array $requests) @@ -94,24 +98,28 @@ public function batchRequest(array $requests) } /** - * @param string $method - * @param array $args + * @param string $method + * @param array $args + * * @return \Illuminate\Http\Response */ public function quickCall($method, array $args) { array_unshift($args, $method); - return call_user_func_array([$this, "singleRequest"], $args); + + return call_user_func_array([$this, 'singleRequest'], $args); } /** - * @param string $method - * @param string $uri - * @param array $data - * @param array $headers - * @param string $content - * @return \Illuminate\Http\Response + * @param string $method + * @param string $uri + * @param array $data + * @param array $headers + * @param string $content + * * @throws \Exception + * + * @return \Illuminate\Http\Response */ public function singleRequest($method, $uri, array $data = [], array $headers = [], $content = null) { @@ -141,11 +149,13 @@ public function singleRequest($method, $uri, array $data = [], array $headers = /** * @param array $default * @param array $headers + * * @return array */ private function overrideHeaders(array $default, array $headers) { $headers = $this->transformHeadersToUppercaseUnderscoreType($headers); + return array_merge($default, $headers); } @@ -160,13 +170,15 @@ public function disableMiddleware() } /** - * @param string $method - * @param string $uri - * @param array $data - * @param array $headers - * @param string $content - * @return \Illuminate\Http\Response + * @param string $method + * @param string $uri + * @param array $data + * @param array $headers + * @param string $content + * * @throws \Exception + * + * @return \Illuminate\Http\Response */ private function request($method, $uri, array $data = [], array $headers = [], $content = null) { @@ -180,11 +192,12 @@ private function request($method, $uri, array $data = [], array $headers = [], $ } /** - * @param string $method - * @param string $uri - * @param array $data - * @param array $headers - * @param string $content + * @param string $method + * @param string $uri + * @param array $data + * @param array $headers + * @param string $content + * * @return \Illuminate\Http\Request */ private function createRequest($method, $uri, array $data = [], array $headers = [], $content = null) @@ -196,6 +209,7 @@ private function createRequest($method, $uri, array $data = [], array $headers = /** * @param $headers + * * @return array */ private function transformHeadersToUppercaseUnderscoreType($headers) @@ -212,9 +226,10 @@ private function transformHeadersToUppercaseUnderscoreType($headers) } /** - * https://github.com/symfony/symfony/issues/5074 + * https://github.com/symfony/symfony/issues/5074. + * + * @param array $headers * - * @param array $headers * @return array */ private function transformHeadersToServerVariables($headers) @@ -222,7 +237,7 @@ private function transformHeadersToServerVariables($headers) $server = []; foreach ($headers as $headerType => $headerValue) { - $headerType = 'HTTP_' . $headerType; + $headerType = 'HTTP_'.$headerType; $server[$headerType] = $headerValue; } diff --git a/src/Transformers/ApiTransformer.php b/src/Transformers/ApiTransformer.php index d692468..24b4c93 100644 --- a/src/Transformers/ApiTransformer.php +++ b/src/Transformers/ApiTransformer.php @@ -10,8 +10,7 @@ use Illuminate\Support\Str; /** - * Class ApiTransformer - * @package Napp\Core\Api\Transformers + * Class ApiTransformer. */ class ApiTransformer implements TransformerInterface { @@ -30,6 +29,7 @@ class ApiTransformer implements TransformerInterface /** * @param array|Model $apiMapping + * * @return void */ public function setApiMapping($apiMapping) @@ -45,6 +45,7 @@ public function setApiMapping($apiMapping) /** * @param array|Arrayable $data + * * @return array */ public function transformInput($data): array @@ -61,6 +62,7 @@ public function transformInput($data): array /** * @param array|Arrayable|LengthAwarePaginator|Paginator $data + * * @return array */ public function transformOutput($data): array @@ -73,7 +75,7 @@ public function transformOutput($data): array if (true === $data instanceof Collection) { $output = $this->transformCollection($output, $data); - } else if (true === $data instanceof Model) { + } elseif (true === $data instanceof Model) { $output = $this->transformAttributes($output, $data->attributesToArray()); $output = $this->transformRelationships($output, $data); } else { @@ -86,6 +88,7 @@ public function transformOutput($data): array /** * @param array $data + * * @return array */ public function transformOutputKeys(array $data): array @@ -98,10 +101,10 @@ public function transformOutputKeys(array $data): array return $output; } - /** * @param array $output * @param array $data + * * @return array */ protected function transformAttributes(array $output, array $data): array @@ -120,6 +123,7 @@ protected function transformAttributes(array $output, array $data): array /** * @param array $output * @param Model $data + * * @return array */ protected function transformRelationships(array $output, Model $data): array @@ -127,7 +131,7 @@ protected function transformRelationships(array $output, Model $data): array /** @var Model $data */ $relationships = $data->getRelations(); foreach ($relationships as $relationshipName => $relationship) { - if (true === $this->strict && ! $this->isMapped($relationshipName)) { + if (true === $this->strict && !$this->isMapped($relationshipName)) { continue; } @@ -161,6 +165,7 @@ protected function transformRelationships(array $output, Model $data): array /** * @param $data + * * @return array */ protected function transformPaginatedOutput($data): array @@ -168,14 +173,14 @@ protected function transformPaginatedOutput($data): array $items = $this->transformOutput($data->getCollection()); $output = [ - 'data' => $items, + 'data' => $items, 'pagination' => [ - 'currentPage' => $data->currentPage(), - 'perPage' => $data->perPage(), + 'currentPage' => $data->currentPage(), + 'perPage' => $data->perPage(), 'firstPageUrl' => $data->url(1), - 'nextPageUrl' => $data->nextPageUrl(), - 'prevPageUrl' => $data->previousPageUrl() - ] + 'nextPageUrl' => $data->nextPageUrl(), + 'prevPageUrl' => $data->previousPageUrl(), + ], ]; if (true === $data instanceof LengthAwarePaginator) { @@ -188,8 +193,9 @@ protected function transformPaginatedOutput($data): array } /** - * @param array $output + * @param array $output * @param Collection $data + * * @return array */ protected function transformCollection(array $output, Collection $data): array @@ -203,6 +209,7 @@ protected function transformCollection(array $output, Collection $data): array /** * @param string $newKey + * * @return string */ protected function findOriginalKey(string $newKey) @@ -218,6 +225,7 @@ protected function findOriginalKey(string $newKey) /** * @param string $originalKey + * * @return string */ protected function findNewKey(string $originalKey): string @@ -231,7 +239,8 @@ protected function findNewKey(string $originalKey): string /** * @param string $key - * @param mixed $value + * @param mixed $value + * * @return mixed */ protected function convertValueType(string $key, $value) @@ -247,7 +256,7 @@ protected function convertValueType(string $key, $value) if ('Nullable' === $method) { if (true === empty($value) && false === \is_numeric($value)) { - return null; + return; } continue; @@ -265,6 +274,7 @@ protected function convertValueType(string $key, $value) /** * @param $type + * * @return array */ protected static function parseStringDataType($type): array @@ -288,7 +298,8 @@ protected static function parseStringDataType($type): array /** * Parse a parameter list. * - * @param string $parameter + * @param string $parameter + * * @return array */ protected static function parseParameters($parameter): array @@ -298,6 +309,7 @@ protected static function parseParameters($parameter): array /** * @param $type + * * @return array */ protected static function parseManyDataTypes($type): array @@ -315,6 +327,7 @@ protected static function parseManyDataTypes($type): array /** * @param $type + * * @return array */ protected static function normalizeType($type): array @@ -330,6 +343,7 @@ protected static function normalizeType($type): array /** * @param $type + * * @return bool */ protected static function hasParameters($type): bool @@ -339,6 +353,7 @@ protected static function hasParameters($type): bool /** * @param $dataTypes + * * @return array */ protected static function normalizeNullable($dataTypes): array @@ -352,6 +367,7 @@ protected static function normalizeNullable($dataTypes): array /** * @param $type + * * @return string */ protected static function normalizeDataType($type): string @@ -370,6 +386,7 @@ protected static function normalizeDataType($type): string /** * @param $model + * * @return bool */ protected function isTransformAware($model): bool @@ -378,8 +395,10 @@ protected function isTransformAware($model): bool } /** - * Check if key is mapped with apiMapping + * Check if key is mapped with apiMapping. + * * @param $key + * * @return bool */ protected function isMapped($key): bool diff --git a/src/Transformers/NoneTransformer.php b/src/Transformers/NoneTransformer.php index 606fe81..16f3070 100644 --- a/src/Transformers/NoneTransformer.php +++ b/src/Transformers/NoneTransformer.php @@ -6,13 +6,13 @@ use Illuminate\Support\Arr; /** - * Class NoneTransformer - * @package Napp\Core\Api\Transformers + * Class NoneTransformer. */ class NoneTransformer implements TransformerInterface { /** * @param array|Arrayable $data + * * @return array */ public function transformInput($data): array @@ -26,6 +26,7 @@ public function transformInput($data): array /** * @param array|Arrayable $data + * * @return array */ public function transformOutput($data): array @@ -39,6 +40,7 @@ public function transformOutput($data): array /** * @param array $data + * * @return array */ public function transformOutputKeys(array $data): array diff --git a/src/Transformers/TransformerAware.php b/src/Transformers/TransformerAware.php index 0698a08..67bae75 100644 --- a/src/Transformers/TransformerAware.php +++ b/src/Transformers/TransformerAware.php @@ -3,8 +3,7 @@ namespace Napp\Core\Api\Transformers; /** - * Trait TransformerAware - * @package Napp\Core\Api\Transformers + * Trait TransformerAware. */ trait TransformerAware { diff --git a/src/Transformers/TransformerInterface.php b/src/Transformers/TransformerInterface.php index c27f152..9800070 100644 --- a/src/Transformers/TransformerInterface.php +++ b/src/Transformers/TransformerInterface.php @@ -5,25 +5,27 @@ use Illuminate\Contracts\Support\Arrayable; /** - * Interface TransformerInterface - * @package Napp\Core\Api\Transformers + * Interface TransformerInterface. */ interface TransformerInterface { /** * @param array|Arrayable $data + * * @return array */ public function transformInput($data): array; /** * @param array|Arrayable $data + * * @return array */ public function transformOutput($data): array; /** * @param array $data + * * @return array */ public function transformOutputKeys(array $data): array; diff --git a/src/Transformers/TransformerMethods.php b/src/Transformers/TransformerMethods.php index 970bbc9..ce6c6bf 100644 --- a/src/Transformers/TransformerMethods.php +++ b/src/Transformers/TransformerMethods.php @@ -3,13 +3,13 @@ namespace Napp\Core\Api\Transformers; /** - * Class TransformerMethods - * @package Napp\Core\Api\Transformers + * Class TransformerMethods. */ class TransformerMethods { /** * @param $value + * * @return mixed */ public static function convertJson($value) @@ -19,6 +19,7 @@ public static function convertJson($value) /** * @param $value + * * @return int */ public static function convertInteger($value): int @@ -28,6 +29,7 @@ public static function convertInteger($value): int /** * @param $value + * * @return string */ public static function convertString($value): string @@ -38,6 +40,7 @@ public static function convertString($value): string /** * @param $value * @param $parameters + * * @return float */ public static function convertFloat($value, $parameters): float @@ -51,6 +54,7 @@ public static function convertFloat($value, $parameters): float /** * @param $value + * * @return bool */ public static function convertBoolean($value): bool @@ -60,6 +64,7 @@ public static function convertBoolean($value): bool /** * @param $value + * * @return array */ public static function convertArray($value): array @@ -73,6 +78,7 @@ public static function convertArray($value): array /** * @param $value + * * @return mixed|object */ public static function convertObject($value) @@ -86,6 +92,7 @@ public static function convertObject($value) /** * @param $value + * * @return string */ public static function convertDatetime($value): string diff --git a/src/Validation/ValidateTrait.php b/src/Validation/ValidateTrait.php index 607e71e..490e29d 100644 --- a/src/Validation/ValidateTrait.php +++ b/src/Validation/ValidateTrait.php @@ -7,16 +7,17 @@ use Napp\Core\Api\Exceptions\Exceptions\ValidationException; /** - * Trait ValidateTrait - * @package Napp\Core\Api\Validation + * Trait ValidateTrait. */ trait ValidateTrait { /** * @param array $attributes * @param array $rules - * @return void + * * @throws ValidationException + * + * @return void */ public static function validate(array $attributes, array $rules) { @@ -25,8 +26,8 @@ public static function validate(array $attributes, array $rules) if (true === $validator->fails()) { $message = $validator->messages()->first(); - $exception = new ValidationException; - $exception->statusMessage = $exception->statusMessage . ': ' . $message; + $exception = new ValidationException(); + $exception->statusMessage = $exception->statusMessage.': '.$message; $exception->validation = $validator->messages(); throw $exception; diff --git a/tests/ApiPaginatedTransformerTest.php b/tests/ApiPaginatedTransformerTest.php index 11c1a73..4638258 100644 --- a/tests/ApiPaginatedTransformerTest.php +++ b/tests/ApiPaginatedTransformerTest.php @@ -6,9 +6,9 @@ use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\Paginator; use Napp\Core\Api\Tests\Models\Category; +use Napp\Core\Api\Tests\TestCase; use Napp\Core\Api\Tests\Transformers\CategoryStrictTransformer; use Napp\Core\Api\Transformers\ApiTransformer; -use Napp\Core\Api\Tests\TestCase; class ApiPaginatedTransformerTest extends TestCase { @@ -45,10 +45,10 @@ public function test_transform_paginated_output_data() for ($x = 1; $x <= 10; $x++) { $input[] = [ - 'id' => $x, + 'id' => $x, 'first_name' => $this->faker->firstName, - 'last_name' => $this->faker->lastName, - 'age' => $this->faker->numberBetween(20, 80), + 'last_name' => $this->faker->lastName, + 'age' => $this->faker->numberBetween(20, 80), ]; } @@ -78,10 +78,10 @@ public function test_transform_length_aware_paginated_output_data() for ($x = 1; $x <= 10; $x++) { $input[] = [ - 'id' => $x, + 'id' => $x, 'first_name' => $this->faker->firstName, - 'last_name' => $this->faker->lastName, - 'age' => $this->faker->numberBetween(20, 80), + 'last_name' => $this->faker->lastName, + 'age' => $this->faker->numberBetween(20, 80), ]; } @@ -107,6 +107,7 @@ public function test_transform_length_aware_paginated_output_data() $this->assertArrayHasKey('age', $item); } } + /** @group me */ public function test_transform_length_aware_paginated_with_relationships() { @@ -178,5 +179,4 @@ public function test_transform_length_aware_paginated_with_relationships_with_st $this->assertEquals(1, $transformedOutput['data'][0]['id']); $this->assertEquals(2, $transformedOutput['data'][1]['id']); } - -} \ No newline at end of file +} diff --git a/tests/ApiRequestTest.php b/tests/ApiRequestTest.php index 6c48fe4..fd34c31 100644 --- a/tests/ApiRequestTest.php +++ b/tests/ApiRequestTest.php @@ -33,6 +33,7 @@ public function test_required_field() // Laravel 5.6 changed the method validate method on the FormRequest to validateResolved. if (true === version_compare($this->app->version(), '5.6', '>=')) { $this->request->validateResolved(); + return; } @@ -55,9 +56,10 @@ public function test_field_with_correct_validation_object() try { $this->request->validateResolved(); - } catch(ValidationException $exception) { + } catch (ValidationException $exception) { $this->assertArrayHasKey('number', $exception->validation); $this->assertEquals('The number must be an integer.', $exception->validation['number'][0]); + return; } @@ -70,10 +72,11 @@ public function test_field_with_required_validation_object_multiple() try { $this->request->validateResolved(); - } catch(ValidationException $exception) { + } catch (ValidationException $exception) { $this->assertCount(2, $exception->validation); $this->assertArrayHasKey('email', $exception->validation); $this->assertEquals('The email field is required.', $exception->validation['email'][0]); + return; } @@ -90,6 +93,7 @@ public function test_invalid_field() // Laravel 5.6 changed the method validate method on the FormRequest to validateResolved. if (true === version_compare($this->app->version(), '5.6', '>=')) { $this->request->validateResolved(); + return; } @@ -99,7 +103,7 @@ public function test_invalid_field() /** * Get package providers. * - * @param \Illuminate\Foundation\Application $app + * @param \Illuminate\Foundation\Application $app * * @return array */ @@ -109,4 +113,4 @@ protected function getPackageProviders($app) RequestServiceProvider::class, ]; } -} \ No newline at end of file +} diff --git a/tests/ApiTransformerTest.php b/tests/ApiTransformerTest.php index 149809a..6923cb2 100644 --- a/tests/ApiTransformerTest.php +++ b/tests/ApiTransformerTest.php @@ -5,12 +5,12 @@ use Napp\Core\Api\Tests\Models\Category; use Napp\Core\Api\Tests\Models\Post; use Napp\Core\Api\Tests\Models\Product; +use Napp\Core\Api\Tests\TestCase; use Napp\Core\Api\Tests\Transformers\CategoryStrictTransformer; use Napp\Core\Api\Tests\Transformers\CategoryTransformerWithDifferentOutputKey; use Napp\Core\Api\Tests\Transformers\PostTransformer; use Napp\Core\Api\Tests\Transformers\ProductTransformer; use Napp\Core\Api\Transformers\ApiTransformer; -use Napp\Core\Api\Tests\TestCase; class ApiTransformerTest extends TestCase { @@ -24,11 +24,11 @@ public function setUp(): void parent::setUp(); $apiMapping = [ - 'id' => ['newName' => 'id', 'dataType' => 'int'], - 'name' => ['newName' => 'companyName', 'dataType' => 'string'], + 'id' => ['newName' => 'id', 'dataType' => 'int'], + 'name' => ['newName' => 'companyName', 'dataType' => 'string'], 'has_access' => ['newName' => 'hasAccess', 'dataType' => 'bool'], 'categories' => ['newName' => 'cats', 'dataType' => 'array'], - 'price' => ['newName' => 'price', 'dataType' => 'float'] + 'price' => ['newName' => 'price', 'dataType' => 'float'], ]; $this->transformer = new ApiTransformer(); @@ -38,21 +38,21 @@ public function setUp(): void public function test_input_transforming() { $input = [ - 'id' => 1, - 'companyName' => 'Wayne Industries', - 'hasAccess' => 0, + 'id' => 1, + 'companyName' => 'Wayne Industries', + 'hasAccess' => 0, 'someAdditionalField' => 'someAdditionalValue', - 'cats' => ['foo' => 'bar'], - 'price' => '1000' + 'cats' => ['foo' => 'bar'], + 'price' => '1000', ]; $expected = [ - 'id' => 1, - 'name' => 'Wayne Industries', - 'has_access' => 0, + 'id' => 1, + 'name' => 'Wayne Industries', + 'has_access' => 0, 'someAdditionalField' => 'someAdditionalValue', - 'categories' => ['foo' => 'bar'], - 'price' => (float) 1000, + 'categories' => ['foo' => 'bar'], + 'price' => (float) 1000, ]; $transformedInput = $this->transformer->transformInput($input); @@ -66,16 +66,16 @@ public function test_strict_output_transforming() $reflection->setValue($this->transformer, true); $output = [ - 'id' => 1, - 'name' => 'Wayne Industries', - 'has_access' => 0, - 'some_additional_field' => 'some_additional_value' + 'id' => 1, + 'name' => 'Wayne Industries', + 'has_access' => 0, + 'some_additional_field' => 'some_additional_value', ]; $expectedOutput = [ - 'id' => 1, + 'id' => 1, 'companyName' => 'Wayne Industries', - 'hasAccess' => false, + 'hasAccess' => false, ]; $transformedOutput = $this->transformer->transformOutput($output); @@ -86,19 +86,19 @@ public function test_strict_output_transforming() public function test_output_transforming() { $output = [ - 'id' => 1, - 'name' => 'Wayne Industries', - 'has_access' => 0, + 'id' => 1, + 'name' => 'Wayne Industries', + 'has_access' => 0, 'some_additional_field' => 'some_additional_value', - 'price' => '1000' + 'price' => '1000', ]; $expectedOutput = [ - 'id' => 1, - 'companyName' => 'Wayne Industries', - 'hasAccess' => false, + 'id' => 1, + 'companyName' => 'Wayne Industries', + 'hasAccess' => false, 'some_additional_field' => 'some_additional_value', - 'price' => (float) 1000 + 'price' => (float) 1000, ]; $transformedOutput = $this->transformer->transformOutput($output); @@ -110,32 +110,32 @@ public function test_output_transforming_with_collection() { $output = collect(); $output->push([ - 'id' => 1, - 'name' => 'Wayne Industries', - 'has_access' => 0, - 'some_additional_field' => 'some_additional_value' + 'id' => 1, + 'name' => 'Wayne Industries', + 'has_access' => 0, + 'some_additional_field' => 'some_additional_value', ]); $output->push([ - 'id' => 2, - 'name' => 'LexCorp', - 'has_access' => 1, - 'some_additional_field' => 'some_additional_value' + 'id' => 2, + 'name' => 'LexCorp', + 'has_access' => 1, + 'some_additional_field' => 'some_additional_value', ]); $expectedOutput = [ [ - 'id' => 1, - 'companyName' => 'Wayne Industries', - 'hasAccess' => false, - 'some_additional_field' => 'some_additional_value' + 'id' => 1, + 'companyName' => 'Wayne Industries', + 'hasAccess' => false, + 'some_additional_field' => 'some_additional_value', ], [ - 'id' => 2, - 'companyName' => 'LexCorp', - 'hasAccess' => true, - 'some_additional_field' => 'some_additional_value' - ] + 'id' => 2, + 'companyName' => 'LexCorp', + 'hasAccess' => true, + 'some_additional_field' => 'some_additional_value', + ], ]; $transformedOutput = $this->transformer->transformOutput($output); @@ -151,30 +151,30 @@ public function test_output_transforming_with_collection_strict_mode() $output = collect(); $output->push([ - 'id' => 1, - 'name' => 'Wayne Industries', - 'has_access' => 0, - 'some_additional_field' => 'some_additional_value' + 'id' => 1, + 'name' => 'Wayne Industries', + 'has_access' => 0, + 'some_additional_field' => 'some_additional_value', ]); $output->push([ - 'id' => 2, - 'name' => 'LexCorp', - 'has_access' => 1, - 'some_additional_field' => 'some_additional_value' + 'id' => 2, + 'name' => 'LexCorp', + 'has_access' => 1, + 'some_additional_field' => 'some_additional_value', ]); $expectedOutput = [ [ - 'id' => 1, + 'id' => 1, 'companyName' => 'Wayne Industries', - 'hasAccess' => false, + 'hasAccess' => false, ], [ - 'id' => 2, + 'id' => 2, 'companyName' => 'LexCorp', - 'hasAccess' => true, - ] + 'hasAccess' => true, + ], ]; $transformedOutput = $this->transformer->transformOutput($output); @@ -185,39 +185,39 @@ public function test_output_transforming_with_collection_strict_mode() public function test_the_datatype_is_nullable() { $this->transformer->setApiMapping([ - 'price' => ['newName' => 'price_new', 'dataType' => 'nullable|int'] + 'price' => ['newName' => 'price_new', 'dataType' => 'nullable|int'], ]); $input = [ - 'price' => '100' + 'price' => '100', ]; $expectedOutput = [ - 'price_new' => 100 + 'price_new' => 100, ]; $this->assertSame($expectedOutput, $this->transformer->transformOutput($input)); $input = [ - 'price' => 0 + 'price' => 0, ]; $expectedOutput = [ - 'price_new' => 0 + 'price_new' => 0, ]; $this->assertSame($expectedOutput, $this->transformer->transformOutput($input)); $this->transformer->setApiMapping([ - 'description' => ['newName' => 'description', 'dataType' => 'array|nullable'] + 'description' => ['newName' => 'description', 'dataType' => 'array|nullable'], ]); $input = [ - 'description' => [] + 'description' => [], ]; $expectedOutput = [ - 'description' => null + 'description' => null, ]; $this->assertSame($expectedOutput, $this->transformer->transformOutput($input)); @@ -226,15 +226,15 @@ public function test_the_datatype_is_nullable() public function test_arguments_can_be_passed_to_the_datatype() { $this->transformer->setApiMapping([ - 'price' => ['newName' => 'price', 'dataType' => 'float:2'] + 'price' => ['newName' => 'price', 'dataType' => 'float:2'], ]); $input = [ - 'price' => '100.5542' + 'price' => '100.5542', ]; $expectedOutput = [ - 'price' => 100.55 + 'price' => 100.55, ]; $this->assertSame($expectedOutput, $this->transformer->transformOutput($input)); @@ -299,16 +299,16 @@ public function test_transform_deeply_nested_relationships() $this->assertEquals('iPhone 8', $result['products'][0]['variants'][0]['title']); } - + public function test_transform_not_strict_model() { $post = Post::create([ - 'title' => 'My First post', - 'desc' => 'body text', - 'tags' => [2, '222', 'wow'], + 'title' => 'My First post', + 'desc' => 'body text', + 'tags' => [2, '222', 'wow'], 'other_tags' => null, - 'owner' => 34, - 'uuid' => '66220588-c944-3425-a3ea-0fc80f8c32fe' + 'owner' => 34, + 'uuid' => '66220588-c944-3425-a3ea-0fc80f8c32fe', ]); $result = app(PostTransformer::class)->transformOutput($post); @@ -324,7 +324,7 @@ public function test_transform_model_relations_is_exluded_if_not_found_in_transf $category->products()->create(['name' => 'iPhone', 'price'=> 100.0]); $category->load('products'); - $result = (new CategoryStrictTransformer)->transformOutput($category); + $result = (new CategoryStrictTransformer())->transformOutput($category); $this->assertArrayNotHasKey('products', $result); } @@ -336,7 +336,7 @@ public function test_transform_model_relations_with_different_output_key() $category->products()->create(['name' => 'iPhone', 'price'=> 100.0]); $category->load('products'); - $result = (new CategoryTransformerWithDifferentOutputKey)->transformOutput($category); + $result = (new CategoryTransformerWithDifferentOutputKey())->transformOutput($category); $this->assertArrayNotHasKey('products', $result); $this->assertArrayHasKey('indexes', $result); diff --git a/tests/Models/Post.php b/tests/Models/Post.php index 0454226..ad0c9be 100644 --- a/tests/Models/Post.php +++ b/tests/Models/Post.php @@ -6,9 +6,8 @@ class Post extends Model { - protected $guarded = [ - 'uuid' + 'uuid', ]; /** @@ -22,7 +21,7 @@ class Post extends Model * @var array */ public $casts = [ - 'tags' => 'json' + 'tags' => 'json', ]; /** @@ -44,6 +43,4 @@ class Post extends Model 'owner' => ['newName' => 'owner_id', 'dataType' => 'nullable[int'], 'created_at' => ['newName' => 'postedAt', 'dataType' => 'datetime'], ]; - - } diff --git a/tests/TestCase.php b/tests/TestCase.php index 039c25d..9539c3d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -40,7 +40,8 @@ public function setUpTestDatabases() /** * Define environment setup. * - * @param \Illuminate\Foundation\Application $app + * @param \Illuminate\Foundation\Application $app + * * @return void */ protected function getEnvironmentSetUp($app) @@ -64,30 +65,31 @@ protected function getEnvironmentSetUp($app) // sqlite $app['config']->set('database.connections.testing', [ - 'driver' => 'sqlite', + 'driver' => 'sqlite', 'database' => ':memory:', - 'prefix' => '', + 'prefix' => '', ]); $app['config']->set('database.default', 'testing'); } /** - * Loading package service provider + * Loading package service provider. * * @param \Illuminate\Foundation\Application $app + * * @return array */ protected function getPackageProviders($app) { return [ RequestServiceProvider::class, - RouterServiceProvider::class + RouterServiceProvider::class, ]; } public function migrateTables() { - if ( ! Schema::hasTable('categories')) { + if (!Schema::hasTable('categories')) { Schema::create('categories', function (Blueprint $table) { $table->increments('id'); $table->string('title'); @@ -95,7 +97,7 @@ public function migrateTables() }); } - if ( ! Schema::hasTable('products')) { + if (!Schema::hasTable('products')) { Schema::create('products', function (Blueprint $table) { $table->increments('id'); $table->string('name'); @@ -105,7 +107,7 @@ public function migrateTables() }); } - if ( ! Schema::hasTable('variants')) { + if (!Schema::hasTable('variants')) { Schema::create('variants', function (Blueprint $table) { $table->increments('id'); $table->string('name'); @@ -115,7 +117,7 @@ public function migrateTables() }); } - if ( ! Schema::hasTable('posts')) { + if (!Schema::hasTable('posts')) { Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string('title'); @@ -126,12 +128,11 @@ public function migrateTables() $table->uuid('uuid')->nullable(); $table->timestamps(); }); - } } /** - * Drop all tables to start the test with fresh data + * Drop all tables to start the test with fresh data. */ public function dropAllTables() { @@ -145,4 +146,4 @@ public function dropAllTables() }); Schema::enableForeignKeyConstraints(); } -} \ No newline at end of file +} diff --git a/tests/Transformers/CategoryTransformer.php b/tests/Transformers/CategoryTransformer.php index 4a3f8f4..8c712d6 100644 --- a/tests/Transformers/CategoryTransformer.php +++ b/tests/Transformers/CategoryTransformer.php @@ -1,4 +1,4 @@ -setApiMapping($category); } -} \ No newline at end of file +} diff --git a/tests/Transformers/PostTransformer.php b/tests/Transformers/PostTransformer.php index 70a9c5c..bd4efb2 100644 --- a/tests/Transformers/PostTransformer.php +++ b/tests/Transformers/PostTransformer.php @@ -1,4 +1,4 @@ -setApiMapping($post); } -} \ No newline at end of file +} diff --git a/tests/Transformers/ProductTransformer.php b/tests/Transformers/ProductTransformer.php index 9e6e1cf..f54d1e1 100644 --- a/tests/Transformers/ProductTransformer.php +++ b/tests/Transformers/ProductTransformer.php @@ -1,4 +1,4 @@ -setApiMapping($product); } -} \ No newline at end of file +} diff --git a/tests/Transformers/VariantTransformer.php b/tests/Transformers/VariantTransformer.php index d97a262..936df9f 100644 --- a/tests/Transformers/VariantTransformer.php +++ b/tests/Transformers/VariantTransformer.php @@ -1,4 +1,4 @@ -setApiMapping($variant); } -} \ No newline at end of file +} diff --git a/tests/stubs/ApiRequestStub.php b/tests/stubs/ApiRequestStub.php index f5d4be9..274406d 100644 --- a/tests/stubs/ApiRequestStub.php +++ b/tests/stubs/ApiRequestStub.php @@ -20,6 +20,7 @@ class ApiRequestStub extends ApiRequest /** * @param array $rules + * * @return void */ public function setRules(array $rules) @@ -29,6 +30,7 @@ public function setRules(array $rules) /** * @param array $data + * * @return void */ public function setData(array $data)