|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace VojtaSvoboda\CnbRates\Services; |
|
4
|
|
|
|
|
5
|
|
|
use VojtaSvoboda\CnbRates\Providers\CnbDataProvider; |
|
6
|
|
|
|
|
7
|
|
|
class BaseService |
|
8
|
|
|
{ |
|
9
|
|
|
protected $dataProvider; |
|
10
|
|
|
|
|
11
|
|
|
protected $baseUrl; |
|
12
|
|
|
|
|
13
|
|
|
protected $ident; |
|
14
|
|
|
|
|
15
|
21 |
|
public function __construct(CnbDataProvider $dataProvider) |
|
16
|
|
|
{ |
|
17
|
21 |
|
$this->dataProvider = $dataProvider; |
|
18
|
21 |
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Return service data |
|
22
|
|
|
* |
|
23
|
|
|
* @param $date |
|
24
|
|
|
* |
|
25
|
|
|
* @return string |
|
26
|
|
|
*/ |
|
27
|
1 |
|
public function getDataSource($date = null) |
|
28
|
|
|
{ |
|
29
|
1 |
|
return $this->dataProvider->getData($this->getSourceUrl($date), $date, $this->ident); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get data as array |
|
34
|
|
|
* |
|
35
|
|
|
* @param null $date |
|
36
|
|
|
* @param int $keyIndex |
|
37
|
|
|
* |
|
38
|
|
|
* @return array |
|
39
|
|
|
* |
|
40
|
|
|
* @throws \Exception |
|
41
|
|
|
*/ |
|
42
|
16 |
|
public function getData($date = null, $keyIndex = 0) |
|
43
|
|
|
{ |
|
44
|
16 |
|
$data = $this->dataProvider->getData($this->getSourceUrl($date), $date, $this->ident); |
|
45
|
|
|
|
|
46
|
16 |
|
return $this->transformSourceToArray($data, $keyIndex); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get service identifier |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function getIdent() |
|
55
|
|
|
{ |
|
56
|
1 |
|
return $this->ident; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get service source URL |
|
61
|
|
|
* |
|
62
|
|
|
* @param $date |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
19 |
|
public function getSourceUrl($date = null) |
|
67
|
|
|
{ |
|
68
|
19 |
|
if ($date) { |
|
69
|
3 |
|
$dateObj = new \DateTime($date); |
|
70
|
3 |
|
$date = '?datum=' . $dateObj->format('d.m.Y'); |
|
71
|
3 |
|
} |
|
72
|
|
|
|
|
73
|
19 |
|
return $this->baseUrl . $date; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Returns service base URL |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
2 |
|
public function getBaseUrl() |
|
82
|
|
|
{ |
|
83
|
2 |
|
return $this->baseUrl; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Convert price string to float number |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $string |
|
90
|
|
|
* @param int $rounding |
|
91
|
|
|
* |
|
92
|
|
|
* @return float |
|
93
|
|
|
*/ |
|
94
|
12 |
|
public function priceStringToFloat($string, $rounding = 2) |
|
95
|
|
|
{ |
|
96
|
12 |
|
$stringWithDots = strtr($string, [',' => '.']); |
|
97
|
|
|
|
|
98
|
12 |
|
return round(floatval($stringWithDots), $rounding); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Transform data source to array |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $source |
|
105
|
|
|
* @param int $keyIndex |
|
106
|
|
|
* |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
16 |
|
private function transformSourceToArray($source, $keyIndex = 0) |
|
110
|
|
|
{ |
|
111
|
16 |
|
$return = []; |
|
112
|
16 |
|
$lines = preg_split('/\r\n|\n|\r/', trim($source)); |
|
113
|
|
|
|
|
114
|
16 |
|
foreach ($lines as $key => $line) |
|
115
|
|
|
{ |
|
116
|
|
|
// file headers |
|
117
|
16 |
|
if ($key < 3) { |
|
118
|
16 |
|
continue; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
// file lines |
|
122
|
12 |
|
$data = explode('|', $line); |
|
123
|
12 |
|
$return[$data[$keyIndex]] = $data; |
|
124
|
16 |
|
} |
|
125
|
|
|
|
|
126
|
16 |
|
return $return; |
|
127
|
|
|
} |
|
128
|
|
|
} |