Skip to content

Instantly share code, notes, and snippets.

@saeedvir
Last active July 17, 2020 20:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save saeedvir/4287f1ea5db2fb5eb6530fb95bdb8d0d to your computer and use it in GitHub Desktop.
Save saeedvir/4287f1ea5db2fb5eb6530fb95bdb8d0d to your computer and use it in GitHub Desktop.
Standard API Response in PHP
<?php
/**
* Standard API Response in PHP
*
* https://github.com/obiefy/api-response
*
* @author Saeedvir [
* 'email' => 'saeed.es91@gmail.com',
* 'telegram' => 'https://t.me/PhpWebDeveloper'
* ]
*
* @usage :
* $api_response = new ApiDataResponse;
* $apiResponse->setLogger(true,function($json_data,$time_stamp){
file_put_contents('log.txt', var_export($json_data, true)."\r\n".$time_stamp."\r\n" , FILE_APPEND);
});
$data = ['user'=>['username','email@domain.com']]
return $apiResponse->response(200,'data_success',$data);
return $apiResponse->success('data_success',$data);
*
* */
class ApiDataResponse
{
protected $constant_data;
protected $statusLabel;
protected $messageLabel;
protected $dataLabel;
protected $constantDataLabel = '_DATA';
protected $enableLogger = false;
protected $loggerFunction = false; #function($json_data,$timestamps=time()){}
/**
* @param $constant_data
* @param $statusLabel
* @param $messageLabel
* @param $dataLabel
*/
public function __construct($constant_data = null, $statusLabel = 'status', $messageLabel = 'message', $dataLabel = 'data')
{
$this->constant_data = $constant_data;
$this->statusLabel = $statusLabel;
$this->messageLabel = $messageLabel;
$this->dataLabel = $dataLabel;
}
/**
* @param $status
* @param $message
* @param $data
* @param array $extraData
*
* @return JsonResponse
*/
public function response($status, $message, $data, ...$extraData)
{
//Init Json
$json = [
$this->statusLabel => ($status !== null) ? strval($status) : 200,
$this->messageLabel => $message,
$this->dataLabel => $data,
];
//Add Constant_Data
if ($this->constant_data) {
$json[$this->constantDataLabel] = $this->constant_data;
}
//Merge Extra_Data
if ($extraData) {
foreach ($extraData as $extra) {
$json = array_merge($json, $extra);
}
}
//Logger
if ($this->enableLogger && is_callable($this->loggerFunction)) {
call_user_func($this->loggerFunction, $json, time());
}
return ($status !== null) ? response()->json($json, $status) : response()->json($json);
}
/**
* @param string $message
* @param array $data
* @param array $extraData
*
* @return JsonResponse
*/
public function success($message = '', $data = [], ...$extraData)
{
return $this->response(200, $message, $data, ...$extraData);
}
/**
* @param string $message
*
* @return JsonResponse
*/
public function error($message = '', $data = [])
{
return $this->response(404, $message, $data);
}
/**
* @param $enableLogger
* @param $loggerFunction
*
* @return (Bollean) Logger Status
*/
public function setLogger($enableLogger = true, $loggerFunction = false)
{
$this->enableLogger = boolval($enableLogger);
if ($enableLogger && is_callable($loggerFunction)) {
$this->loggerFunction = $loggerFunction;
return true;
} else {
$this->loggerFunction = false;
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment