1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ultraleet\CurrencyRates; |
4
|
|
|
|
5
|
|
|
use Ultraleet\CurrencyRates\Contracts\Result as ResultContract; |
6
|
|
|
use DateTime; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Encapsulates results from an API call. |
10
|
|
|
* |
11
|
|
|
* @property-read string $base The base currency |
12
|
|
|
* @property-read \DateTime $date The date of the resulting rates |
13
|
|
|
* @property-read array $rates An array of currency-rate pairs |
14
|
|
|
* @property-read array $converted Converted rates (currency-amount pairs) |
15
|
|
|
*/ |
16
|
|
|
class Result implements ResultContract |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The base currency the result was returned in. |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $base; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The date the result was generated for. |
26
|
|
|
* @var \DateTime |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
protected $date; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* All of the rates returned. |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $rates; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* All of the converted amounts. |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $converted; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Result constructor. |
45
|
|
|
* |
46
|
|
|
* @param string $base |
47
|
|
|
* @param \DateTime $date |
48
|
|
|
* @param array $rates |
49
|
|
|
*/ |
50
|
|
|
public function __construct($base, DateTime $date, $rates) |
51
|
|
|
{ |
52
|
|
|
$this->base = $base; |
53
|
|
|
$this->date = $date; |
54
|
|
|
$this->rates = $rates; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the base currency. |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getBase() |
63
|
|
|
{ |
64
|
|
|
return $this->base; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the date of the rates. |
69
|
|
|
* |
70
|
|
|
* @return DateTime |
71
|
|
|
*/ |
72
|
|
|
public function getDate() |
73
|
|
|
{ |
74
|
|
|
return $this->date; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get all requested currency rates. |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
public function getRates() |
83
|
|
|
{ |
84
|
|
|
return $this->rates; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get an individual rate by Currency code. |
89
|
|
|
* Will return null if currency is not found in the result. |
90
|
|
|
* |
91
|
|
|
* @param string $code |
92
|
|
|
* @return float|null |
93
|
|
|
*/ |
94
|
|
|
public function getRate($code) |
95
|
|
|
{ |
96
|
|
|
// Return 1 for base currency |
97
|
|
|
if ($code == $this->getBase()) { |
98
|
|
|
return 1.0; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (isset($this->rates[$code])) { |
102
|
|
|
return $this->rates[$code]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get all requested currency conversions. |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
public function getConverted() |
114
|
|
|
{ |
115
|
|
|
return $this->converted ? $this->converted : $this->rates; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set all requested currency conversions. |
120
|
|
|
* |
121
|
|
|
* @param array |
122
|
|
|
* @return self |
123
|
|
|
*/ |
124
|
|
|
public function setConverted($converted) |
125
|
|
|
{ |
126
|
|
|
$this->converted = $converted; |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Magic getter function for getting property values. |
133
|
|
|
* |
134
|
|
|
* @param string $name Property name |
135
|
|
|
* @return mixed |
136
|
|
|
*/ |
137
|
|
|
public function __get($name) |
138
|
|
|
{ |
139
|
|
|
$getter = 'get' . ucfirst($name); |
140
|
|
|
if (method_exists($this, $getter)) { |
141
|
|
|
return $this->$getter(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
trigger_error('Undefined property: ' . get_class() . '::$' . $name, E_USER_NOTICE); |
145
|
|
|
return null; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|