Skip to content

Commit

Permalink
Added function additionalData to add additional_data parameter in r…
Browse files Browse the repository at this point in the history
…esponse (usage mostly with collection)
  • Loading branch information
adrianzofcin committed Mar 8, 2023
1 parent c226562 commit 237bc86
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/Helpers/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class ApiResponse
*/
private static mixed $data = null;

/**
* @var mixed|null
*/
private static mixed $additionalData = null;

/**
* @var mixed|null
*/
Expand Down Expand Up @@ -99,6 +104,19 @@ public static function message(string $message): static
return new static;
}

/**
* Meta Data
*
* @param mixed $message
* @return static
*/
public static function additionalData(mixed $additionalData): static
{
static::$additionalData = $additionalData;

return new static;
}

/**
* Response :D
*
Expand All @@ -108,7 +126,6 @@ public static function message(string $message): static
public static function response(int $statusCode = 200): \Illuminate\Http\JsonResponse
{
if ($statusCode === 0) $statusCode = 500;
$message = null;
if (self::$message) $message = self::$message;
else $message = self::$code ? __(self::$codePrefix .'.' . self::$code) : null;

Expand All @@ -118,13 +135,15 @@ public static function response(int $statusCode = 200): \Illuminate\Http\JsonRes
$response = $response->merge([
'code' => self::$code,
'errors' => self::$errors,
'additional_data' => self::$additionalData,
'message' => $message,
]);
} else {
$response = [
'data' => self::$data,
'code' => self::$code,
'errors' => self::$errors,
'additional_data' => self::$additionalData,
'message' => $message,
];
}
Expand All @@ -133,6 +152,7 @@ public static function response(int $statusCode = 200): \Illuminate\Http\JsonRes
'data' => self::$data,
'code' => self::$code,
'errors' => self::$errors,
'additional_data' => self::$additionalData,
'message' => $message,
];
}
Expand Down

0 comments on commit 237bc86

Please sign in to comment.