From a8d3a26682f13745dbb9aabce36f108ca6a210a3 Mon Sep 17 00:00:00 2001 From: Jonas Dambacher Date: Wed, 8 Nov 2017 16:09:51 +0100 Subject: [PATCH 1/2] Added AlphaVantage adapter --- Adapter/AdapterFactory.php | 14 +++ Adapter/AlphaCurrencyAdapter.php | 112 ++++++++++++++++++ DependencyInjection/Configuration.php | 5 + .../LexikCurrencyExtension.php | 1 + Resources/config/adapters.xml | 9 ++ Tests/Unit/Adapter/AdapterFactoryTest.php | 11 ++ 6 files changed, 152 insertions(+) create mode 100644 Adapter/AlphaCurrencyAdapter.php diff --git a/Adapter/AdapterFactory.php b/Adapter/AdapterFactory.php index 025ff9f..fed981e 100644 --- a/Adapter/AdapterFactory.php +++ b/Adapter/AdapterFactory.php @@ -118,4 +118,18 @@ public function createYahooAdapter($adapterClass = null) return $this->create($adapterClass); } + + /** + * Create an AlphaCurrencyAdapter. + * + * @return Lexik\Bundle\CurrencyBundle\Adapter\AlphaCurrencyAdapter + */ + public function createAlphaAdapter($adapterClass = null) + { + if (null == $adapterClass) { + $adapterClass = 'Lexik\Bundle\CurrencyBundle\Adapter\AlphaCurrencyAdapter'; + } + + return $this->create($adapterClass); + } } \ No newline at end of file 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/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 15fb221..c0e6af0 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -79,6 +79,11 @@ public function getConfigTreeBuilder() ->defaultValue('https://query.yahooapis.com/v1/public/yql') ->end() + ->scalarNode('alpha_api_key') + ->cannotBeEmpty() + ->defaultValue(null) + ->end() + ->end() ; diff --git a/DependencyInjection/LexikCurrencyExtension.php b/DependencyInjection/LexikCurrencyExtension.php index 3bca23d..2cfd9a5 100644 --- a/DependencyInjection/LexikCurrencyExtension.php +++ b/DependencyInjection/LexikCurrencyExtension.php @@ -31,6 +31,7 @@ public function load(array $configs, ContainerBuilder $container) $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..d7ffee8 100644 --- a/Resources/config/adapters.xml +++ b/Resources/config/adapters.xml @@ -11,6 +11,7 @@ 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 @@ -63,6 +64,14 @@ + + + + + %lexik_currency.alpha_api_key% + + + diff --git a/Tests/Unit/Adapter/AdapterFactoryTest.php b/Tests/Unit/Adapter/AdapterFactoryTest.php index 72e0449..2d63d33 100644 --- a/Tests/Unit/Adapter/AdapterFactoryTest.php +++ b/Tests/Unit/Adapter/AdapterFactoryTest.php @@ -40,6 +40,17 @@ public function testCreateYahooAdapter() $this->assertEquals(0, count($adapter)); } + public function testCreateAlphaAdapter() + { + $factory = new AdapterFactory($this->doctrine, 'EUR', array('EUR', 'USD'), self::CURRENCY_ENTITY); + $adapter = $factory->createAlphaAdapter(); + + $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)); + } + public function testCreateDoctrineAdapter() { $em = $this->getEntityManager(); From c0b5ab8666f6a488afa37e51baa0f864162ee044 Mon Sep 17 00:00:00 2001 From: Jonas Dambacher Date: Wed, 8 Nov 2017 16:16:37 +0100 Subject: [PATCH 2/2] Removed the deprecated Yahoo adapter Fixes #54 - lexik:currency:import yahoo not working --- Adapter/AdapterFactory.php | 14 -- Adapter/YahooCurrencyAdapter.php | 134 ------------------ DependencyInjection/Configuration.php | 5 - .../LexikCurrencyExtension.php | 1 - Resources/config/adapters.xml | 9 -- Tests/Unit/Adapter/AdapterFactoryTest.php | 11 -- 6 files changed, 174 deletions(-) delete mode 100644 Adapter/YahooCurrencyAdapter.php diff --git a/Adapter/AdapterFactory.php b/Adapter/AdapterFactory.php index fed981e..66ba95b 100644 --- a/Adapter/AdapterFactory.php +++ b/Adapter/AdapterFactory.php @@ -105,20 +105,6 @@ public function createOerAdapter($adapterClass = null) return $this->create($adapterClass); } - /** - * Create an YahooCurrencyAdapter. - * - * @return Lexik\Bundle\CurrencyBundle\Adapter\YahooCurrencyAdapter - */ - public function createYahooAdapter($adapterClass = null) - { - if (null == $adapterClass) { - $adapterClass = 'Lexik\Bundle\CurrencyBundle\Adapter\YahooCurrencyAdapter'; - } - - return $this->create($adapterClass); - } - /** * Create an AlphaCurrencyAdapter. * 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 c0e6af0..b2106e1 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -74,11 +74,6 @@ public function getConfigTreeBuilder() ->defaultValue(null) ->end() - ->scalarNode('yahoo_url') - ->cannotBeEmpty() - ->defaultValue('https://query.yahooapis.com/v1/public/yql') - ->end() - ->scalarNode('alpha_api_key') ->cannotBeEmpty() ->defaultValue(null) diff --git a/DependencyInjection/LexikCurrencyExtension.php b/DependencyInjection/LexikCurrencyExtension.php index 2cfd9a5..f3c29e7 100644 --- a/DependencyInjection/LexikCurrencyExtension.php +++ b/DependencyInjection/LexikCurrencyExtension.php @@ -30,7 +30,6 @@ 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 d7ffee8..6ffd61d 100644 --- a/Resources/config/adapters.xml +++ b/Resources/config/adapters.xml @@ -10,7 +10,6 @@ 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 @@ -57,14 +56,6 @@ - - - - %lexik_currency.yahoo_url% - - - - diff --git a/Tests/Unit/Adapter/AdapterFactoryTest.php b/Tests/Unit/Adapter/AdapterFactoryTest.php index 2d63d33..ff274a8 100644 --- a/Tests/Unit/Adapter/AdapterFactoryTest.php +++ b/Tests/Unit/Adapter/AdapterFactoryTest.php @@ -29,17 +29,6 @@ public function testCreateEcbAdapter() $this->assertEquals(0, count($adapter)); } - public function testCreateYahooAdapter() - { - $factory = new AdapterFactory($this->doctrine, 'EUR', array('EUR', 'USD'), self::CURRENCY_ENTITY); - $adapter = $factory->createYahooAdapter(); - - $this->assertInstanceOf('Lexik\Bundle\CurrencyBundle\Adapter\YahooCurrencyAdapter', $adapter); - $this->assertEquals('EUR', $adapter->getDefaultCurrency()); - $this->assertEquals(array('EUR', 'USD'), $adapter->getManagedCurrencies()); - $this->assertEquals(0, count($adapter)); - } - public function testCreateAlphaAdapter() { $factory = new AdapterFactory($this->doctrine, 'EUR', array('EUR', 'USD'), self::CURRENCY_ENTITY);