Skip to content

Commit

Permalink
add factory methods for ApiResult
Browse files Browse the repository at this point in the history
  • Loading branch information
z985342160 committed Aug 18, 2024
1 parent d8c8645 commit 57cdd37
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/ApiResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,39 @@ class ApiResult

private ?Throwable $exception = null;

public static function success(array $data = [], string $message = 'ok'): static
{
$result = new static();

$result->setSuccess(true)
->setData($data)
->setMessage($message)
;

return $result;
}

public static function fail(string $message, ?array $data = null): static
{
$result = new static();

$result->setSuccess(false)
->setData($data)
->setMessage($message)
;

return $result;
}

public static function fromException(Throwable $e): static
{
$result = static::fail($e->getMessage());

$result->setException($e);

return $result;
}

/**
* @return bool
*/
Expand Down

0 comments on commit 57cdd37

Please sign in to comment.