-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTcmb.php
189 lines (166 loc) · 5.85 KB
/
Tcmb.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
declare(strict_types=1);
namespace Mlevent\Tcmb;
use DateTime;
use DateTimeZone;
use Mlevent\Tcmb\Exceptions\TcmbException;
class Tcmb
{
private string $bulletinDate = '';
private string $bulletinNumber = '';
private array $exchangeRates = [];
/**
* __construct
*/
public function __construct(
private DateTime $dateTime = new DateTime,
private string $timeZone = 'Europe/Istanbul',
) {
$this->dateTime->setTimeZone(new DateTimeZone($this->timeZone));
}
/**
* getExchangeRates
*/
public function getExchangeRates() : array {
if (!sizeof($this->exchangeRates)) {
$this->setExchangeRates($this->fetchExchangeRates());
}
return $this->exchangeRates;
}
/**
* getCurrencies
*/
public function getCurrencies() : array {
return array_combine(
array_column($this->getExchangeRates(), 'currencyCode'),
array_column($this->getExchangeRates(), 'currencyAlias')
);
}
/**
* getBulletinDate
*/
public function getBulletinDate() : string {
return $this->bulletinDate;
}
/**
* getBulletinNumber
*/
public function getBulletinNumber() : string {
return $this->bulletinNumber;
}
/**
* setExchangeRates
*/
public function setExchangeRates(string $data) : self {
if (!$data = json_decode(json_encode(simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOWARNING | LIBXML_NOERROR)), true)) {
throw new TcmbException('Xml data cannot be read properly.');
}
$this->bulletinDate = $data['@attributes']['Tarih'];
$this->bulletinNumber = $data['@attributes']['Bulten_No'];
foreach ($data['Currency'] as &$currency) {
foreach ($currency as $key => $value) {
$currency[$key] = $key == '@attributes' || !is_array($value) ? $value : null;
}
}
$this->exchangeRates['TRY'] = new Currency(
currencyCode : 'TRY',
currencyName : 'TURKISH LIRA',
currencyAlias : 'TÜRK LİRASI',
forexBuying : '1',
forexSelling : '1',
banknoteBuying : '1',
banknoteSelling : '1',
);
foreach ($data['Currency'] as $currency) {
$this->exchangeRates[$currency['@attributes']['CurrencyCode']] = new Currency(
currencyCode : $currency['@attributes']['CurrencyCode'],
currencyName : $currency['CurrencyName'],
currencyAlias : $currency['Isim'],
forexBuying : $currency['ForexBuying'],
forexSelling : $currency['ForexSelling'],
banknoteBuying : $currency['BanknoteBuying'],
banknoteSelling : $currency['BanknoteSelling'],
crossRateUSD : $currency['CrossRateUSD'],
crossRateOther : $currency['CrossRateOther'],
);
}
return $this;
}
/**
* fetchExchangeRates
*/
public function fetchExchangeRates() : string {
$requestUrl = !$this->isOutOfDate()
? 'https://www.tcmb.gov.tr/kurlar/today.xml'
: "https://www.tcmb.gov.tr/kurlar/{$this->dateTime->format('Ym')}/{$this->dateTime->format('dmY')}.xml";
return $this->request($requestUrl);
}
/**
* request
*/
private function request(string $url) : string {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$response = curl_exec($ch);
curl_close($ch);
if (!$response) {
throw new TcmbException('Request to server failed.');
}
return $response;
}
/**
* setDate
* !!! Tarih haftasonuna denk geliyorsa, bir önceki Cuma olacak şekilde değiştirilir !!!
*/
public function setDate(string $date) : self {
$this->dateTime = DateTime::createFromFormat('d/m/Y', $date, new DateTimeZone($this->timeZone));
if ($this->dateTime->format('N') > 5 && $this->isOutOfDate()) {
$this->dateTime->modify('Last Friday');
}
return $this;
}
/**
* isOutOfDate
*/
public function isOutOfDate() : bool {
return $this->dateTime->diff((new DateTime)->setTimeZone(new DateTimeZone($this->timeZone)))->format('%r%a') > 0;
}
/**
* get
*/
public function get(string $currency) : Currency {
$exchangeRates = $this->getExchangeRates();
if (isset($exchangeRates[$currency])) {
return $exchangeRates[$currency];
}
throw new TcmbException("Currency not found: {$currency}");
}
/**
* convert
*/
public function convert($from, $to, $amount = 1, $slot = 'forexSelling') : float {
if (in_array($slot, ['forexBuying', 'forexSelling'])) {
return round(($this->get($from)->{$slot} / $this->get($to)->{$slot}) * $amount, 4);
}
throw new TcmbException("Currency cannot be converted: {$slot}");
}
/**
* __call
*/
public function __call(string $name, $arguments) : Currency|float {
$camelCaseSplit = preg_split('/(?<=\\w)(?=[A-Z])/', $name);
if (str_starts_with($name, 'get')) {
if (sizeof($camelCaseSplit) === 2) {
return $this->get(strtoupper($camelCaseSplit[1]));
}
}
if (str_starts_with($name, 'convert')) {
if (sizeof($camelCaseSplit) === 3) {
return $this->convert(strtoupper($camelCaseSplit[1]), strtoupper($camelCaseSplit[2]), ...$arguments);
}
}
throw new TcmbException("There is no such method: {$name}");
}
}