Last active
March 7, 2018 08:28
-
-
Save arount/853959d6251c0cc758704429826f248e to your computer and use it in GitHub Desktop.
Api PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include_once('sql.php'); | |
include_once('request.php'); | |
include_once('response.php'); | |
header('Content-Type: application/json'); | |
$request = Request::auto_construct(); | |
$response = new Response($request); | |
$response->respond(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Request { | |
protected static $allowed_verbes = array("GET"); | |
protected static $allowed_items = array('games', 'companies', 'keywords'); | |
protected static $allowed_fields = array('id', 'slug'); | |
protected static $max_limit = 100; | |
public $error = false; | |
public $error_msg = null; | |
public $item = null; | |
public $limit = null; | |
public $verbe = null; | |
public $pagination = null; | |
public $fields = null; | |
function __construct($verbe, $item, $fields, $limit = 20, $pagination = 0) | |
{ | |
$this->verbe = $verbe; | |
$this->item = $item; | |
$this->limit = $limit; | |
$this->fields = $fields; | |
$this->pagination = $pagination; | |
$this->validate(); | |
} | |
function validate() | |
{ | |
if(!in_array($this->verbe, get_called_class()::$allowed_verbes)) { | |
$this->error = true; | |
$this->error_msg = 'HTTP verbe "' . $this->verbe . '" unauthorized.'; | |
} | |
elseif(!in_array($this->item, get_called_class()::$allowed_items)) { | |
$this->error = true; | |
$this->error_msg = 'Item type "' . $this->item . '" unauthorized.'; | |
} | |
elseif($this->limit > get_called_class()::$max_limit) { | |
$this->error = true; | |
$this->error_msg = 'Limit ' . $this->limit . ' too high, max: ' . $this->max_limit; | |
} | |
else { | |
foreach($this->fields as $field) { | |
if(!in_array($field, get_called_class()::$allowed_fields)) { | |
$this->error = true; | |
$this->error_msg = 'Fields "' . join(', ', $this->fields) . '" contains unauthorized fields.'; | |
continue; | |
} | |
} | |
} | |
return $this->error; | |
} | |
static function from_array($array) | |
{ | |
return new self( | |
$array['HTTP_METHOD'], | |
$array['REQUEST']['item'], | |
explode(',', $array['REQUEST']['fields']), | |
$array['REQUEST']['limit'], | |
$array['REQUEST']['pagination'] | |
); | |
} | |
static function auto_construct() | |
{ | |
$verbe = $_SERVER['REQUEST_METHOD']; | |
if(isset($_REQUEST['item'])) { | |
$item = $_REQUEST['item']; | |
} else { | |
$item = null; | |
} | |
if(isset($_REQUEST['fields'])) { | |
$fields = explode(',', $_REQUEST['fields']); | |
} else { | |
$fields = null; | |
} | |
if(isset($_REQUEST['limit'])) { | |
$limit = $_REQUEST['limit']; | |
} else { | |
$limit = null; | |
} | |
if(isset($_REQUEST['pagination'])) { | |
$pagin = $_REQUEST['pagination']; | |
} else { | |
$pagin = null; | |
} | |
if($item == 'games') { | |
return new GamesRequest($verbe, $fields, $limit, $pagin); | |
} | |
elseif($item == 'companies') { | |
return new CompaniesRequest($verbe, $fields, $limit, $pagin); | |
} | |
elseif($item == 'keywords') { | |
return new KeywordsRequest($verbe, $fields, $limit, $pagin); | |
} | |
else { | |
return new self($verbe, $item, $fields, $limit, $pagin); | |
} | |
} | |
} | |
class GamesRequest extends Request{ | |
protected static $allowed_fields = array('id', 'name', 'slug', 'popularity'); | |
protected static $allowed_items = array('games'); | |
function __construct($verbe, $fields, $limit = 20, $pagination = 0) | |
{ | |
parent::__construct($verbe, 'games', $fields, $limit, $pagination); | |
} | |
} | |
class CompaniesRequest extends Request{ | |
protected static $allowed_fields = array('id', 'name', 'slug'); | |
protected static $allowed_items = array('companies'); | |
function __construct($verbe, $fields, $limit = 20, $pagination = 0) | |
{ | |
parent::__construct($verbe, 'companies', $fields, $limit, $pagination); | |
} | |
} | |
class KeywordsRequest extends Request{ | |
protected static $allowed_fields = array('id', 'name', 'slug'); | |
protected static $allowed_items = array('keywords'); | |
protected static $max_limit = 400; | |
function __construct($verbe, $fields, $limit = 20, $pagination = 0) | |
{ | |
parent::__construct($verbe, 'keywords', $fields, $limit, $pagination); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Response { | |
public $error = false; | |
public $http_status = 200; | |
public $request = null; | |
public $data = array(); | |
function __construct($request) | |
{ | |
$this->request = $request; | |
if($this->request->error === true) { | |
$this->http_status = 400; | |
} | |
$this->db = new Database('localhost', 'root', 'root', 'igdb'); | |
} | |
function get_data() | |
{ | |
$query = $this->db->compute_query( | |
$this->request->item, | |
$this->request->fields, | |
$this->request->limit, | |
$this->request->pagination | |
); | |
return array( | |
"data" => $this->db->execute($query), | |
"error" => false, | |
"_query" => $query | |
); | |
} | |
function respond() | |
{ | |
if($this->request->error) { | |
echo(json_encode(array("error" => true, "message" => $this->request->error_msg))); | |
} | |
else { | |
echo(json_encode($this->get_data())); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Database { | |
function __construct($hostname, $username, $password, $database) | |
{ | |
$this->db = new mysqli($hostname, $username, $password, $database); | |
} | |
function compute_query($item, $fields, $limit, $pagination) | |
{ | |
$sql_fields = join(', ', $fields); | |
$offset = $pagination * $limit; | |
return "SELECT $sql_fields FROM $item LIMIT $offset, $limit"; | |
} | |
function execute($query) | |
{ | |
return $this->db->query($query)->fetch_all(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment