-
Notifications
You must be signed in to change notification settings - Fork 155
Eduardo Lopes - Conclusão do desafio #210
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
Open
elopes-sv
wants to merge
2
commits into
Apiki:master
Choose a base branch
from
elopes-sv:eduardo-lopes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| <?php | ||
| /** | ||
| * Exchange request value object. | ||
| * | ||
| * PHP version 7.4 | ||
| * | ||
| * @category Challenge | ||
| * @package Back-end | ||
| * @author Eduardo Lopes <eduardo.frlopes@hotmail.com> | ||
| * @license http://opensource.org/licenses/MIT MIT | ||
| * @link https://github.com/apiki/back-end-challenge | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Exchange; | ||
|
|
||
| /** | ||
| * Exchange request value object. | ||
| * | ||
| * @category Challenge | ||
| * @package Back-end | ||
| * @author Eduardo Lopes <eduardo.frlopes@hotmail.com> | ||
| * @license http://opensource.org/licenses/MIT MIT | ||
| * @link https://github.com/apiki/back-end-challenge | ||
| */ | ||
| class ExchangeRequest | ||
| { | ||
| private float $_amount; | ||
|
|
||
| private string $_from; | ||
|
|
||
| private string $_to; | ||
|
|
||
| private float $_rate; | ||
|
|
||
| /** | ||
| * Create a new exchange request. | ||
| * | ||
| * @param float $amount Exchange amount. | ||
| * @param string $from Source currency. | ||
| * @param string $to Target currency. | ||
| * @param float $rate Exchange rate. | ||
| */ | ||
| public function __construct(float $amount, string $from, string $to, float $rate) | ||
| { | ||
| $this->_amount = $amount; | ||
| $this->_from = $from; | ||
| $this->_to = $to; | ||
| $this->_rate = $rate; | ||
| } | ||
|
|
||
| /** | ||
| * Return the exchange amount. | ||
| * | ||
| * @return float | ||
| */ | ||
| public function getAmount(): float | ||
| { | ||
| return $this->_amount; | ||
| } | ||
|
|
||
| /** | ||
| * Return the source currency. | ||
| * | ||
| * @return string | ||
| */ | ||
| public function getFrom(): string | ||
| { | ||
| return $this->_from; | ||
| } | ||
|
|
||
| /** | ||
| * Return the target currency. | ||
| * | ||
| * @return string | ||
| */ | ||
| public function getTo(): string | ||
| { | ||
| return $this->_to; | ||
| } | ||
|
|
||
| /** | ||
| * Return the exchange rate. | ||
| * | ||
| * @return float | ||
| */ | ||
| public function getRate(): float | ||
| { | ||
| return $this->_rate; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| <?php | ||
| /** | ||
| * Exchange request factory. | ||
| * | ||
| * PHP version 7.4 | ||
| * | ||
| * @category Challenge | ||
| * @package Back-end | ||
| * @author Eduardo Lopes <eduardo.frlopes@hotmail.com> | ||
| * @license http://opensource.org/licenses/MIT MIT | ||
| * @link https://github.com/apiki/back-end-challenge | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Exchange; | ||
|
|
||
| use App\Http\BadRequestException; | ||
|
|
||
| /** | ||
| * Build exchange requests from URIs. | ||
| * | ||
| * @category Challenge | ||
| * @package Back-end | ||
| * @author Eduardo Lopes <eduardo.frlopes@hotmail.com> | ||
| * @license http://opensource.org/licenses/MIT MIT | ||
| * @link https://github.com/apiki/back-end-challenge | ||
| */ | ||
| class ExchangeRequestFactory | ||
| { | ||
| /** | ||
| * Supported currencies. | ||
| * | ||
| * @var string[] | ||
| */ | ||
| private array $_supportedCurrencies = ['BRL', 'USD', 'EUR']; | ||
|
|
||
| /** | ||
| * Build a request from a URI. | ||
| * | ||
| * @param string $uri Request URI. | ||
| * | ||
| * @return ExchangeRequest | ||
| * @throws BadRequestException | ||
| */ | ||
| public function fromUri(string $uri): ExchangeRequest | ||
| { | ||
| $path = parse_url($uri, PHP_URL_PATH); | ||
| $pattern = '#^/exchange/' | ||
| . '([^/]+)/' | ||
| . '([^/]+)/' | ||
| . '([^/]+)/' | ||
| . '([^/]+)$#'; | ||
|
|
||
| if (!is_string($path)) { | ||
| throw new BadRequestException(); | ||
| } | ||
|
|
||
| if (preg_match($pattern, $path, $segments) !== 1) { | ||
| throw new BadRequestException(); | ||
| } | ||
|
|
||
| [, $amount, $from, $to, $rate] = $segments; | ||
|
|
||
| if (!$this->_isValidNumber($amount) || !$this->_isValidNumber($rate)) { | ||
| throw new BadRequestException(); | ||
| } | ||
|
|
||
| if (!$this->_isValidCurrency($from) || !$this->_isValidCurrency($to)) { | ||
| throw new BadRequestException(); | ||
| } | ||
|
|
||
| return new ExchangeRequest((float) $amount, $from, $to, (float) $rate); | ||
| } | ||
|
|
||
| /** | ||
| * Validate a numeric input. | ||
| * | ||
| * @param string $value Numeric string. | ||
| * | ||
| * @return bool | ||
| */ | ||
| private function _isValidNumber(string $value): bool | ||
| { | ||
| $number = (float) $value; | ||
|
|
||
| return is_numeric($value) && is_finite($number) && $number >= 0; | ||
| } | ||
|
|
||
| /** | ||
| * Validate a supported currency. | ||
| * | ||
| * @param string $currency Currency code. | ||
| * | ||
| * @return bool | ||
| */ | ||
| private function _isValidCurrency(string $currency): bool | ||
| { | ||
| return preg_match('/^[A-Z]{3}$/', $currency) === 1 | ||
| && in_array($currency, $this->_supportedCurrencies, true); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?php | ||
| /** | ||
| * Exchange service. | ||
| * | ||
| * PHP version 7.4 | ||
| * | ||
| * @category Challenge | ||
| * @package Back-end | ||
| * @author Eduardo Lopes <eduardo.frlopes@hotmail.com> | ||
| * @license http://opensource.org/licenses/MIT MIT | ||
| * @link https://github.com/apiki/back-end-challenge | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Exchange; | ||
|
|
||
| use App\Http\BadRequestException; | ||
|
|
||
| /** | ||
| * Convert exchange requests. | ||
| * | ||
| * @category Challenge | ||
| * @package Back-end | ||
| * @author Eduardo Lopes <eduardo.frlopes@hotmail.com> | ||
| * @license http://opensource.org/licenses/MIT MIT | ||
| * @link https://github.com/apiki/back-end-challenge | ||
| */ | ||
| class ExchangeService | ||
| { | ||
| /** | ||
| * Supported exchange pairs. | ||
| * | ||
| * @var array<string, string> | ||
| */ | ||
| private array $_supportedPairs = [ | ||
| 'BRL:USD' => '$', | ||
| 'USD:BRL' => 'R$', | ||
| 'BRL:EUR' => '€', | ||
| 'EUR:BRL' => 'R$', | ||
| ]; | ||
|
|
||
| /** | ||
| * Convert an exchange request. | ||
| * | ||
| * @param ExchangeRequest $request Exchange request. | ||
| * | ||
| * @return array<string, float|string> | ||
| * @throws BadRequestException | ||
| */ | ||
| public function convert(ExchangeRequest $request): array | ||
| { | ||
| $pair = $request->getFrom() . ':' . $request->getTo(); | ||
|
|
||
| if (!array_key_exists($pair, $this->_supportedPairs)) { | ||
| throw new BadRequestException(); | ||
| } | ||
|
|
||
| return [ | ||
| 'valorConvertido' => $request->getAmount() * $request->getRate(), | ||
| 'simboloMoeda' => $this->_supportedPairs[$pair], | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
| /** | ||
| * Bad request exception. | ||
| * | ||
| * PHP version 7.4 | ||
| * | ||
| * @category Challenge | ||
| * @package Back-end | ||
| * @author Eduardo Lopes <eduardo.frlopes@hotmail.com> | ||
| * @license http://opensource.org/licenses/MIT MIT | ||
| * @link https://github.com/apiki/back-end-challenge | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http; | ||
|
|
||
| use RuntimeException; | ||
|
|
||
| /** | ||
| * Signal invalid input data. | ||
| * | ||
| * @category Challenge | ||
| * @package Back-end | ||
| * @author Eduardo Lopes <eduardo.frlopes@hotmail.com> | ||
| * @license http://opensource.org/licenses/MIT MIT | ||
| * @link https://github.com/apiki/back-end-challenge | ||
| */ | ||
| class BadRequestException extends RuntimeException | ||
| { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| <?php | ||
| /** | ||
| * JSON response. | ||
| * | ||
| * PHP version 7.4 | ||
| * | ||
| * @category Challenge | ||
| * @package Back-end | ||
| * @author Eduardo Lopes <eduardo.frlopes@hotmail.com> | ||
| * @license http://opensource.org/licenses/MIT MIT | ||
| * @link https://github.com/apiki/back-end-challenge | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http; | ||
|
|
||
| /** | ||
| * Send JSON responses. | ||
| * | ||
| * @category Challenge | ||
| * @package Back-end | ||
| * @author Eduardo Lopes <eduardo.frlopes@hotmail.com> | ||
| * @license http://opensource.org/licenses/MIT MIT | ||
| * @link https://github.com/apiki/back-end-challenge | ||
| */ | ||
| class JsonResponse | ||
| { | ||
| /** | ||
| * Response payload. | ||
| * | ||
| * @var array<string, mixed> | ||
| */ | ||
| private array $_data; | ||
|
|
||
| /** | ||
| * Response status code. | ||
| * | ||
| * @var int | ||
| */ | ||
| private int $_statusCode; | ||
|
|
||
| /** | ||
| * Create a JSON response. | ||
| * | ||
| * @param array<string, mixed> $data Response payload. | ||
| * @param int $statusCode HTTP status code. | ||
| */ | ||
| public function __construct(array $data, int $statusCode = 200) | ||
| { | ||
| $this->_data = $data; | ||
| $this->_statusCode = $statusCode; | ||
| } | ||
|
|
||
| /** | ||
| * Send the response. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function send(): void | ||
| { | ||
| http_response_code($this->_statusCode); | ||
| header('Content-Type: application/json'); | ||
|
|
||
| echo json_encode($this->_data); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using direct float multiplication for
valorConvertidocan produce visibly wrong monetary values for some decimal inputs (for example,0.1 * 0.2yields0.020000000000000004in PHP), which hurts API accuracy and can break strict client assertions. Since this endpoint returns currency amounts, the result should be normalized to a defined precision (or computed with decimal-safe arithmetic) before serializing the response.Useful? React with 👍 / 👎.