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
Binary file added composer
Binary file not shown.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
},
"autoload": {
"psr-4": {
"App\\": "src/"
"App\\": "src/",
"App\\Classes\\": "src/class/",
"App\\Routers\\": "src/router/"
}
}
}
74 changes: 74 additions & 0 deletions src/class/CurrencyConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Back-end Challenge.
*
* PHP version 7.4
*
* @category Challenge
* @package Back-end
* @author João Paulin <ppaulin23@hotmail.com>
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/joaopaulin/back-end-challenge
*/
declare(strict_types=1);

namespace App\Classes;

/**
* Classe CurrencyConverter.
*
* PHP version 7.4
*
* Gerencia a lógica de conversão de moedas e o mapeamento de símbolos.
*
* @category Challenge
* @package Back-end
* @author João Paulin <ppaulin23@hotmail.com>
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/joaopaulin/back-end-challenge
*/
class CurrencyConverter
{
private const SYMBOLS = [
'BRL' => 'R$',
'USD' => '$',
'EUR' => '€'
];

/**
* Converte um valor com base em uma taxa fornecida.
*
* @param float $amount Valor a ser convertido.
* @param float $rate Taxa de conversão.
*
* @return float
*/
public function convert(float $amount, float $rate): float
{
return $amount * $rate;
}

/**
* Retorna o símbolo para um determinado código de moeda.
*
* @param string $currencyCode Código da moeda (BRL, USD, EUR).
*
* @return string
*/
public function getSymbol(string $currencyCode): string
{
return self::SYMBOLS[$currencyCode] ?? '';
}

/**
* Valida se o código da moeda é suportado e está em letras maiúsculas.
*
* @param string $currencyCode Código da moeda.
*
* @return bool
*/
public function isValidCurrency(string $currencyCode): bool
{
return isset(self::SYMBOLS[$currencyCode]);
}
}
9 changes: 7 additions & 2 deletions src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
*
* @category Challenge
* @package Back-end
* @author Seu Nome <seu-email@seu-provedor.com>
* @author João Paulin <ppaulin23@hotmail.com>
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/apiki/back-end-challenge
* @link https://github.com/joaopaulin/back-end-challenge
*/
declare(strict_types=1);

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

use App\Routers\Router;

$router = new Router();
$router->handleRequest();

109 changes: 109 additions & 0 deletions src/router/Router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* Back-end Challenge.
*
* PHP version 7.4
*
* @category Challenge
* @package Back-end
* @author João Paulin <ppaulin23@hotmail.com>
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/joaopaulin/back-end-challenge
*/
declare(strict_types=1);

namespace App\Routers;

use App\Classes\CurrencyConverter;

/**
* Classe Router.
*
* PHP version 7.4
*
* Responsável por gerenciar as rotas e despachar para o conversor de moedas.
*
* @category Challenge
* @package Back-end
* @author João Paulin <ppaulin23@hotmail.com>
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/joaopaulin/back-end-challenge
*/
class Router
{
/**
* Lida com a requisição atual.
*
* @return void
*/
public function handleRequest(): void
{
// Define o cabeçalho JSON
header('Content-Type: application/json');

// Obtém a URI da requisição
$request_uri = $_SERVER['REQUEST_URI'];
$path = parse_url($request_uri, PHP_URL_PATH);
$pathParts = explode('/', trim($path, '/'));

if (count($pathParts) < 4) {
$this->_sendErrorResponse();
}

// Se a primeira parte for 'exchange', remove-a
if ($pathParts[0] === 'exchange') {
array_shift($pathParts);
}

// Verifica se temos partes suficientes (amount, from, to, rate)
if (count($pathParts) !== 4) {
$this->_sendErrorResponse();
}

[$amountRaw, $from, $to, $rateRaw] = $pathParts;

// Validação básica
if (!is_numeric($amountRaw) || !is_numeric($rateRaw)) {
$this->_sendErrorResponse();
}

$amount = (float) $amountRaw;
$rate = (float) $rateRaw;

if ($amount < 0 || $rate < 0) {
$this->_sendErrorResponse();
}

$converter = new CurrencyConverter();

if (!$converter->isValidCurrency($from)
|| !$converter->isValidCurrency($to)
) {
$this->_sendErrorResponse();
}

// Executa a conversão e mapeia símbolo
$convertedValue = $converter->convert($amount, $rate);
$symbol = $converter->getSymbol($to);

// Resposta de Sucesso
echo json_encode(
[
'valorConvertido' => $convertedValue,
'simboloMoeda' => $symbol
]
);
}

/**
* Envia uma resposta de erro 400.
*
* @return void
*/
private function _sendErrorResponse(): void
{
http_response_code(400);
echo json_encode(['error' => 'Requisicao Invalida']);
exit;
}
}
Loading