1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Orkhanahmadov\Currencylayer\Data; |
4
|
|
|
|
5
|
|
|
use Carbon\CarbonImmutable; |
6
|
|
|
|
7
|
|
|
class Timeframe |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
private $source; |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private $quotes; |
17
|
|
|
/** |
18
|
|
|
* @var \DateTimeImmutable |
19
|
|
|
*/ |
20
|
|
|
private $startDate; |
21
|
|
|
/** |
22
|
|
|
* @var \DateTimeImmutable |
23
|
|
|
*/ |
24
|
|
|
private $endDate; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Timeframe constructor. |
28
|
|
|
* |
29
|
|
|
* @param array $data |
30
|
|
|
* |
31
|
|
|
* @throws \Exception |
32
|
|
|
*/ |
33
|
|
|
public function __construct(array $data) |
34
|
|
|
{ |
35
|
|
|
$this->source = $data['source']; |
36
|
|
|
$this->quotes = $data['quotes']; |
37
|
|
|
$this->startDate = new CarbonImmutable($data['start_date']); |
38
|
|
|
$this->endDate = new CarbonImmutable($data['end_date']); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getSource(): string |
45
|
|
|
{ |
46
|
|
|
return $this->source; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function getQuotes(): array |
53
|
|
|
{ |
54
|
|
|
return $this->quotes; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return \DateTimeImmutable |
59
|
|
|
*/ |
60
|
|
|
public function getStartDate(): \DateTimeImmutable |
61
|
|
|
{ |
62
|
|
|
return $this->startDate; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return \DateTimeImmutable |
67
|
|
|
*/ |
68
|
|
|
public function getEndDate(): \DateTimeImmutable |
69
|
|
|
{ |
70
|
|
|
return $this->endDate; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param \DateTimeImmutable|string $date |
75
|
|
|
* |
76
|
|
|
* @throws \Exception |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function quotes($date): array |
81
|
|
|
{ |
82
|
|
|
$date = $date instanceof \DateTimeImmutable ? |
83
|
|
|
$date->format('Y-m-d') : |
84
|
|
|
(new CarbonImmutable($date))->format('Y-m-d'); |
85
|
|
|
|
86
|
|
|
if (!isset($this->quotes[$date])) { |
87
|
|
|
throw new \InvalidArgumentException( |
88
|
|
|
"Quotes for {$date} is not available. Did you put it in request?" |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->quotes[$date]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $name |
97
|
|
|
* @param $arguments |
98
|
|
|
* |
99
|
|
|
* @return float |
100
|
|
|
*/ |
101
|
|
|
public function __call(string $name, $arguments): float |
102
|
|
|
{ |
103
|
|
|
$key = $this->source.$name; |
104
|
|
|
if (!isset($arguments[0]) || !isset($this->quotes[$arguments[0]]) || !isset($this->quotes[$arguments[0]][$key])) { |
105
|
|
|
throw new \InvalidArgumentException( |
106
|
|
|
"{$name} currency or its argument is invalid. You sure you calling correct currency with correct date?" |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->quotes[$arguments[0]][$key]; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|