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

api de conversão de moedas #144

Open
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Back-end Challenge.
*
Expand All @@ -8,11 +9,25 @@
*
* @category Challenge
* @package Back-end
* @author Seu Nome <seu-email@seu-provedor.com>
* @author Pedro Henrique da Silva <pedrohenriquedasilva100@yahoo.com.br>
* @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\Controller\Api;
use App\Controller\HttpStatus;

$status = new HttpStatus();
$status->statusVerification();

$objFeedController = new Api();

$amount = (float) @$status->getUri()[2];
$from = @$status->getUri()[3];
$to = @$status->getUri()[4];
$rate = (float) @$status->getUri()[5];
echo $objFeedController->conversion($amount, $from, $to, $rate);
82 changes: 82 additions & 0 deletions src/Controller/Api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/**
* Back-end Challenge.
*
* PHP version 7.4
*
* Arquivo de conversão de moedas
*
* @category Challenge
* @package Back-end
* @author Pedro Henrique da Silva <pedrohenriquedasilva100@yahoo.com.br>
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/apiki/back-end-challenge
*/

namespace App\Controller;

/**
* Essa classe controla faz a conversão de moedas
*
* @category Challenge
* @package Api
* @author Pedro Henrique da Silva <pedrohenriquedasilva100@yahoo.com.br>
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/apiki/back-end-challenge
*/
class Api
{
/**
* Esse método recebe os dados da url e faz a conversão das moedas
*
* @param float $amount recebe o valor a ser convertido
* @param string $from recebe a moeda de origem da conversão
* @param string $to recebe a moeda a ser convertida
* @param float $rate recebe o valor base para conversão
*
* @return json
*/
public function conversion(float $amount, string $from, string $to, float $rate)
{
$moedas = array(
"BRL" => "R$",
"USD" => "$",
"EUR" => "€",
);

if ($this->tipos($from, $to)) {
$convert = $amount * $rate;

$data["valorConvertido"] = $convert;
$data["simboloMoeda"] = @$moedas[$to];
} else {
$data["response"] = false;
}

return json_encode($data);
}

/**
* Esse método verifica se o tipo passado na url é um tipo válido
*
* @param string $from recebe a moeda de origem da conversão
* @param string $to recebe a moeda a ser convertida
*
* @return bool
*/
public function tipos(string $from, string $to)
{
$tipos = array(
"BRL" => ["USD", "EUR"],
"USD" => ["BRL"],
"EUR" => ["BRL"],
);

if (in_array($to, @$tipos[$from])) {
return true;
} else {
return false;
}
}
}
90 changes: 90 additions & 0 deletions src/Controller/HttpStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/**
* Back-end Challenge.
*
* PHP version 7.4
*
* Arquivo de verificação da url
*
* @category Challenge
* @package Back-end
* @author Pedro Henrique da Silva <pedrohenriquedasilva100@yahoo.com.br>
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/apiki/back-end-challenge
*/

namespace App\Controller;

/**
* Essa classe verifica se os parâmetros da url possui erros
*
* @category Challenge
* @package Http
* @author Pedro Henrique da Silva <pedrohenriquedasilva100@yahoo.com.br>
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/apiki/back-end-challenge
*/
class HttpStatus
{
private $_uri;

/**
* Método construtor, passa o valor url para a propriedade _uri
*/
public function __construct()
{
$this->_uri = $this->getUri();
}

/**
* Pega a url do navegador
*
* @return array
*/
public function getUri()
{
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri = explode('/', $uri);

return $uri;
}

/**
* Verifica se a url está nos padrões definidos
*
* @return null
*/
public function statusVerification()
{
if ((isset($this->_uri[1]) && $this->_uri[1] != 'exchange')
|| !isset($this->_uri[2])
|| !isset($this->_uri[3])
|| !isset($this->_uri[4])
|| !isset($this->_uri[5])
) {
$callback["message"] = 'HTTP/1.1 400 Bad Request';
echo json_encode($callback);
header("HTTP/1.1 400 Bad Request");
exit();
}

if ((is_numeric($this->_uri[2]) == false || $this->_uri[2] < 0)
|| (is_numeric($this->_uri[5]) == false || $this->_uri[5] < 0)
) {
$callback["message"] = 'HTTP/1.1 400 Bad Request';
echo json_encode($callback);
header("HTTP/1.1 400 Bad Request");
exit();
}

if (ctype_upper($this->_uri[3]) == false
|| ctype_upper($this->_uri[4]) == false
) {
$callback["message"] = 'HTTP/1.1 400 Bad Request';
echo json_encode($callback);
header("HTTP/1.1 400 Bad Request");
exit();
}
}
}