From 1e577fe93f52cc6daad7f94512e0a8a7ebcc7456 Mon Sep 17 00:00:00 2001 From: Leonardo Mazza Date: Mon, 22 Aug 2022 22:25:20 -0300 Subject: [PATCH 1/2] feat: Currency Convertion --- .idea/.gitignore | 8 ++++ .idea/back-end-challenge.iml | 83 ++++++++++++++++++++++++++++++++++++ .idea/modules.xml | 8 ++++ .idea/php.xml | 79 ++++++++++++++++++++++++++++++++++ .idea/vcs.xml | 6 +++ composer.json | 3 +- index.php | 19 ++++++++- src/CurrencyConvertion.php | 71 ++++++++++++++++++++++++++++++ 8 files changed, 274 insertions(+), 3 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/back-end-challenge.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/php.xml create mode 100644 .idea/vcs.xml create mode 100644 src/CurrencyConvertion.php diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 000000000..13566b81b --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/back-end-challenge.iml b/.idea/back-end-challenge.iml new file mode 100644 index 000000000..de2ac5167 --- /dev/null +++ b/.idea/back-end-challenge.iml @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 000000000..91aab9d04 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 000000000..6b9b404cd --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 000000000..94a25f7f4 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file 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); + } +} From ee9827cefbbd5aa5caf5808fd62a8e5694fe3160 Mon Sep 17 00:00:00 2001 From: Leonardo Mazza Date: Mon, 22 Aug 2022 22:37:38 -0300 Subject: [PATCH 2/2] Remove .idea --- .idea/.gitignore | 8 ---- .idea/back-end-challenge.iml | 83 ------------------------------------ .idea/modules.xml | 8 ---- .idea/php.xml | 79 ---------------------------------- .idea/vcs.xml | 6 --- 5 files changed, 184 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/back-end-challenge.iml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/php.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b81b..000000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/back-end-challenge.iml b/.idea/back-end-challenge.iml deleted file mode 100644 index de2ac5167..000000000 --- a/.idea/back-end-challenge.iml +++ /dev/null @@ -1,83 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 91aab9d04..000000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml deleted file mode 100644 index 6b9b404cd..000000000 --- a/.idea/php.xml +++ /dev/null @@ -1,79 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7f4..000000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file