diff --git a/composer.json b/composer.json index ce7dfb195..1fd6f9f31 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,8 @@ "description": "Desafio para candidatos a back-end.", "type": "project", "require": { - "php": ">= 7.4" + "php": ">= 7.4", + "ext-json": "*" }, "require-dev": { "squizlabs/php_codesniffer": "^3.4", diff --git a/index.php b/index.php index 249b94af4..f547f5a04 100644 --- a/index.php +++ b/index.php @@ -8,11 +8,26 @@ * * @category Challenge * @package Back-end - * @author Seu Nome + * @author Leonardo Mazza de Souza * @license http://opensource.org/licenses/MIT MIT * @link https://github.com/apiki/back-end-challenge */ -declare(strict_types=1); + + +use App\CurrencyConvertion; require __DIR__ . '/vendor/autoload.php'; +$allowedCurrency = ['BRL' => 'R$','USD' => '$','EUR' => '€']; +$urlParams = array_filter(explode('/', $_SERVER['REQUEST_URI'])); + +$amount = !empty($urlParams[2]) ? (double) $urlParams[2] : NULL; +$rate = !empty($urlParams[5]) ? (double) $urlParams[5] : NULL; + +$from = !empty($urlParams[3]) ? $urlParams[3] : NULL; +$to = !empty($urlParams[4]) ? $urlParams[4] : ''; +$exchangeUrl = !empty($urlParams[1]) && $urlParams[1] == 'exchange'; + +$object = new CurrencyConvertion; +$object->convert($amount, $from, $to, $rate, $allowedCurrency, $exchangeUrl); +echo json_encode($object); diff --git a/src/CurrencyConvertion.php b/src/CurrencyConvertion.php new file mode 100644 index 000000000..f9c702219 --- /dev/null +++ b/src/CurrencyConvertion.php @@ -0,0 +1,71 @@ + + * @license http://opensource.org/licenses/MIT MIT + * @link https://github.com/apiki/back-end-challenge + */ + +namespace App; + +/** + * CurrencyConvertion. + * + * PHP version 7.4 + * + * Classe responsável pela conversão. + * + * @category Challenge + * @package Back-end + * @author Leonardo Mazza de Souza + * @license http://opensource.org/licenses/MIT MIT + * @link https://github.com/apiki/back-end-challenge + */ +class CurrencyConvertion +{ + public $valorConvertido; + public $simboloMoeda; + + /** + * Convert. + * + * PHP version 7.4 + * + * Função responsável pela conversão. + * + * @param $amount double Recebe a quantia + * @param $from string De + * @param $to string Para + * @param $rate double Taxa de Conversão + * @param $allowedCurrency array Conversões Permitidas + * @param $url string Exchange Url + * + * @return int + */ + public function convert($amount, $from, $to, $rate, $allowedCurrency, $url) + { + if ($amount < 0 + || $rate < 0 + || empty($amount) + || empty($rate) + || empty($from) + || empty($to) + || empty($url) + || array_key_exists($from, $allowedCurrency) === false + || array_key_exists($to, $allowedCurrency) === false + ) { + return http_response_code(400); + } + + $this->valorConvertido = $amount * $rate; + $this->simboloMoeda = $allowedCurrency[$to]; + return http_response_code(200); + } +}