mirror of
https://github.com/sctn4elk/CustomerRewardsRESTAPI.git
synced 2025-01-09 05:34:29 -06:00
149 lines
4.3 KiB
PHP
149 lines
4.3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Description of BaseController
|
|
*
|
|
* @author Mike Howard
|
|
*/
|
|
class BaseController {
|
|
public $basename;
|
|
public $requestMethod;
|
|
public $arrQueryStringParams;
|
|
public $strErrorDesc;
|
|
public $strErrorHeader;
|
|
public $strErrorMessage;
|
|
|
|
public function __construct() {
|
|
$this->basename = 'BaseController';
|
|
}
|
|
|
|
public static function create() { return new self(); }
|
|
|
|
/**
|
|
* The __call() method is invoked automatically when a non-existing method or inaccessible method is called.
|
|
*/
|
|
public function __call($name, $arguments)
|
|
{
|
|
$this->sendOutput('', array('HTTP/1.1 404 Non-Existant method or inaccessible method called'));
|
|
}
|
|
|
|
/*
|
|
* Set the error description when an unknown error occurs
|
|
*/
|
|
public function internalErrorResponse($error)
|
|
{
|
|
$response['status_code_header'] = 'HTTP/1.1 500 Internal Server Error';
|
|
$response['body'] = json_encode([
|
|
'error' => 'Invalid request'
|
|
]);
|
|
$this->strErrorDesc = 'An internal Error has occured! Please contact support.';
|
|
$this->strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
|
|
$this->strErrorMessage = $error->getMessage();
|
|
return $response;
|
|
}
|
|
|
|
/*
|
|
* Set the error description when an unknown method is called
|
|
*/
|
|
public function unprocessableRequestResponse($msg)
|
|
{
|
|
$response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Request';
|
|
$response['body'] = json_encode([
|
|
'error' => 'Invalid request'
|
|
]);
|
|
$this->strErrorDesc = 'Request Method not supported for processAction';
|
|
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
|
|
$this->strErrorMessage = $msg;
|
|
return $response;
|
|
}
|
|
|
|
/*
|
|
* Set the error description when the payload does not contain the required info
|
|
*/
|
|
public function unprocessableEntityResponse($msg)
|
|
{
|
|
$response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Payload';
|
|
$response['body'] = json_encode([
|
|
'error' => 'Invalid input'
|
|
]);
|
|
$this->strErrorDesc = 'Unprocessable Payload';
|
|
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Payload';
|
|
$this->strErrorMessage = $msg;
|
|
return $response;
|
|
}
|
|
|
|
/*
|
|
* Set the error description when the
|
|
*/
|
|
public function notFoundResponse($msg)
|
|
{
|
|
$response['status_code_header'] = 'HTTP/1.1 404 Entity Not Found';
|
|
$response['body'] = null;
|
|
$this->strErrorDesc = 'Request Entity Not Found';
|
|
$this->strErrorHeader = 'HTTP/1.1 422 Entity Not Found';
|
|
$this->strErrorMessage = $msg;
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* Get URI elements.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getUriSegments()
|
|
{
|
|
$requestUri = filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_STRING); /*htmlspecialchars()*/
|
|
$parsedUri = parse_url($requestUri, PHP_URL_PATH);
|
|
$uri = explode( '/', $parsedUri );
|
|
return $uri;
|
|
}
|
|
/**
|
|
* Get querystring params.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getQueryStringParams()
|
|
{
|
|
$query = array();
|
|
$queryString = filter_input(INPUT_SERVER, 'QUERY_STRING', FILTER_SANITIZE_STRING); /*htmlspecialchars()*/
|
|
parse_str($queryString, $query);
|
|
return $query;
|
|
}
|
|
|
|
public function getServerRequestMethod()
|
|
{
|
|
$requestMethod = filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_STRING); /*htmlspecialchars()*/
|
|
return $requestMethod;
|
|
}
|
|
|
|
/*
|
|
* Validate the resuest type against the called method
|
|
*/
|
|
public function checkRequestType($request)
|
|
{
|
|
$response = 'false';
|
|
if (strtoupper($this->requestMethod) == $request) {
|
|
$response = 'true';
|
|
}
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* Send API output.
|
|
*
|
|
* @param mixed $data
|
|
* @param string $httpHeader
|
|
*/
|
|
public function sendOutput($data, $httpHeaders=array())
|
|
{
|
|
header_remove('Set-Cookie');
|
|
if (is_array($httpHeaders) && count($httpHeaders)) {
|
|
foreach ($httpHeaders as $httpHeader) {
|
|
header($httpHeader);
|
|
}
|
|
}
|
|
echo $data;
|
|
exit;
|
|
}
|
|
}
|