-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_course.php
79 lines (60 loc) · 1.74 KB
/
get_course.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
/**
* Created by Maus 29.04.2019 22:39 [email protected]
*/
/**
* Class API gets currency rate ratio
* To run, use 'php get_course.php --from=USD --to=EUR'
*/
class API
{
const URL = 'http://www.nbrb.by/API/ExRates/Rates?Periodicity=0';
protected $_GET, $from, $to;
public function __construct(array $arg)
{
$_GET = getopt(null, $arg);
if (!isset($_GET['to'])) {
exit('Parameter --to not written in console');
}
if (!isset($_GET['from'])) {
$_GET['from'] = 'BYN';
}
$this->_GET = $_GET;
}
protected function get_object()
{
$response = file_get_contents(self::URL);
return json_decode($response);
}
protected function set_rate()
{
$array = $this->get_object();
foreach ($array as $currency) {
if ($currency->Cur_Abbreviation == $_GET['from']) {
$this->from = $currency->Cur_OfficialRate;
if ($currency->Cur_Scale!=1) $this->from = $this->from / $currency->Cur_Scale;
}
if ($currency->Cur_Abbreviation == $_GET['to']) {
$this->to = $currency->Cur_OfficialRate;
if ($currency->Cur_Scale!=1) $this->to = $this->to / $currency->Cur_Scale;
}
}
if ($_GET['from'] == 'BYN') {
$this->from = 1;
}
if ($_GET['to'] == 'BYN') {
$this->to = 1;
}
if (!$this->to OR !$this->from) {
exit('No currencies found');
}
}
public function show()
{
$this->set_rate();
$ratio = round($this->to / $this->from, 2);
return $ratio;
}
}
$api = new API(['from:', 'to:']);
echo $api->show();