-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Day.php
75 lines (55 loc) · 1.55 KB
/
Day.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
<?php declare(strict_types=1);
namespace h4kuna\Exchange\Driver\Cnb;
use DateTimeInterface;
use DateTimeZone;
use h4kuna\Exchange;
use h4kuna\Exchange\Download\SourceData;
use h4kuna\Exchange\Utils;
use Psr\Http\Message\ResponseInterface;
class Day implements Exchange\Driver\Source
{
public static string $url = 'https://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.txt';
private DateTimeZone $timeZone;
public function __construct(
string|DateTimeZone $timeZone = 'Europe/Prague',
private string $refresh = 'today 14:30:00',
)
{
$this->timeZone = Utils::createTimeZone($timeZone);
}
public function getTimeZone(): DateTimeZone
{
return $this->timeZone;
}
public function makeUrl(?DateTimeInterface $date): string
{
$url = self::$url;
if ($date === null) {
return $url;
}
return "$url?" . http_build_query([
'date' => $date->format('d.m.Y'),
]);
}
public function createSourceData(ResponseInterface $response): SourceData
{
$data = $response->getBody()->getContents();
$list = explode("\n", Utils::stroke2point($data));
$list[1] = 'Česká Republika|koruna|1|CZK|1';
$date = Utils::createFromFormat('!d.m.Y', explode(' ', $list[0])[0], $this->timeZone);
unset($list[0]);
return new SourceData($date, $this->refresh, $list);
}
public function createProperty(mixed $row): Property
{
assert(is_string($row));
$currency = explode('|', $row);
return new Property(
intval($currency[2]),
floatval($currency[4]),
$currency[3],
$currency[0],
$currency[1],
);
}
}