Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possível solução para o Back-end Challenge #130

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\/?([0-9]*)\/?$ /index.php?params=$1
</IfModule>
71 changes: 71 additions & 0 deletions exchange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/* Deveria verificar a porta?
if ($_SERVER["SERVER_PORT"] != 8000) {
$strErro = "Porta não permitida!";
echo "";
return http_response_code(400);
}
*/

$params = explode( "/", $_GET['params'] );

class Exchange {
// Properties
public $valorConvertido;
public $simboloMoeda;

}
$conversao = new Exchange();

if (count($params) != 5) {
// $strErro = "Quantidade de parâmetros inválida!";
echo "";
return http_response_code(400);
}

// Pasta é "exchange"?
if ($params[0] != "exchange") {
// $strErro = "URL inválida!";
echo "";
return http_response_code(400);
}

$amount = (float)$params[1];
$from = $params[2];
$to = $params[3];
$rate = (float)$params[4];

// if (!ctype_upper($from)) { ... } Conferir se moeda está com caracteres em maiúsculo?
// if (!ctype_upper($to)) { ... } Conferir se moeda está com caracteres em maiúsculo?


if (!(($from == "BRL") && ($to == "USD")) &&
!(($from == "BRL") && ($to == "EUR")) &&
!(($from == "EUR") && ($to == "BRL"))) {
$strErro = "Moeda(s) ou conversão(ões) inexistente(s)!";
echo "";
return http_response_code(400);
}

if (!is_numeric($amount) || !is_numeric($rate) ||
$amount <= 0 || $rate <= 0) {
// $strErro = "Valor(es) deve(m) ser numérico(s) e maior(es) que zero!";
echo "";
return http_response_code(400);
}

echo $strErro;

// Possiblidade de if ou array.
$simbolosMoedas = array(
"BRL" => "R$",
"USD" => "$",
"EUR" => "€", // Codificar caractere?
);

$conversao->valorConvertido = $amount * $rate;
$conversao->simboloMoeda = $simbolosMoedas[$to];

$resposta = json_encode($conversao);
echo $resposta;
return http_response_code(200);
3 changes: 2 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
*
* @category Challenge
* @package Back-end
* @author Seu Nome <seu-email@seu-provedor.com>
* @author Fernando Saling <fernandosaling@gmail.com>
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/apiki/back-end-challenge
*/
declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

require 'exchange.php';