385 lines
11 KiB
PHP
385 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: Earwin Delos Santos
|
|
* Date: 12 Nov 2017
|
|
* Time: 7:49 PM
|
|
*/
|
|
|
|
namespace App\Helpers;
|
|
|
|
|
|
use App\Contracts\HttpStatusCodeInterface;
|
|
use Illuminate\Support\Facades\Response;
|
|
|
|
class HttpStatusCode implements HttpStatusCodeInterface
|
|
{
|
|
/**
|
|
* Standard response for successful HTTP requests. The actual response will depend on the request method used.
|
|
* In a GET request, the response will contain an entity corresponding to the requested resource.
|
|
* In a POST request, the response will contain an entity describing or containing the result of the action.[7]
|
|
*
|
|
* @param string $message
|
|
* @param array $content
|
|
* @return Response object
|
|
*/
|
|
public function success($message="SUCCESS",$content=array(), $withJson = true)
|
|
{
|
|
$return = [
|
|
'code'=>200,
|
|
'message'=>$message,
|
|
'data' => $content
|
|
];
|
|
|
|
if($withJson)
|
|
return Response::json($return,$return['code']);
|
|
else
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* The request has been fulfilled, resulting in the creation of a new resource
|
|
*
|
|
* @param string $message
|
|
* @param array $content
|
|
* @return Response object
|
|
*/
|
|
public function created($message="CREATED",$content=array(),$withJson = true)
|
|
{
|
|
$return = [
|
|
'code'=>201,
|
|
'message'=>$message,
|
|
'data' => $content
|
|
];
|
|
|
|
if($withJson)
|
|
return Response::json($return,$return['code']);
|
|
else
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* The server successfully processed the request and is not returning any data.
|
|
*
|
|
* @param string $message
|
|
* @param array $content
|
|
* @return Response object
|
|
*/
|
|
public function noContent($message="NO CONTENT",$content=array(),$withJson = true)
|
|
{
|
|
$return = [
|
|
'code'=>204,
|
|
'message'=>$message,
|
|
'data' => $content
|
|
];
|
|
|
|
if($withJson)
|
|
return Response::json($return,$return['code']);
|
|
else
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* The server successfully processed the request, but is not returning any data.
|
|
* Unlike a 204 response, this response requires that the requester reset the document view.
|
|
*
|
|
* @param string $message
|
|
* @param array $content
|
|
* @return Response object
|
|
*/
|
|
public function resetContent($message="RESET CONTENT",$content=array(),$withJson = true)
|
|
{
|
|
$return = [
|
|
'code'=>205,
|
|
'message'=>$message,
|
|
'data' => $content
|
|
];
|
|
|
|
if($withJson)
|
|
return Response::json($return,$return['code']);
|
|
else
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* The server is delivering only part of the resource (byte serving) due to a range header sent by the client.
|
|
* The range header is used by HTTP clients to enable resuming of interrupted downloads,
|
|
* or split a download into multiple simultaneous streams.
|
|
*
|
|
* @param string $message
|
|
* @param array $content
|
|
* @return Response object
|
|
*/
|
|
public function partialContent($message="PARTIAL CONTENT",$content=array(),$withJson = true)
|
|
{
|
|
$return = [
|
|
'code'=>206,
|
|
'message'=>$message,
|
|
'data' => $content
|
|
];
|
|
|
|
if($withJson)
|
|
return Response::json($return,$return['code']);
|
|
else
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* The server cannot or will not process the request due to an apparent client error
|
|
* (e.g., malformed request syntax, too large size, invalid request message framing, or deceptive request routing)
|
|
*
|
|
* @param string $message
|
|
* @param array $content
|
|
* @return Response object
|
|
*/
|
|
public function badRequest($message="BAD REQUEST",$content=array(),$withJson = true)
|
|
{
|
|
$return = [
|
|
'code'=>400,
|
|
'message'=>$message,
|
|
'data' => $content
|
|
];
|
|
|
|
if($withJson)
|
|
return Response::json($return,$return['code']);
|
|
else
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided.
|
|
* The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource.
|
|
* See Basic access authentication and Digest access authentication.
|
|
* 401 semantically means "unauthenticated",[34] i.e. the user does not have the necessary credentials.
|
|
*
|
|
* @param string $message
|
|
* @param array $content
|
|
* @return Response object
|
|
*/
|
|
public function unauthorized($message="UNAUTHORIZED",$content=array(),$withJson = true)
|
|
{
|
|
$return = [
|
|
'code'=>401,
|
|
'message'=>$message,
|
|
'data' => $content
|
|
];
|
|
|
|
if($withJson)
|
|
return Response::json($return,$return['code']);
|
|
else
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* The request was a valid request, but the server is refusing to respond to it.
|
|
* The user might be logged in but does not have the necessary permissions for the resource.
|
|
*
|
|
* @param string $message
|
|
* @param array $content
|
|
* @return Response object
|
|
*/
|
|
public function forbidden($message="FORBIDDEN",$content=array(),$withJson = true)
|
|
{
|
|
$return = [
|
|
'code'=>403,
|
|
'message'=>$message,
|
|
'data' => $content
|
|
];
|
|
|
|
if($withJson)
|
|
return Response::json($return,$return['code']);
|
|
else
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* The requested resource could not be found but may be available in the future.
|
|
* Subsequent requests by the client are permissible.
|
|
*
|
|
* @param string $message
|
|
* @param array $content
|
|
* @return Response object
|
|
*/
|
|
public function notFound($message="NOT FOUND",$content=array(),$withJson = true)
|
|
{
|
|
$return = [
|
|
'code'=>404,
|
|
'message'=>$message,
|
|
'data' => $content
|
|
];
|
|
|
|
if($withJson)
|
|
return Response::json($return,$return['code']);
|
|
else
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* The request is larger than the server is willing or able to process.
|
|
* Previously called "Request Entity Too Large".
|
|
*
|
|
* @param string $message
|
|
* @param array $content
|
|
* @return Response object
|
|
*/
|
|
public function payloadToLarge($message="PAYLOAD TO LARGE",$content=array(),$withJson = true)
|
|
{
|
|
$return = [
|
|
'code'=>413,
|
|
'message'=>$message,
|
|
'data' => $content
|
|
];
|
|
|
|
if($withJson)
|
|
return Response::json($return,$return['code']);
|
|
else
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* The request was well-formed but was unable to be followed due to semantic errors
|
|
*
|
|
* @param string $message
|
|
* @param array $content
|
|
* @return Response object
|
|
*/
|
|
public function unprocessableEntity($message="Unprocessable Entity",$content=array(),$withJson = true)
|
|
{
|
|
$return = [
|
|
'code'=>422,
|
|
'message'=>$message,
|
|
'data' => $content
|
|
];
|
|
|
|
if($withJson)
|
|
return Response::json($return,$return['code']);
|
|
else
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* Error found during process
|
|
*
|
|
* @param string $message
|
|
* @param array $content
|
|
* @return Response object
|
|
*/
|
|
public function applicationValidationErrorCode($message="Error found",$content=array(),$withJson = true)
|
|
{
|
|
$return = [
|
|
'code'=>400,
|
|
'message'=>$message,
|
|
'data' => $content
|
|
];
|
|
|
|
if($withJson)
|
|
return Response::json($return,$return['code']);
|
|
else
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* A code of 498 indicates an expired or otherwise invalid token.
|
|
*
|
|
* @param string $message
|
|
* @param array $content
|
|
* @return Response object
|
|
*/
|
|
public function invalidToken($message="INVALID TOKEN",$content=array(),$withJson = true)
|
|
{
|
|
$return = [
|
|
'code'=>498,
|
|
'message'=>$message,
|
|
'data' => $content
|
|
];
|
|
|
|
if($withJson)
|
|
return Response::json($return,$return['code']);
|
|
else
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* A code of 499 indicates that a token is required but was not submitted.
|
|
*
|
|
* @param string $message
|
|
* @param array $content
|
|
* @return Response object
|
|
*/
|
|
public function requiredToken($message="REQUIRED TOKEN",$content=array(),$withJson = true)
|
|
{
|
|
$return = [
|
|
'code'=>499,
|
|
'message'=>$message,
|
|
'data' => $content
|
|
];
|
|
|
|
if($withJson)
|
|
return Response::json($return,$return['code']);
|
|
else
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* A generic error message, given when an unexpected condition was encountered and no more specific message is suitable.
|
|
*
|
|
* @param string $message
|
|
* @param array $content
|
|
* @return Response object
|
|
*/
|
|
public function internalServerError($message="INTERNAL SERVER ERROR",$content=array(),$withJson = true)
|
|
{
|
|
$return = [
|
|
'code'=>500,
|
|
'message'=>$message,
|
|
'data' => $content
|
|
];
|
|
|
|
if($withJson)
|
|
return Response::json($return,$return['code']);
|
|
else
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* Mobile Success Response
|
|
*
|
|
* @param string $message
|
|
* @param array $content
|
|
* @return Response object
|
|
*/
|
|
public function mobile_success($message="Success",$content=null, $withJson = true)
|
|
{
|
|
$return = [
|
|
'status'=>1,
|
|
'message'=>$message,
|
|
'data' => is_null($content) ? (object)[] : $content
|
|
];
|
|
|
|
if($withJson)
|
|
return Response::json($return,200);
|
|
else
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* Mobile Error Response
|
|
*
|
|
* @param string $message
|
|
* @param array $content
|
|
* @return Response object
|
|
*/
|
|
public function mobile_error($message="Error",$content=null, $withJson = true)
|
|
{
|
|
$return = [
|
|
'status'=>0,
|
|
'message'=>$message,
|
|
'data' => is_null($content) ? (object)[] : $content
|
|
];
|
|
|
|
if($withJson)
|
|
return Response::json($return,200);
|
|
else
|
|
return $return;
|
|
}
|
|
} |