Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/ExchangeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Service responsible for performing currency conversion.
*
* PHP version 7.4+
*
* @category Service
* @package App
* @author Andre Ruegger <rueggertec@gmail.com>
* @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 <rueggertec@gmail.com>
* @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];
}
}
73 changes: 72 additions & 1 deletion src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,82 @@
*
* @category Challenge
* @package Back-end
* @author Seu Nome <seu-email@seu-provedor.com>
* @author Andre Ruegger <rueggertec@gmail.com>
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/apiki/back-end-challenge
*/
declare(strict_types=1);

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,
]
);