forked from hurbcom/challenge-bravo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddCurrency.php
78 lines (49 loc) · 1.9 KB
/
addCurrency.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
require_once('SecurityManager.php');
require_once('CurrencyManager.php');
header("Content-Type: application/json; charset=UTF-8");
// formato da resposta da API
$response = array(
"error" => '',
"code"=> $_GET['code'] ?? '',
"name"=> $_GET['name'] ?? '',
"rate"=> $_GET['rate'] ?? '',
"msg"=> '',
"query-timestamp"=> date("Y-m-d G:i:s")
);
if($_SERVER["REQUEST_METHOD"] != 'GET'){
$response['error'] = true;
$response['msg'] = "Método HTTP inválido";
die(json_encode($response, JSON_UNESCAPED_UNICODE));
}
// Verifica se os parâmetros foram informados e efetua uma prevenção de segurança básica dos valores inseridos
if(isset($_GET['code']) && !empty($_GET['code'])){
$code = SecurityManager::clearInput($_GET['code']);
}else{
$response['error'] = true;
$response['msg'] = "Parâmetro CODE não informado";
die(json_encode($response, JSON_UNESCAPED_UNICODE));
}
if(isset($_GET['name']) && !empty($_GET['name'])){
$name = SecurityManager::clearInput($_GET['name']);
}else{
$response['error'] = true;
$response['msg'] = "Parâmetro NAME não informado";
die(json_encode($response, JSON_UNESCAPED_UNICODE));
}
if(isset($_GET['rate']) && is_numeric($_GET['rate'])){
$rate = SecurityManager::clearInput($_GET['rate']);
}else{
$response['error'] = true;
$response['msg'] = "Parâmetro RATE não informado";
die(json_encode($response, JSON_UNESCAPED_UNICODE));
}
// Caso os parâmetros estejam OK, instanciar o gerenciador de moedas
$mng = new CurrencyManager();
// Chama método do gerenciador responsável por efetuar a conversão
$mngMsg = $mng->addCurrency($code, $name, $rate);
// atribui a resposta do gerenciador à resposta do endpoint
$response['error'] = $mngMsg['error'];
$response['msg'] = $mngMsg['msg'];
die(json_encode($response, JSON_UNESCAPED_UNICODE));
?>