diff --git a/Adapter/AdapterFactory.php b/Adapter/AdapterFactory.php
index 025ff9f..66ba95b 100644
--- a/Adapter/AdapterFactory.php
+++ b/Adapter/AdapterFactory.php
@@ -106,14 +106,14 @@ public function createOerAdapter($adapterClass = null)
}
/**
- * Create an YahooCurrencyAdapter.
+ * Create an AlphaCurrencyAdapter.
*
- * @return Lexik\Bundle\CurrencyBundle\Adapter\YahooCurrencyAdapter
+ * @return Lexik\Bundle\CurrencyBundle\Adapter\AlphaCurrencyAdapter
*/
- public function createYahooAdapter($adapterClass = null)
+ public function createAlphaAdapter($adapterClass = null)
{
if (null == $adapterClass) {
- $adapterClass = 'Lexik\Bundle\CurrencyBundle\Adapter\YahooCurrencyAdapter';
+ $adapterClass = 'Lexik\Bundle\CurrencyBundle\Adapter\AlphaCurrencyAdapter';
}
return $this->create($adapterClass);
diff --git a/Adapter/AlphaCurrencyAdapter.php b/Adapter/AlphaCurrencyAdapter.php
new file mode 100644
index 0000000..9a20101
--- /dev/null
+++ b/Adapter/AlphaCurrencyAdapter.php
@@ -0,0 +1,112 @@
+
+ */
+class AlphaCurrencyAdapter extends AbstractCurrencyAdapter
+{
+ /**
+ * @var string
+ */
+ private $apiKey;
+
+ /**
+ * @var array
+ */
+ private $currencyCodes = array();
+
+ /**
+ * Set the Alpha Vantage API key.
+ *
+ * @param string $apiKey
+ */
+ public function setApiKey($apiKey)
+ {
+ $this->apiKey = $apiKey;
+ }
+
+ /**
+ * Init object storage
+ */
+ public function attachAll()
+ {
+ foreach ($this->managedCurrencies as $managedCurrency) {
+ $this->addCurrency($managedCurrency);
+ }
+
+ $defaultRate = 1;
+
+ // Add default currency (euro in this example)
+ $defaultCurrency = new $this->currencyClass;
+ $defaultCurrency->setCode('EUR');
+ $defaultCurrency->setRate($defaultRate);
+
+ $this[$defaultCurrency->getCode()] = $defaultCurrency;
+
+ $currencies = [];
+ // Build query
+ foreach ($this->currencyCodes as $index=>$currencyCode) {
+ $currencies[$currencyCode] = $this->getExchangeRate($currencyCode);
+ }
+
+ foreach ($currencies as $code => $rate) {
+ if (in_array($code, $this->managedCurrencies)) { // you can check if the currency is in the managed currencies
+ $currency = new $this->currencyClass;
+ $currency->setCode($code);
+ $currency->setRate($rate);
+
+ $this[$currency->getCode()] = $currency;
+ }
+ }
+
+ // get the default rate from the default currency defined in the configuration
+ if (isset($this[$this->defaultCurrency])) {
+ $defaultRate = $this[$this->defaultCurrency]->getRate();
+ }
+
+ // convert rates according to the default one.
+ $this->convertAll($defaultRate);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getIdentifier()
+ {
+ return 'alpha';
+ }
+
+ /**
+ * Add currency to the query
+ *
+ * @param $code
+ */
+ private function addCurrency($code)
+ {
+ $this->currencyCodes[] = $code;
+ }
+
+ /**
+ * @param $currency
+ * @return float
+ */
+ private function getExchangeRate($currency)
+ {
+ if (!$this->apiKey) {
+ throw new \InvalidArgumentException('ALPHA_API_KEY must be set in order to use AlphaCurrencyAdapter');
+ }
+
+ $url = sprintf('https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=%s&to_currency=%s&apikey=%s', $this->defaultCurrency, $currency, $this->apiKey);
+ $json = file_get_contents($url);
+ $data = json_decode($json, true);
+
+ return $data['Realtime Currency Exchange Rate']['5. Exchange Rate'];
+ }
+
+}
\ No newline at end of file
diff --git a/Adapter/YahooCurrencyAdapter.php b/Adapter/YahooCurrencyAdapter.php
deleted file mode 100644
index 5354574..0000000
--- a/Adapter/YahooCurrencyAdapter.php
+++ /dev/null
@@ -1,134 +0,0 @@
-
- */
-class YahooCurrencyAdapter extends AbstractCurrencyAdapter
-{
- /**
- * @var string
- */
- private $yahooUrl;
-
- /**
- * @var array
- */
- private $currencyCodes = array();
-
-
- /**
- * Set the Yahoo! url.
- *
- * @param string $url
- */
- public function setYahooUrl($url)
- {
- $this->yahooUrl = $url;
- }
-
- /**
- * Init object storage
- */
- public function attachAll()
- {
- foreach ($this->managedCurrencies as $managedCurrency) {
- $this->addCurrency($managedCurrency);
- }
-
- $defaultRate = 1;
-
- // Add default currency (euro in this example)
- $euro = new $this->currencyClass;
- $euro->setCode('EUR');
- $euro->setRate($defaultRate);
-
- $this[$euro->getCode()] = $euro;
-
- // Build YQL query
- $strCodes = '';
- foreach ($this->currencyCodes as $index=>$currencyCode) {
- $strCodes .= "'EUR".$currencyCode."'";
- if ($index != count($this->currencyCodes) - 1) {
- $strCodes .= ", ";
- }
- }
-
- $yqlQuery = "select id,Rate from yahoo.finance.xchange where pair in (".$strCodes.")";
-
- $args = array(
- 'q' => $yqlQuery,
- 'format' => "json",
- 'env' => "store://datatables.org/alltableswithkeys",
- );
-
- $yqlQueryURL = $this->yahooUrl
- . "?q=" . urlencode($yqlQuery)
- . "&format=json"
- . "&env=store://datatables.org/alltableswithkeys";
-
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $yqlQueryURL);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
- $json = curl_exec($ch);
-
- // Convert JSON response to PHP object
- $data = json_decode($json);
- $results = $data->query->results->rate;
-
- // Check if query was okay and result is given
- if (is_null($results)) {
- new \RuntimeException('YQL query failed!');
- }
-
- $currencies = array();
-
- foreach ($results as $row) {
- $code = substr($row->id, 3);
- $rate = $row->Rate;
-
- $currencies[$code] = $rate;
- }
-
- foreach ($currencies as $code => $rate) {
- if (in_array($code, $this->managedCurrencies)) { // you can check if the currency is in the managed currencies
- $currency = new $this->currencyClass;
- $currency->setCode($code);
- $currency->setRate($rate);
-
- $this[$currency->getCode()] = $currency;
- }
- }
-
- // get the default rate from the default currency defined in the configuration
- if (isset($this[$this->defaultCurrency])) {
- $defaultRate = $this[$this->defaultCurrency]->getRate();
- }
-
- // convert rates according to the default one.
- $this->convertAll($defaultRate);
- }
-
- /**
- * {@inheritdoc}
- */
- public function getIdentifier()
- {
- return 'yahoo';
- }
-
- /**
- * Add currency to the query
- *
- * @param $code
- */
- private function addCurrency($code) {
- $this->currencyCodes[] = $code;
- }
-
-}
\ No newline at end of file
diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php
index 15fb221..b2106e1 100644
--- a/DependencyInjection/Configuration.php
+++ b/DependencyInjection/Configuration.php
@@ -74,9 +74,9 @@ public function getConfigTreeBuilder()
->defaultValue(null)
->end()
- ->scalarNode('yahoo_url')
+ ->scalarNode('alpha_api_key')
->cannotBeEmpty()
- ->defaultValue('https://query.yahooapis.com/v1/public/yql')
+ ->defaultValue(null)
->end()
->end()
diff --git a/DependencyInjection/LexikCurrencyExtension.php b/DependencyInjection/LexikCurrencyExtension.php
index 3bca23d..f3c29e7 100644
--- a/DependencyInjection/LexikCurrencyExtension.php
+++ b/DependencyInjection/LexikCurrencyExtension.php
@@ -30,7 +30,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('lexik_currency.ecb_url', $config['ecb_url']);
$container->setParameter('lexik_currency.oer_url', $config['oer_url']);
$container->setParameter('lexik_currency.oer_app_id', $config['oer_app_id']);
- $container->setParameter('lexik_currency.yahoo_url', $config['yahoo_url']);
+ $container->setParameter('lexik_currency.alpha_api_key', $config['alpha_api_key']);
$container->setParameter('lexik_currency.decimal_part.precision', $config['decimal_part']['precision']);
$container->setParameter('lexik_currency.decimal_part.round_mode', $config['decimal_part']['round_mode']);
diff --git a/Resources/config/adapters.xml b/Resources/config/adapters.xml
index 44446a4..6ffd61d 100644
--- a/Resources/config/adapters.xml
+++ b/Resources/config/adapters.xml
@@ -10,7 +10,7 @@
Lexik\Bundle\CurrencyBundle\Adapter\AbstractCurrencyAdapter
Lexik\Bundle\CurrencyBundle\Adapter\EcbCurrencyAdapter
Lexik\Bundle\CurrencyBundle\Adapter\OerCurrencyAdapter
- Lexik\Bundle\CurrencyBundle\Adapter\YahooCurrencyAdapter
+ Lexik\Bundle\CurrencyBundle\Adapter\AlphaCurrencyAdapter
Lexik\Bundle\CurrencyBundle\Adapter\DoctrineCurrencyAdapter
default
@@ -56,12 +56,12 @@
-
-
-
- %lexik_currency.yahoo_url%
+
+
+
+ %lexik_currency.alpha_api_key%
-
+
diff --git a/Tests/Unit/Adapter/AdapterFactoryTest.php b/Tests/Unit/Adapter/AdapterFactoryTest.php
index 72e0449..ff274a8 100644
--- a/Tests/Unit/Adapter/AdapterFactoryTest.php
+++ b/Tests/Unit/Adapter/AdapterFactoryTest.php
@@ -29,12 +29,12 @@ public function testCreateEcbAdapter()
$this->assertEquals(0, count($adapter));
}
- public function testCreateYahooAdapter()
+ public function testCreateAlphaAdapter()
{
$factory = new AdapterFactory($this->doctrine, 'EUR', array('EUR', 'USD'), self::CURRENCY_ENTITY);
- $adapter = $factory->createYahooAdapter();
+ $adapter = $factory->createAlphaAdapter();
- $this->assertInstanceOf('Lexik\Bundle\CurrencyBundle\Adapter\YahooCurrencyAdapter', $adapter);
+ $this->assertInstanceOf('Lexik\Bundle\CurrencyBundle\Adapter\AlphaCurrencyAdapter', $adapter);
$this->assertEquals('EUR', $adapter->getDefaultCurrency());
$this->assertEquals(array('EUR', 'USD'), $adapter->getManagedCurrencies());
$this->assertEquals(0, count($adapter));