1
|
|
|
<?php |
2
|
|
|
/*************************************************************************************/ |
3
|
|
|
/* This file is part of the Thelia package. */ |
4
|
|
|
/* */ |
5
|
|
|
/* Copyright (c) OpenStudio */ |
6
|
|
|
/* email : [email protected] */ |
7
|
|
|
/* web : http://www.thelia.net */ |
8
|
|
|
/* */ |
9
|
|
|
/* For the full copyright and license information, please view the LICENSE.txt */ |
10
|
|
|
/* file that was distributed with this source code. */ |
11
|
|
|
/*************************************************************************************/ |
12
|
|
|
|
13
|
|
|
namespace Thelia\CurrencyConverter\Provider; |
14
|
|
|
|
15
|
|
|
use Thelia\CurrencyConverter\Exception\CurrencyNotFoundException; |
16
|
|
|
use Thelia\Math\Number; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
* Europen Central Bank provider. |
21
|
|
|
* |
22
|
|
|
* The European Central Bank provide all currencies quoted against the euro |
23
|
|
|
* The euro is the base currency |
24
|
|
|
* |
25
|
|
|
* Class ECBProvider |
26
|
|
|
* @package Thelia\CurrencyConverter\Provider |
27
|
|
|
* @author Manuel Raynaud <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class ECBProvider extends BaseProvider implements ProviderInterface |
30
|
|
|
{ |
31
|
|
|
protected $endPoint = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"; |
32
|
|
|
|
33
|
|
|
protected $data; |
34
|
|
|
|
35
|
|
|
public function __construct($loadWebService = true) |
36
|
|
|
{ |
37
|
|
|
if (true === $loadWebService) { |
38
|
|
|
$this->loadFromWebService(); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
private function loadFromWebService($endPoint = null) |
44
|
|
|
{ |
45
|
|
|
if (null !== $endPoint) { |
46
|
|
|
$this->endPoint = $endPoint; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$ch = curl_init($this->endPoint); |
50
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
51
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
52
|
|
|
|
53
|
|
|
$data = curl_exec($ch); |
54
|
|
|
|
55
|
|
|
$xml = new \SimpleXMLElement($data); |
56
|
|
|
|
57
|
|
|
$this->data = $xml->Cube[0]->Cube[0]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function loadFromXml($data) |
61
|
|
|
{ |
62
|
|
|
$xml = new \SimpleXMLElement($data); |
63
|
|
|
|
64
|
|
|
$this->data = $xml->Cube[0]->Cube[0]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getData() |
68
|
|
|
{ |
69
|
|
|
return $this->data; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function convert(Number $number) |
73
|
|
|
{ |
74
|
|
|
$rateFactor = $this->retrieveRateFactor(); |
75
|
|
|
|
76
|
|
|
if ($this->to === 'EUR') { |
77
|
|
|
return $number->multiply($rateFactor); |
78
|
|
|
} else { |
79
|
|
|
return $this->convertToOther($rateFactor, $number); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param \Thelia\Math\Number $rateFactor |
85
|
|
|
* @param \Thelia\Math\Number $number |
86
|
|
|
* @return \Thelia\Math\Number |
87
|
|
|
* @throws \Thelia\CurrencyConverter\Exception\CurrencyNotFoundException if the `to` currency is not support |
88
|
|
|
*/ |
89
|
|
|
private function convertToOther(Number $rateFactor, Number $number) |
90
|
|
|
{ |
91
|
|
|
$conversion = false; |
92
|
|
|
foreach ($this->data->Cube as $last) { |
93
|
|
|
$code = strtoupper($last["currency"]); |
94
|
|
|
|
95
|
|
|
if ($code === $this->to) { |
96
|
|
|
$rate = $rateFactor->multiply((string) $last['rate']); |
97
|
|
|
$conversion = $number->multiply($rate); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($conversion === false) { |
102
|
|
|
throw new CurrencyNotFoundException($this->to); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $conversion; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return \Thelia\Math\Number |
110
|
|
|
* @throws \Thelia\CurrencyConverter\Exception\CurrencyNotFoundException if the `from` currency is not support |
111
|
|
|
*/ |
112
|
|
|
private function retrieveRateFactor() |
113
|
|
|
{ |
114
|
|
|
$rateFactor = false; |
115
|
|
|
|
116
|
|
|
if ($this->from === 'EUR') { |
117
|
|
|
$rateFactor = new Number(1); |
118
|
|
|
} else { |
119
|
|
|
// Find the exchange rate for this currency |
120
|
|
|
foreach ($this->data->Cube as $last) { |
121
|
|
|
$code = strtoupper((string) $last["currency"]); |
122
|
|
|
|
123
|
|
|
if ($code === $this->from) { |
124
|
|
|
// Get the rate factor |
125
|
|
|
$rate = new Number((string) $last['rate']); |
126
|
|
|
$base = new Number(1); |
127
|
|
|
$rateFactor = $base->divide($rate); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if (false === $rateFactor) { |
133
|
|
|
throw new CurrencyNotFoundException($this->from); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $rateFactor; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|