diff --git a/composer.json b/composer.json index 6064aac..deed55c 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,7 @@ { "name": "wamesk/laravel-api-response", "description": "Laravel api response helper.", + "version": "1.2", "keywords": [ "helper", "laravel", diff --git a/src/Helpers/ApiResponse.php b/src/Helpers/ApiResponse.php index 4bc20c3..3474a8a 100644 --- a/src/Helpers/ApiResponse.php +++ b/src/Helpers/ApiResponse.php @@ -1,15 +1,17 @@ toResponse(app('request'))->getData(); - else static::$data = $pagination->toArray(); + public static function collection( + LengthAwarePaginator $pagination, + $resource = null, + ): static { + if ($resource) { + static::$data = (array) ($resource::collection($pagination))->toResponse(app(abstract: 'request'))->getData(); + } else { + static::$data = $pagination->toArray(); + } - return new static; + return new static(); } /** * Response Message - * - * @param string $message - * @return static */ - public static function message(string $message): static - { + public static function message( + string $message, + ): static { static::$message = $message; - return new static; + return new static(); } /** * Meta Data - * - * @param mixed $message - * @return static */ - public static function additionalData(mixed $additionalData): static - { + public static function additionalData( + mixed $additionalData, + ): static { static::$additionalData = $additionalData; - return new static; + return new static(); } /** * Response :D - * - * @param int $statusCode - * @return \Illuminate\Http\JsonResponse */ - public static function response(int $statusCode = 200): \Illuminate\Http\JsonResponse - { - if ($statusCode === 0) $statusCode = 500; - if (self::$message) $message = self::$message; - else $message = self::$code ? __(self::$codePrefix .'.' . self::$code) : null; - - if (gettype(self::$data) === 'array') { - if (key_exists('data', self::$data)) { - $response = collect(self::$data); - $response = $response->merge([ + public static function response( + int $statusCode = 200, + ): JsonResponse { + if (0 === $statusCode) { + $statusCode = 500; + } + if (self::$message) { + $message = self::$message; + } else { + $message = self::$code ? __(key: self::$codePrefix . '.' . self::$code) : null; + } + + if ('array' === gettype(value: self::$data)) { + if (array_key_exists(key: 'data', array: self::$data)) { + $response = collect(value: self::$data); + $response = $response->merge(items: [ 'code' => self::$code, 'errors' => self::$errors, 'additional_data' => self::$additionalData, @@ -157,15 +150,27 @@ public static function response(int $statusCode = 200): \Illuminate\Http\JsonRes ]; } - return response()->json($response)->setStatusCode($statusCode); + return response()->json(data: $response)->setStatusCode(code: $statusCode); } - public static function exception(\Exception $exception): \Illuminate\Http\JsonResponse - { - if (env('APP_DEBUG')) dd($exception); + /** + * Handles exception + */ + public static function exception( + Exception $exception, + ): JsonResponse { + if (env('APP_DEBUG')) { + dd($exception); + } + + Log::build([ + 'driver' => 'single', + 'path' => storage_path(path: 'logs/errors/' . date(format: 'Y-m-d') . '.log'), + ]) + ->error(message: $exception); - self::$message = __('wamesk-api-response.exception-message', ['file' => $exception->getFile(), 'line' => $exception->getLine()]); + self::$message = __(key: 'wamesk-api-response.server-error'); - return self::response(500); + return self::response(statusCode: 500); } } diff --git a/src/resources/lang/en/wamesk-api-response.php b/src/resources/lang/en/wamesk-api-response.php index 3ddb7ab..2114080 100644 --- a/src/resources/lang/en/wamesk-api-response.php +++ b/src/resources/lang/en/wamesk-api-response.php @@ -1,5 +1,7 @@ "Exception found in file :file on line :line", + 'server-error' => 'Server error', ]; diff --git a/src/resources/lang/sk/wamesk-api-response.php b/src/resources/lang/sk/wamesk-api-response.php index 5ede2f0..fdf134a 100644 --- a/src/resources/lang/sk/wamesk-api-response.php +++ b/src/resources/lang/sk/wamesk-api-response.php @@ -1,5 +1,7 @@ "Chyba nájdená v súbore :file na riadku :line", + 'server-error' => 'Serverová chyba', ];