* feat: integrate PHPStan, Rector and Pint code quality tools - Add larastan, rector, and rector-laravel dev dependencies - Configure PHPStan at max level with Larastan extension - Configure Rector with Laravel sets and code quality rules - Configure Pint with strict rules (final_class, strict_types) - Add composer scripts: lint, test:lint, test:types, test:unit - Add GitHub Actions workflow for CI on push/PR - Apply code style fixes across all files * docs: add code quality section to README
79 lines
2.0 KiB
PHP
79 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Traits;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
trait ApiResponse
|
|
{
|
|
protected function success(
|
|
mixed $data = null,
|
|
string $message = 'Success',
|
|
int $code = Response::HTTP_OK
|
|
): JsonResponse {
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => $message,
|
|
'data' => $data,
|
|
], $code);
|
|
}
|
|
|
|
protected function created(
|
|
mixed $data = null,
|
|
string $message = 'Resource created successfully'
|
|
): JsonResponse {
|
|
return $this->success($data, $message, Response::HTTP_CREATED);
|
|
}
|
|
|
|
protected function noContent(): JsonResponse
|
|
{
|
|
return response()->json(null, Response::HTTP_NO_CONTENT);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $errors
|
|
*/
|
|
protected function error(
|
|
string $message = 'Error',
|
|
int $code = Response::HTTP_BAD_REQUEST,
|
|
array $errors = []
|
|
): JsonResponse {
|
|
$response = [
|
|
'success' => false,
|
|
'message' => $message,
|
|
];
|
|
|
|
if ($errors !== []) {
|
|
$response['errors'] = $errors;
|
|
}
|
|
|
|
return response()->json($response, $code);
|
|
}
|
|
|
|
protected function notFound(string $message = 'Resource not found'): JsonResponse
|
|
{
|
|
return $this->error($message, Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
protected function unauthorized(string $message = 'Unauthorized'): JsonResponse
|
|
{
|
|
return $this->error($message, Response::HTTP_UNAUTHORIZED);
|
|
}
|
|
|
|
protected function forbidden(string $message = 'Forbidden'): JsonResponse
|
|
{
|
|
return $this->error($message, Response::HTTP_FORBIDDEN);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $errors
|
|
*/
|
|
protected function validationError(array $errors, string $message = 'Validation failed'): JsonResponse
|
|
{
|
|
return $this->error($message, Response::HTTP_UNPROCESSABLE_ENTITY, $errors);
|
|
}
|
|
}
|