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.phar
Binary file not shown.
101 changes: 101 additions & 0 deletions src/Conversor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

/**
* Back-end Challenge.
*
* PHP version 7.4
*
* @category Challenge
* @package Back-end
* @author Seu Nome <seu-email@seu-provedor.com>
* @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 <seu-email@seu-provedor.com>
* @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);
}
}
60 changes: 60 additions & 0 deletions src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Loading