Commit 34b423aa authored by thiaramus's avatar thiaramus
Browse files

Added parent class to be extended by the api endpoint classes

parent 649f61e2
<?php
/*
* webJAM - A modern JAM MessageBase Editor
* Copyright (C) 2017 Philipp Giebel (stimpy@kuehlbox.wtf)
*
* This program is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see (http://www.gnu.org/licenses/).
*
* * filename: mainApiParent.php
* * description: A parent class to be extended by all apiClasses
* * author: Roman Litvinenko (temp@thiaramus.com)
*
*/
abstract class mainApiParent
{
protected $apiCallPath;
// A list of allowed HTTP methods (GET, POST, PATCH, PUT, DELETE) for the API endpoint class.
// Should be overwritten by the child classes.
protected $allowedHttpMethods = [];
public function __construct($params) {
$this->parseApiCallParams($params);
$this->validate();
}
private function validate() {
if (!count($this->apiCallPath)) {
$this->syntaxErrorMessage();
}
if (!in_array($_SERVER['REQUEST_METHOD'], $this->allowedHttpMethods)) {
$this->syntaxErrorMessage('Method ' . $_SERVER['REQUEST_METHOD'] . ' is not accepted by the function');
}
}
private function parseApiCallParams($params) {
$this->apiCallPath = explode('/', trim($params['path'], '/'));
}
/**
* A tiny wrapper for json_encode to quickly toggle pretty print for all API methods
*
* @param $response
*/
protected function response($response) {
echo json_encode($response, JSON_PRETTY_PRINT);
}
protected function syntaxErrorMessage($message = 'Syntax error') {
http_response_code(400);
$this->response([$message]);
die();
}
abstract public function run();
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment