1
+ <?php
2
+
3
+ namespace Lexik \Bundle \CurrencyBundle \Adapter ;
4
+
5
+ use Lexik \Bundle \CurrencyBundle \Exception \CurrencyNotFoundException ;
6
+
7
+ /**
8
+ * Yahoo! Adapter
9
+ *
10
+ * @author Jonas Dambacher <[email protected] >
11
+ */
12
+ class YahooCurrencyAdapter extends AbstractCurrencyAdapter
13
+ {
14
+ /**
15
+ * @var string
16
+ */
17
+ private $ yahooUrl ;
18
+
19
+ /**
20
+ * @var array
21
+ */
22
+ private $ currencyCodes = array ();
23
+
24
+
25
+ /**
26
+ * Set the Yahoo! url.
27
+ *
28
+ * @param string $url
29
+ */
30
+ public function setYahooUrl ($ url )
31
+ {
32
+ $ this ->yahooUrl = $ url ;
33
+ }
34
+
35
+ /**
36
+ * Init object storage
37
+ */
38
+ public function attachAll ()
39
+ {
40
+ foreach ($ this ->managedCurrencies as $ managedCurrency ) {
41
+ $ this ->addCurrency ($ managedCurrency );
42
+ }
43
+
44
+ $ defaultRate = 1 ;
45
+
46
+ // Add default currency (euro in this example)
47
+ $ euro = new $ this ->currencyClass ;
48
+ $ euro ->setCode ('EUR ' );
49
+ $ euro ->setRate ($ defaultRate );
50
+
51
+ $ this [$ euro ->getCode ()] = $ euro ;
52
+
53
+ // Build YQL query
54
+ $ strCodes = '' ;
55
+ foreach ($ this ->currencyCodes as $ index =>$ currencyCode ) {
56
+ $ strCodes .= "'EUR " .$ currencyCode ."' " ;
57
+ if ($ index != count ($ this ->currencyCodes ) - 1 ) {
58
+ $ strCodes .= ", " ;
59
+ }
60
+ }
61
+
62
+ $ yqlQuery = "select id,Rate from yahoo.finance.xchange where pair in ( " .$ strCodes .") " ;
63
+
64
+ $ args = array (
65
+ 'q ' => $ yqlQuery ,
66
+ 'format ' => "json " ,
67
+ 'env ' => "store://datatables.org/alltableswithkeys " ,
68
+ );
69
+
70
+ $ yqlQueryURL = $ this ->yahooUrl
71
+ . "?q= " . urlencode ($ yqlQuery )
72
+ . "&format=json "
73
+ . "&env=store://datatables.org/alltableswithkeys " ;
74
+
75
+ $ ch = curl_init ();
76
+ curl_setopt ($ ch , CURLOPT_URL , $ yqlQueryURL );
77
+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , TRUE );
78
+ $ json = curl_exec ($ ch );
79
+
80
+ // Convert JSON response to PHP object
81
+ $ data = json_decode ($ json );
82
+ $ results = $ data ->query ->results ->rate ;
83
+
84
+ // Check if query was okay and result is given
85
+ if (is_null ($ results )) {
86
+ new \RuntimeException ('YQL query failed! ' );
87
+ }
88
+
89
+ $ currencies = array ();
90
+
91
+ foreach ($ results as $ row ) {
92
+ $ code = substr ($ row ->id , 3 );
93
+ $ rate = $ row ->Rate ;
94
+
95
+ $ currencies [$ code ] = $ rate ;
96
+ }
97
+
98
+ foreach ($ currencies as $ code => $ rate ) {
99
+ if (in_array ($ code , $ this ->managedCurrencies )) { // you can check if the currency is in the managed currencies
100
+ $ currency = new $ this ->currencyClass ;
101
+ $ currency ->setCode ($ code );
102
+ $ currency ->setRate ($ rate );
103
+
104
+ $ this [$ currency ->getCode ()] = $ currency ;
105
+ }
106
+ }
107
+
108
+ // get the default rate from the default currency defined in the configuration
109
+ if (isset ($ this [$ this ->defaultCurrency ])) {
110
+ $ defaultRate = $ this [$ this ->defaultCurrency ]->getRate ();
111
+ }
112
+
113
+ // convert rates according to the default one.
114
+ $ this ->convertAll ($ defaultRate );
115
+ }
116
+
117
+ /**
118
+ * {@inheritdoc}
119
+ */
120
+ public function getIdentifier ()
121
+ {
122
+ return 'yahoo ' ;
123
+ }
124
+
125
+ /**
126
+ * Add currency to the query
127
+ *
128
+ * @param $code
129
+ */
130
+ private function addCurrency ($ code ) {
131
+ $ this ->currencyCodes [] = $ code ;
132
+ }
133
+
134
+ }
0 commit comments