-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: gitauto-ai[bot] <161652217+gitauto-ai[bot]@users.noreply.github.com>
- Loading branch information
1 parent
76eb40f
commit df87489
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace GuiBranco\Pancake; | ||
|
||
class Response | ||
{ | ||
private bool $success; | ||
private $data; | ||
private string $message; | ||
private int $statusCode; | ||
|
||
private function __construct(bool $success, $data, string $message, int $statusCode) | ||
{ | ||
$this->success = $success; | ||
$this->data = $data; | ||
$this->message = $message; | ||
$this->statusCode = $statusCode; | ||
} | ||
|
||
public static function success($data, string $message = '', int $statusCode = 200): self | ||
{ | ||
return new self(true, $data, $message, $statusCode); | ||
} | ||
|
||
public static function error(string $message, int $statusCode = 400, $data = null): self | ||
{ | ||
return new self(false, $data, $message, $statusCode); | ||
} | ||
|
||
public function isSuccess(): bool | ||
{ | ||
return $this->success; | ||
} | ||
|
||
public function getData() | ||
{ | ||
return $this->data; | ||
} | ||
|
||
public function getMessage(): string | ||
{ | ||
return $this->message; | ||
} | ||
|
||
public function getStatusCode(): int | ||
{ | ||
return $this->statusCode; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'success' => $this->success, | ||
'data' => $this->data, | ||
'message' => $this->message, | ||
'statusCode' => $this->statusCode, | ||
]; | ||
} | ||
|
||
public function toJson(): string | ||
{ | ||
return json_encode($this->toArray()); | ||
} | ||
} |