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
Empty file modified .htaccess
100644 → 100755
Empty file.
5 changes: 0 additions & 5 deletions Carro.class.php

This file was deleted.

7 changes: 7 additions & 0 deletions IRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

interface IRequest {
public function getBody();
}

?>
4 changes: 4 additions & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Para testá-lo, basta rodar o seguinte comando de terminal na pasta 'test-dev':

$`php -S 127.0.0.1:8000`

Teste para desenvolvedor do Estadão
==============================

Expand Down
49 changes: 49 additions & 0 deletions Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

include_once 'IRequest.php';

class Request implements IRequest {
function __construct(){
$this->preencheRequest();
}

//seta todas as keys no array $_SERVER como propriedades da classe Request
private function preencheRequest(){
foreach($_SERVER as $key => $value){
$this->{$this->toCamelCase($key)} = $value;
}
}

//equivalente a parameterize() do ruby
private function toCamelCase($string){
$result = strtolower($string);

preg_match_all('/_[a-z]/', $result, $matches);

foreach($matches[0] as $match){
$c = str_replace('_', '', strtoupper($match));
$result = str_replace($match, $c, $result);
}

return $result;
}

//pega o corpo(os parâmetros) da requisição
public function getBody(){
if($this->requestMethod === "GET"){
return;
}

if ($this->requestMethod == "POST"){
$body = array();
$_POST = json_decode(file_get_contents("php://input"),true);
foreach($_POST as $key => $value){
$body[$key] = $value;
}

return $body;
}
}
}

?>
63 changes: 63 additions & 0 deletions Router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

class Router
{
private $request;
private $metodosHttpSuportados = array(
"GET",
"POST"
);

function __construct(IRequest $request) {
$this->request = $request;
}

function __call($nome, $args) {
list($rota, $metodo) = $args;

if(!in_array(strtoupper($nome), $this->metodosHttpSuportados)){
$this->trataMetodoInvalido();
}

$this->{strtolower($nome)}[$this->formataRota($rota)] = $metodo;
}

//remove / no final da rota
private function formataRota($rota) {
$resposta = rtrim($rota, '/');
if ($resposta === ''){
return '/';
}

return $resposta;
}

private function trataMetodoInvalido() {
header("{$this->request->serverProtocol} 405 Method Not Allowed");
}

private function trataRequestPadrao() {
header("{$this->request->serverProtocol} 404 Not Found");
}

//resolve a rota
function resolve() {
$tradutorDeMetodo = $this->{strtolower($this->request->requestMethod)};
$rotaFormatada = $this->formataRota($this->request->requestUri);
$metodo = $tradutorDeMetodo[$rotaFormatada];

if(is_null($metodo))
{
$this->trataRequestPadrao();
return;
}

echo call_user_func_array($metodo, array($this->request));
}

function __destruct() {
$this->resolve();
}
}

?>
4 changes: 0 additions & 4 deletions api.php

This file was deleted.

77 changes: 77 additions & 0 deletions app/servicos/operacoesBanco.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
$localBanco = 'banco';
$camposInteiros = Array('marca_id', 'ano');

function pegaTuplaPorId(String $nomeTabela, int $id)
{
$lista = pegaTabela($nomeTabela);
foreach($lista as $objeto) {
if ($objeto['id'] == $id){
return $objeto;
}
}

return Array();
}

function pegaTabela (String $nomeTabela) {
$lista_json = file_get_contents(pegaArquivoTabela( $nomeTabela));
return json_decode($lista_json, true);
}

function pegaArquivoTabela (String $nomeTabela) {
global $localBanco;

return $localBanco.'/'.$nomeTabela.'.json';
}

function criaTupla (String $nomeTabela, array $tupla) {
$lista = pegaTabela($nomeTabela);
$tupla['id'] = count($lista) + 1;
array_push($lista, trataInteiros($tupla));

consisteTabela($nomeTabela, $lista);
}

function trataInteiros ($tupla) {
global $camposInteiros;

foreach ($camposInteiros as $campo) {
if (isset($tupla[$campo])) {
$tupla[$campo] = (int) $tupla[$campo];
}
}

return $tupla;
}

function consisteTabela (String $nomeTabela, array $lista) {
file_put_contents(pegaArquivoTabela($nomeTabela), json_encode($lista));
}

function editaTupla (String $nomeTabela, array $tuplaAlterada) {
$lista = pegaTabela($nomeTabela);
foreach($lista as $key=>$value) {
if ($value['id'] == (int) $tuplaAlterada['id']){
$lista[$key] = trataInteiros($tuplaAlterada);

break;
}
}

consisteTabela($nomeTabela, $lista);
}

function deletaTupla (String $nomeTabela, int $id) {
$lista = pegaTabela($nomeTabela);
foreach($lista as $key=>$value) {
if ($value['id'] == (int) $id){
unset($lista[$key]);

break;
}
}

consisteTabela($nomeTabela, $lista);
}
?>
1 change: 1 addition & 0 deletions banco/carros.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"id":1,"modelo":"Fusion","ano":2011,"marca_id":4},{"id":2,"marca_id":2,"modelo":"Palio","ano":2015},{"id":3,"marca_id":3,"modelo":"Celta","ano":2012},{"id":4,"modelo":"Up!","ano":2016,"marca_id":1}]
19 changes: 19 additions & 0 deletions banco/marcas.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[
{
"id": 1,
"nome": "VolksWagem"
},
{
"id": 2,
"nome": "Fiat"
},
{
"id": 3,
"nome": "Chevrolet"
},
{
"id": 4,
"nome": "Ford"
}
]

41 changes: 41 additions & 0 deletions css/main.css
100644 → 100755
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
.edita{
display: none;
}

.page_container {
box-shadow: 1px 1px 3px 2px #ccc;
margin-left: 20%;
margin-right: 20%;
}

input {
line-height: normal;
border: solid 1px #cecece;
padding: 10px 20px;
border-radius: 50px;
margin: 2%;
}

.modelo_valor {
max-width: 15%;
}

.ano_valor {
max-width: 10%;
}

.input_container {
float: left;
}

button, select {
padding: 12px 18px;
border-radius: 50px;
border: 0px;
box-shadow: 1px 1px 3px 2px #ccc;
margin: 0 1%;
}

select {
background-color: white;
}
Empty file modified css/normalize.css
100644 → 100755
Empty file.
32 changes: 32 additions & 0 deletions home.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

$page = '
<!doctype html>
<html class="no-js" lang="">

<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Place favicon.ico in the root directory -->

<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css">
<script src="js/vendor/modernizr-2.8.3.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
</head>

<body>
<center class="page_container">
<h1>Carros</h1>
<div id="carros_container"></div>
</center>

<script src="js/main.js"></script>
</body>

</html>';
2 changes: 1 addition & 1 deletion index.html
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</head>

<body>

<h1>Olá</h1>
<script src="js/main.js"></script>
</body>

Expand Down
44 changes: 44 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

require_once 'app/servicos/operacoesBanco.php';

include_once 'Request.php';
include_once 'Router.php';
include_once 'home.php';
$router = new Router(new Request);

$router->get('/', function() {
global $page;

return $page;
});

$router->get('/carros', function($request) {
$carros = pegaTabela('carros');

return json_encode($carros);
});

$router->post('/carros', function($request) {
$carro = $request->getBody();
criaTupla('carros', $carro);
return "CRIADO";
});

$router->post('/edita_carros', function($request) {
$carro = $request->getBody();
editaTupla('carros', $carro);
return "EDITADO";
});

$router->post('/deleta_carros', function($request) {
$carro = $request->getBody();
deletaTupla('carros', $carro['id']);
return "DELETADO";
});

$router->get('/marcas', function($request) {
$marcas = pegaTabela('marcas');

return json_encode($marcas);
});
Loading