Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leonardo Mazza #137

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 17 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,26 @@
*
* @category Challenge
* @package Back-end
* @author Seu Nome <seu-email@seu-provedor.com>
* @author Leonardo Mazza de Souza <desenvolvedormazza@gmail.com>
* @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);
71 changes: 71 additions & 0 deletions src/CurrencyConvertion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Back-end Challenge.
*
* PHP version 7.4
*
* Este será o arquivo chamado na execução dos testes automátizados.
*
* @category Challenge
* @package Back-end
* @author Leonardo Mazza de Souza <desenvolvedormazza@gmail.com>
* @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 <desenvolvedormazza@gmail.com>
* @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);
}
}