Skip to content

Commit

Permalink
Update src/Response.php (#281)
Browse files Browse the repository at this point in the history
Co-authored-by: gitauto-ai[bot] <161652217+gitauto-ai[bot]@users.noreply.github.com>
  • Loading branch information
gitauto-ai[bot] authored Dec 12, 2024
1 parent 76eb40f commit df87489
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/Response.php
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());
}
}

0 comments on commit df87489

Please sign in to comment.