1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Orkhanahmadov\Currencylayer\Data; |
4
|
|
|
|
5
|
|
|
class Change |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
private $source; |
11
|
|
|
/** |
12
|
|
|
* @var \DateTimeInterface |
13
|
|
|
*/ |
14
|
|
|
private $startDate; |
15
|
|
|
/** |
16
|
|
|
* @var \DateTimeInterface |
17
|
|
|
*/ |
18
|
|
|
private $endDate; |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $quotes; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Change constructor. |
26
|
|
|
* |
27
|
|
|
* @param array $data |
28
|
|
|
* |
29
|
|
|
* @throws \Exception |
30
|
|
|
*/ |
31
|
|
|
public function __construct(array $data) |
32
|
|
|
{ |
33
|
|
|
$this->source = $data['source']; |
34
|
|
|
$this->quotes = $data['quotes']; |
35
|
|
|
$this->startDate = new \DateTimeImmutable($data['start_date']); |
36
|
|
|
$this->endDate = new \DateTimeImmutable($data['end_date']); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function source(): string |
43
|
|
|
{ |
44
|
|
|
return $this->source; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return \DateTimeInterface |
49
|
|
|
*/ |
50
|
|
|
public function startDate(): \DateTimeInterface |
51
|
|
|
{ |
52
|
|
|
return $this->startDate; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return \DateTimeInterface |
57
|
|
|
*/ |
58
|
|
|
public function endDate(): \DateTimeInterface |
59
|
|
|
{ |
60
|
|
|
return $this->endDate; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
public function quotes(): array |
67
|
|
|
{ |
68
|
|
|
return $this->quotes; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $currency |
73
|
|
|
* |
74
|
|
|
* @return float |
75
|
|
|
*/ |
76
|
|
|
public function startRate(string $currency): float |
77
|
|
|
{ |
78
|
|
|
return $this->quotes[$this->findKey($currency)]['start_rate']; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $currency |
83
|
|
|
* |
84
|
|
|
* @return float |
85
|
|
|
*/ |
86
|
|
|
public function endRate(string $currency): float |
87
|
|
|
{ |
88
|
|
|
return $this->quotes[$this->findKey($currency)]['end_rate']; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $currency |
93
|
|
|
* |
94
|
|
|
* @return float |
95
|
|
|
*/ |
96
|
|
|
public function amount(string $currency): float |
97
|
|
|
{ |
98
|
|
|
return $this->quotes[$this->findKey($currency)]['change']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $currency |
103
|
|
|
* |
104
|
|
|
* @return float |
105
|
|
|
*/ |
106
|
|
|
public function percentage(string $currency): float |
107
|
|
|
{ |
108
|
|
|
return $this->quotes[$this->findKey($currency)]['change_pct']; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $currency |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
private function findKey(string $currency): string |
117
|
|
|
{ |
118
|
|
|
$key = $this->source.$currency; |
119
|
|
|
if (!isset($this->quotes[$key])) { |
120
|
|
|
throw new \InvalidArgumentException( |
121
|
|
|
"{$currency} currency is not available. Did you put it in request?" |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $key; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|