Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions Carro.class.php

This file was deleted.

4 changes: 0 additions & 4 deletions api.php

This file was deleted.

8 changes: 8 additions & 0 deletions build/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

# RewriteRule
</IfModule>
102 changes: 102 additions & 0 deletions build/api/api-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
require_once __DIR__ . '/car-class.php';
require_once __DIR__ . '/brands-class.php';

class API_Handler_Class {
private $uri;
private $input;
private $versions;
private $models;

public function __construct($uri, $input) {
$this->uri = $uri;
$this->input = $input;
$this->versions = [ 'v1' ];
$this->models = [ 'carros', 'marcas' ];
}

/**
* Handle response
*
* @param array $requested Requested data
* @param array $data Response data
* @param string $message Response message
* @return string JSON
*
*/
public function response_handler($requested = [], $data = [], $message = 'Success!', $response_code = 200) {
$response = [
'success' => $response_code === 200,
'message' => $message,
'data' => $data,
'current_time' => date('Y-m-d H:i:sP'),
'requested' => $requested
];

http_response_code($response_code);
header('Access-Control-Allow-Origin: *');
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: GET,POST,PUT,DELETE");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

return json_encode($response);
}

/**
* Request validation
*
* @return string JSON
*/
public function validate() {
// * * * * * Validate api version * * * * *
$api_version = $this->uri[2];
if (empty($api_version) || !in_array($api_version, $this->versions)) {
echo $this->response_handler($this->input, [], 'Invalid api version!', 400);
exit;
}

// * * * * * Validate model * * * * *
$model = $this->uri[3];
if (empty($model) || !in_array($model, $this->models)) {
echo $this->response_handler($this->input, [], 'Model does not exist!', 400);
exit;
}
}

/**
* Get model from request
*/
private function get_model_data() {
$model = [
'name' => $this->uri[3],
'id' => (int) $this->uri[4]
];

return $model;
}

/**
* Call model
*/
public function handle_model($request_method) {
$model_data = $this->get_model_data();

switch ($model_data['name']) {
case 'carros':
$car_class = new Car_Class($this->input, $request_method, $model_data['id']);
return $car_class->process_request();
break;

case 'marcas':
$brands_class = new Brands_Class($this->input, $request_method, $model_data['id']);
return $brands_class->process_request();
break;

default:
echo $this->response_handler($this->input, [], 'Model does not exist!', 400);
break;
}
exit;
}
} // API_Handler_Class
178 changes: 178 additions & 0 deletions build/api/brands-class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php

require_once __DIR__ . '/api-handler.php';

class Brands_Class extends API_Handler_Class {
private $input;
private $request_method;
private $ID;
private $data;

const DB_FILE = 'db.json';

public static $fields = [ 'name' ];

public function __construct($input, $request_method, $ID) {
$this->input = $input;
$this->request_method = $request_method;
$this->ID = $ID;
$this->data = json_decode(file_get_contents(self::DB_FILE));
}

/**
* Process Request
*/
public function process_request() {
switch ($this->request_method) {
case 'POST':
return $this->post_handler();
break;
case 'PUT':
return $this->put_handler();
break;
case 'DELETE':
return $this->delete_handler();
break;
default:
return $this->get_handler();
}
}

/**
* Get Handler
*/
private function get_handler() {
$data = $this->data->brands;

// * * * * * Check if has ID * * * * *
if ($this->ID) {
$key = $this->get_by_id($this->ID);
return $data[$key];
}

// * * * * * Get all * * * * *
return $data;
}

/**
* Get by ID
*/
private function get_by_id($ID) {
$data = $this->data;
$key = array_search($ID, array_column($data->brands, 'id'));

if (!$key && $key !== 0) {
echo $this->response_handler($this->input, [], 'ID não encontrado!', 404);
die;
}

return $key;
}

/**
* Post Handler
*/
private function post_handler() {
$data = $this->data;

// * * * * * New ID * * * * *
$ids = array_column($data->brands, 'id');
$next_id = max($ids) + 1;

// * * * * * New append data * * * * *
$append_data = $this->valid_input();
$append_data['id'] = $next_id;

// * * * * * Append new data * * * * *
$data->brands[] = $append_data;

// * * * * * Save data * * * * *
$this->save_data($data);

return $append_data;
}

/**
* Put Handler
*/
private function put_handler() {
$data = $this->data;

// * * * * * Check if has ID * * * * *
if (!$this->ID) {
echo $this->response_handler($this->input, [], 'ID não encontrado!', 404);
die;
}

// * * * * * Update data * * * * *
$key = $this->get_by_id($this->ID);
$new_data = $this->valid_input();
$new_data['id'] = $this->ID;
$data->brands[$key] = $new_data;

// * * * * * Save data * * * * *
$this->save_data($data);

return $new_data;
}

/**
* Delete Handler
*/
private function delete_handler() {
$data = $this->data;

// * * * * * Check if has ID * * * * *
if (!$this->ID) {
echo $this->response_handler($this->input, [], 'ID não encontrado!', 404);
die;
}

// * * * * * Delete data * * * * *
$key = $this->get_by_id($this->ID);
unset($data->brands[$key]);

// * * * * * Save data * * * * *
$this->save_data($data);

return $data;
}

/**
* Save data
*/
private function save_data($new_data) {
$save_db = file_put_contents(self::DB_FILE, json_encode($new_data));

if (!$save_db) {
echo $this->response_handler($this->input, [], 'Erro ao salvar.', 500);
die;
}
}

/**
* Validate data
*/
private function valid_input() {
$data = [];

// * * * * * Check keys * * * * *
foreach (self::$fields as $key) {
if (!array_key_exists($key, $this->input)) {
echo $this->response_handler($this->input, [], "Campo \"$key\" em branco", 400);
die;
}
}

$new_title = htmlspecialchars(stripslashes(trim($this->input['name'])));
if (empty($new_title)) {
echo $this->response_handler($this->input, [], "Nome inválido", 400);
die;
}

// * * * * * Filtered data * * * * *
$data['title'] = $new_title;

return $data;
}
}
Loading