1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ultraleet\CurrencyRates; |
4
|
|
|
|
5
|
|
|
use Ultraleet\CurrencyRates\Contracts\Provider as ProviderContract; |
6
|
|
|
use Ultraleet\CurrencyRates\Contracts\Result as ResultContract; |
7
|
|
|
use Ultraleet\CurrencyRates\Exceptions\UnexpectedValueException; |
8
|
|
|
use DateTime; |
9
|
|
|
|
10
|
|
|
abstract class AbstractProvider implements ProviderContract |
11
|
|
|
{ |
12
|
|
|
protected $config = []; |
13
|
|
|
protected $base = 'EUR'; |
14
|
|
|
protected $targets = []; |
15
|
|
|
protected $date = null; |
16
|
|
|
protected $amount = 1; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Set the configuration array for this provider. |
20
|
|
|
* |
21
|
|
|
* @param array $config |
22
|
|
|
* @return self |
23
|
|
|
*/ |
24
|
|
|
public function configure($config) |
25
|
|
|
{ |
26
|
|
|
$this->config = $config; |
27
|
|
|
|
28
|
|
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Set the base currency. |
33
|
|
|
* |
34
|
|
|
* @param string $currency |
35
|
|
|
* @return self |
36
|
|
|
*/ |
37
|
|
|
public function base($currency) |
38
|
|
|
{ |
39
|
|
|
$this->base = $currency; |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Set the target currencies. |
46
|
|
|
* |
47
|
|
|
* @param array|string $currencies |
48
|
|
|
* @return self |
49
|
|
|
*/ |
50
|
|
|
public function target($currencies) |
51
|
|
|
{ |
52
|
|
|
if (!is_array($currencies)) { |
53
|
|
|
$currencies = [$currencies]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->targets = $currencies; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set the date. |
63
|
|
|
* |
64
|
|
|
* @param mixed $date Can be a string, a DateTime object, or null to unset. |
65
|
|
|
* @return self |
66
|
|
|
*/ |
67
|
|
|
public function date($date = null) |
68
|
|
|
{ |
69
|
|
|
if (is_string($date)) { |
70
|
|
|
$date = new DateTime($date); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->date = $date; |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set the amount of base currency to convert. |
80
|
|
|
* |
81
|
|
|
* @param float |
82
|
|
|
* @return self |
83
|
|
|
*/ |
84
|
|
|
public function amount($amount) |
85
|
|
|
{ |
86
|
|
|
$this->amount = $amount; |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Query the API. |
93
|
|
|
* |
94
|
|
|
* @return \Ultraleet\CurrencyRates\Contracts\Result |
95
|
|
|
*/ |
96
|
|
|
public function get() |
97
|
|
|
{ |
98
|
|
|
if ($this->date) { |
99
|
|
|
$result = $this->historical($this->date, $this->base, $this->targets); |
100
|
|
|
} else { |
101
|
|
|
$result = $this->latest($this->base, $this->targets); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Ensure the result object is valid. |
105
|
|
|
if (!$result instanceof ResultContract) { |
106
|
|
|
throw new UnexpectedValueException('Invalid result type'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// Perform conversion if requested. |
110
|
|
|
if ($this->amount !== 1) { |
111
|
|
|
$converted = $result->rates; |
112
|
|
|
|
113
|
|
|
foreach ($converted as $key => $value) { |
114
|
|
|
$converted[$key] = round($this->amount * $value, 2); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// Attach converted values to results. |
118
|
|
|
$result->setConverted($converted); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $result; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|