I'm implementing a REST API in PHP 7.1. It is designed to throw Exceptions to print non-200 responses.
I decided to go with the following:
abstract class APIException extends \Exception
{
public function __construct($message, $code, Exception $previous = null) {
parent::__construct($message, $code, $previous);
}
public function parseException() {
return ResponseParser::parseResponse($this->message, $this->code);
}
}
And then I extended the BaseException with my various personal exceptions, like IncorrectMethodException here.
class IncorrectMethodException extends APIException
{
public function __construct(String $expectedMethod, Exception $previous = null) {
parent::__construct("Incorrect method, $expectedMethod expected", 405, $previous);
}
}
Here is the code to my ResponseParser class:
class ResponseParser
{
/**
* @param $data
* @param int $status
* @return string
*/
public static function parseResponse($data, $status = 200)
{
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: *");
header("Content-Type: application/json");
header("HTTP/1.1 " . $status . " " . self::_requestStatus($status));
return json_encode($data);
}
/**
* @param $code
* @return string status code
*/
private static function _requestStatus($code)
{
$status = array(
200 => 'OK',
400 => 'Bad Request',
401 => 'Unauthorized',
404 => 'Not Found',
405 => 'Method Not Allowed',
500 => 'Internal Server Error',
);
return ($status[$code]) ? $status[$code] : $status[500];
}
}
Is this a correct way to avoid code duplication here?