diff --git a/composer b/composer new file mode 100755 index 00000000..3d1b9838 Binary files /dev/null and b/composer differ diff --git a/composer.json b/composer.json index ce7dfb19..7988dae1 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,9 @@ }, "autoload": { "psr-4": { - "App\\": "src/" + "App\\": "src/", + "App\\Classes\\": "src/class/", + "App\\Routers\\": "src/router/" } } } diff --git a/src/class/CurrencyConverter.php b/src/class/CurrencyConverter.php new file mode 100644 index 00000000..cdc9fb08 --- /dev/null +++ b/src/class/CurrencyConverter.php @@ -0,0 +1,74 @@ + + * @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 + * @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]); + } +} diff --git a/src/index.php b/src/index.php index 92841bc8..d9915b5b 100644 --- a/src/index.php +++ b/src/index.php @@ -8,11 +8,16 @@ * * @category Challenge * @package Back-end - * @author Seu Nome + * @author João Paulin * @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(); + diff --git a/src/router/Router.php b/src/router/Router.php new file mode 100644 index 00000000..f61e0069 --- /dev/null +++ b/src/router/Router.php @@ -0,0 +1,109 @@ + + * @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 + * @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; + } +}