From 9d14be2b07de6b449638be4b160867cfffda60e2 Mon Sep 17 00:00:00 2001 From: Andre Ruegger Date: Wed, 4 Mar 2026 10:18:44 -0300 Subject: [PATCH 1/2] Improve API routing and validation: - Add support for /exchange prefix - Fix server routing for PHP built-in server - Normalize URL parsing - Validate parameters correctly - Integrate with ExchangeService - All 14 tests passing --- src/ExchangeService.php | 20 ++++++++++++ src/index.php | 71 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 src/ExchangeService.php diff --git a/src/ExchangeService.php b/src/ExchangeService.php new file mode 100644 index 00000000..bc16fbf6 --- /dev/null +++ b/src/ExchangeService.php @@ -0,0 +1,20 @@ + '$', + 'BRL' => 'R$', + 'EUR' => '€', + ][$currency] ?? ''; + } +} \ No newline at end of file diff --git a/src/index.php b/src/index.php index 92841bc8..96524e01 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,72 @@ 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, +]); From 25eb612a0f4e3f77fc6a5a243a881ed37ec308a3 Mon Sep 17 00:00:00 2001 From: Andre Ruegger Date: Wed, 4 Mar 2026 11:32:57 -0300 Subject: [PATCH 2/2] style: add full PSR-12 docblocks to ExchangeService --- src/ExchangeService.php | 43 +++++++++++++++++++++++++++++++++++++++-- src/index.php | 6 ++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/ExchangeService.php b/src/ExchangeService.php index bc16fbf6..211e4967 100644 --- a/src/ExchangeService.php +++ b/src/ExchangeService.php @@ -1,20 +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 [ - 'USD' => '$', 'BRL' => 'R$', + 'USD' => '$', 'EUR' => '€', - ][$currency] ?? ''; + ][$currency]; } } \ No newline at end of file diff --git a/src/index.php b/src/index.php index 96524e01..2c2bb26f 100644 --- a/src/index.php +++ b/src/index.php @@ -81,7 +81,9 @@ // Returns JSON http_response_code(200); header('Content-Type: application/json'); -echo json_encode([ +echo json_encode( + [ 'valorConvertido' => $converted, 'simboloMoeda' => $symbol, -]); + ] +);