diff --git a/composer.phar b/composer.phar new file mode 100755 index 00000000..3d1b9838 Binary files /dev/null and b/composer.phar differ diff --git a/src/Conversor.php b/src/Conversor.php new file mode 100644 index 00000000..502ea803 --- /dev/null +++ b/src/Conversor.php @@ -0,0 +1,101 @@ + + * @license http://opensource.org/licenses/MIT MIT + * @link https://github.com/apiki/back-end-challenge + */ + +declare(strict_types=1); + +namespace App; + +/** + * Classe que faz conversão de moedas. + * + * @category Challenge + * @package Back-end + * @author Seu Nome + * @license http://opensource.org/licenses/MIT MIT + * @link https://github.com/apiki/back-end-challenge + */ +class Conversor +{ + /** + * Símbolos das moedas suportadas. + * + * @var array + */ + private array $_simbolos = [ + 'BRL' => 'R$', + 'USD' => '$', + 'EUR' => '€', + ]; + + /** + * Pares de conversão suportados. + * + * @var array + */ + private array $_conversoesSuportadas = [ + 'BRL' => ['USD', 'EUR'], + 'USD' => ['BRL'], + 'EUR' => ['BRL'], + ]; + + /** + * Realiza a conversão entre moedas. + * + * @param float $valor valor a ser convertido + * @param string $de moeda de origem + * @param string $para moeda de destino + * @param float $taxa taxa de conversão + * + * @return array + */ + public function converter( + float $valor, + string $de, + string $para, + float $taxa + ): array { + $valorConvertido = round($valor * $taxa, 2); + + return [ + 'valorConvertido' => $valorConvertido, + 'simboloMoeda' => $this->_simbolos[$para], + ]; + } + + /** + * Verifica se a moeda é válida. + * + * @param string $moeda sigla da moeda + * + * @return bool + */ + public function moedaValida(string $moeda): bool + { + return array_key_exists($moeda, $this->_simbolos); + } + + /** + * Verifica se o par de conversão é suportado. + * + * @param string $de moeda de origem + * @param string $para moeda de destino + * + * @return bool + */ + public function conversaoSuportada(string $de, string $para): bool + { + return isset($this->_conversoesSuportadas[$de]) + && in_array($para, $this->_conversoesSuportadas[$de], true); + } +} \ No newline at end of file diff --git a/src/index.php b/src/index.php index 92841bc8..3dbe5904 100644 --- a/src/index.php +++ b/src/index.php @@ -16,3 +16,63 @@ require __DIR__ . '/../vendor/autoload.php'; +use App\Conversor; + +header('Content-Type: application/json; charset=utf-8'); + +$caminho = $_SERVER['REQUEST_URI']; +$partes = explode('/', trim($caminho, '/')); + +if (isset($partes[0]) && $partes[0] === 'exchange') { + array_shift($partes); +} + +if (count($partes) < 4) { + http_response_code(400); + echo json_encode(['erro' => 'Parâmetros insuficientes.']); + exit; +} + +[$valorBruto, $de, $para, $taxaBruta] = $partes; + +$valorBruto = str_replace('_', '.', $valorBruto); +$taxaBruta = str_replace('_', '.', $taxaBruta); + +if (!is_numeric($valorBruto) || (float) $valorBruto < 0) { + http_response_code(400); + echo json_encode(['erro' => 'Valor deve ser numérico e positivo.']); + exit; +} + +if (!is_numeric($taxaBruta) || (float) $taxaBruta < 0) { + http_response_code(400); + echo json_encode(['erro' => 'Taxa deve ser numérica e positiva.']); + exit; +} + +if ($de !== strtoupper($de) || $para !== strtoupper($para)) { + http_response_code(400); + echo json_encode(['erro' => 'Moedas devem estar em maiúsculas.']); + exit; +} + +$valor = (float) $valorBruto; +$taxa = (float) $taxaBruta; + +$conversor = new Conversor(); + +if (!$conversor->moedaValida($de) || !$conversor->moedaValida($para)) { + http_response_code(400); + echo json_encode(['erro' => 'Moeda não suportada.']); + exit; +} + +if (!$conversor->conversaoSuportada($de, $para)) { + http_response_code(400); + echo json_encode(['erro' => 'Conversão não suportada.']); + exit; +} + +http_response_code(200); +echo json_encode($conversor->converter($valor, $de, $para, $taxa)); +