diff --git a/src/ExchangeService.php b/src/ExchangeService.php new file mode 100644 index 00000000..211e4967 --- /dev/null +++ b/src/ExchangeService.php @@ -0,0 +1,59 @@ + + * @license http://opensource.org/licenses/MIT MIT + * @link https://github.com/apiki/back-end-challenge + */ + +namespace App; + +/** + * ExchangeService class + * + * Handles the calculation of converted values and returns currency symbols. + * + * @category Service + * @package App + * @author Andre Ruegger + * @license http://opensource.org/licenses/MIT MIT + * @link https://github.com/apiki/back-end-challenge + */ +class ExchangeService +{ + /** + * Converts a value from one currency to another using a given rate. + * + * @param float $amount Value to be converted + * @param string $from Source currency + * @param string $to Target currency + * @param float $rate Conversion rate + * + * @return float Converted value + */ + public function convert(float $amount, string $from, string $to, float $rate): float + { + return $amount * $rate; + } + + /** + * Returns the symbol of the given currency. + * + * @param string $currency Currency code (BRL, USD, EUR) + * + * @return string Currency symbol + */ + public function getSymbol(string $currency): string + { + return [ + 'BRL' => 'R$', + 'USD' => '$', + 'EUR' => '€', + ][$currency]; + } +} \ No newline at end of file diff --git a/src/index.php b/src/index.php index 92841bc8..2c2bb26f 100644 --- a/src/index.php +++ b/src/index.php @@ -8,7 +8,7 @@ * * @category Challenge * @package Back-end - * @author Seu Nome + * @author Andre Ruegger * @license http://opensource.org/licenses/MIT MIT * @link https://github.com/apiki/back-end-challenge */ @@ -16,3 +16,74 @@ require __DIR__ . '/../vendor/autoload.php'; +use App\ExchangeService; + +// Routing to the built-in PHP server +if (php_sapi_name() === 'cli-server') { + $path = __DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); + if (is_file($path)) { + return false; + } +} + +// Reads the URL +$uri = strtok($_SERVER['REQUEST_URI'], '?'); + +// Divide into parts +$parts = array_values(array_filter(explode('/', $uri))); + +// If the route starts with "exchange", remove +if (isset($parts[0]) && $parts[0] === 'exchange') { + array_shift($parts); +} + +// To ensure that we have 4 parameters +if (count($parts) !== 4) { + http_response_code(400); + header('Content-Type: application/json'); + echo json_encode(['error' => 'Invalid parameters']); + exit; +} + +[$amount, $from, $to, $rate] = $parts; + +// Valida amount +if (!is_numeric($amount) || (float)$amount <= 0) { + http_response_code(400); + header('Content-Type: application/json'); + echo json_encode(['error' => 'Invalid amount']); + exit; +} + +// Validates currencies +$validCurrencies = ['BRL', 'USD', 'EUR']; + +if (!in_array($from, $validCurrencies, true) || !in_array($to, $validCurrencies, true)) { + http_response_code(400); + header('Content-Type: application/json'); + echo json_encode(['error' => 'Invalid currency']); + exit; +} + +// Validates rate +if (!is_numeric($rate) || (float)$rate <= 0) { + http_response_code(400); + header('Content-Type: application/json'); + echo json_encode(['error' => 'Invalid rate']); + exit; +} + +// Process conversion +$service = new ExchangeService(); +$converted = $service->convert((float)$amount, $from, $to, (float)$rate); +$symbol = $service->getSymbol($to); + +// Returns JSON +http_response_code(200); +header('Content-Type: application/json'); +echo json_encode( + [ + 'valorConvertido' => $converted, + 'simboloMoeda' => $symbol, + ] +);