You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
799 B

2 years ago
  1. <?php
  2. namespace App\Http\Resources;
  3. use \Illuminate\Http\Response;
  4. use Illuminate\Contracts\Support\Responsable;
  5. class Failure implements Responsable
  6. {
  7. public function __construct(
  8. public ?int $code = null,
  9. public ?string $status = null,
  10. ){}
  11. public static function make()
  12. {
  13. return new static;
  14. }
  15. public function status(int $code, string $status)
  16. {
  17. $this->code = $code;
  18. $this->status = $status;
  19. return $this;
  20. }
  21. public function toResponse($request)
  22. {
  23. return response()->json([
  24. 'data' => [
  25. 'code' => $code = $this->code ?? Response::HTTP_INTERNAL_SERVER_ERROR,
  26. 'message' => $this->status ?? Response::$statusTexts[$code],
  27. ]
  28. ]);
  29. }
  30. }