CustomerRewardsRESTAPI/Controller/API/BaseController.php

149 lines
4.3 KiB
PHP
Raw Normal View History

2024-04-15 11:38:59 -05:00
<?php
/**
* Description of BaseController
*
2024-05-06 13:19:09 -05:00
* @author Mike Howard
2024-04-15 11:38:59 -05:00
*/
class BaseController {
2024-05-09 20:25:56 -05:00
public $basename;
public $requestMethod;
public $arrQueryStringParams;
public $strErrorDesc;
public $strErrorHeader;
2024-05-22 14:55:08 -05:00
public $strErrorMessage;
2024-05-06 13:19:09 -05:00
public function __construct() {
2024-05-09 20:25:56 -05:00
$this->basename = 'BaseController';
2024-05-06 13:19:09 -05:00
}
public static function create() { return new self(); }
2024-04-15 11:38:59 -05:00
/**
2024-05-06 13:19:09 -05:00
* The __call() method is invoked automatically when a non-existing method or inaccessible method is called.
2024-04-15 11:38:59 -05:00
*/
public function __call($name, $arguments)
{
2024-05-06 13:19:09 -05:00
$this->sendOutput('', array('HTTP/1.1 404 Non-Existant method or inaccessible method called'));
2024-04-15 11:38:59 -05:00
}
2024-05-09 20:25:56 -05:00
2024-05-22 14:55:08 -05:00
/*
* Set the error description when an unknown error occurs
*/
public function internalErrorResponse($error)
2024-05-09 20:25:56 -05:00
{
2024-05-22 14:55:08 -05:00
$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();
2024-05-09 20:25:56 -05:00
return $response;
}
2024-05-22 14:55:08 -05:00
/*
* Set the error description when an unknown method is called
*/
public function unprocessableRequestResponse($msg)
2024-05-14 11:38:57 -05:00
{
$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';
2024-05-22 14:55:08 -05:00
$this->strErrorMessage = $msg;
2024-05-14 11:38:57 -05:00
return $response;
}
2024-05-22 14:55:08 -05:00
/*
* Set the error description when the payload does not contain the required info
*/
public function unprocessableEntityResponse($msg)
2024-05-09 20:25:56 -05:00
{
$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';
2024-05-22 14:55:08 -05:00
$this->strErrorMessage = $msg;
2024-05-09 20:25:56 -05:00
return $response;
}
2024-05-22 14:55:08 -05:00
/*
* Set the error description when the
*/
public function notFoundResponse($msg)
2024-05-09 20:25:56 -05:00
{
$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';
2024-05-22 14:55:08 -05:00
$this->strErrorMessage = $msg;
2024-05-09 20:25:56 -05:00
return $response;
}
2024-04-15 11:38:59 -05:00
/**
2024-05-22 14:55:08 -05:00
* Get URI elements.
*
* @return array
*/
public function getUriSegments()
2024-04-15 11:38:59 -05:00
{
$requestUri = filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_STRING); /*htmlspecialchars()*/
$parsedUri = parse_url($requestUri, PHP_URL_PATH);
$uri = explode( '/', $parsedUri );
2024-04-15 11:38:59 -05:00
return $uri;
}
/**
2024-05-22 14:55:08 -05:00
* Get querystring params.
*
* @return array
*/
public function getQueryStringParams()
2024-04-15 11:38:59 -05:00
{
$query = array();
$queryString = filter_input(INPUT_SERVER, 'QUERY_STRING', FILTER_SANITIZE_STRING); /*htmlspecialchars()*/
parse_str($queryString, $query);
2024-04-15 11:38:59 -05:00
return $query;
}
public function getServerRequestMethod()
{
$requestMethod = filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_STRING); /*htmlspecialchars()*/
return $requestMethod;
}
2024-05-22 14:55:08 -05:00
/*
* Validate the resuest type against the called method
*/
public function checkRequestType($request)
{
$response = 'false';
if (strtoupper($this->requestMethod) == $request) {
$response = 'true';
}
return $response;
}
2024-04-15 11:38:59 -05:00
/**
2024-05-22 14:55:08 -05:00
* Send API output.
*
* @param mixed $data
* @param string $httpHeader
*/
public function sendOutput($data, $httpHeaders=array())
2024-04-15 11:38:59 -05:00
{
header_remove('Set-Cookie');
if (is_array($httpHeaders) && count($httpHeaders)) {
foreach ($httpHeaders as $httpHeader) {
header($httpHeader);
}
}
echo $data;
exit;
}
}