vendor/florianv/swap/src/Swap.php line 52

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Swap.
  5.  *
  6.  * (c) Florian Voutzinos <florian@voutzinos.com>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Swap;
  12. use Exchanger\Contract\ExchangeRateProvider;
  13. use Exchanger\ExchangeRateQueryBuilder;
  14. use Exchanger\Contract\ExchangeRate as ExchangeRateContract;
  15. /**
  16.  * Swap is an easy to use facade to retrieve exchange rates from various services.
  17.  *
  18.  * @author Florian Voutzinos <florian@voutzinos.com>
  19.  */
  20. class Swap
  21. {
  22.     /**
  23.      * The exchange rate provider.
  24.      *
  25.      * @var ExchangeRateProvider
  26.      */
  27.     private $exchangeRateProvider;
  28.     /**
  29.      * Creates a new Swap.
  30.      *
  31.      * @param ExchangeRateProvider $exchangeRateProvider
  32.      */
  33.     public function __construct(ExchangeRateProvider $exchangeRateProvider)
  34.     {
  35.         $this->exchangeRateProvider $exchangeRateProvider;
  36.     }
  37.     /**
  38.      * Quotes a currency pair.
  39.      *
  40.      * @param string $currencyPair The currency pair like "EUR/USD"
  41.      * @param array  $options      An array of query options
  42.      *
  43.      * @return ExchangeRateContract
  44.      */
  45.     public function latest(string $currencyPair, array $options = []): ExchangeRateContract
  46.     {
  47.         return $this->quote($currencyPairnull$options);
  48.     }
  49.     /**
  50.      * Quotes a currency pair.
  51.      *
  52.      * @param string             $currencyPair The currency pair like "EUR/USD"
  53.      * @param \DateTimeInterface $date         An optional date for historical rates
  54.      * @param array              $options      An array of query options
  55.      *
  56.      * @return ExchangeRateContract
  57.      */
  58.     public function historical(string $currencyPair, \DateTimeInterface $date, array $options = []): ExchangeRateContract
  59.     {
  60.         return $this->quote($currencyPair$date$options);
  61.     }
  62.     /**
  63.      * Quotes a currency pair.
  64.      *
  65.      * @param string             $currencyPair The currency pair like "EUR/USD"
  66.      * @param \DateTimeInterface $date         An optional date for historical rates
  67.      * @param array              $options      An array of query options
  68.      *
  69.      * @return ExchangeRateContract
  70.      */
  71.     private function quote(string $currencyPair, \DateTimeInterface $date null, array $options = []): ExchangeRateContract
  72.     {
  73.         $exchangeQueryBuilder = new ExchangeRateQueryBuilder($currencyPair);
  74.         if (null !== $date) {
  75.             $exchangeQueryBuilder->setDate($date);
  76.         }
  77.         foreach ($options as $name => $value) {
  78.             $exchangeQueryBuilder->addOption($name$value);
  79.         }
  80.         $query $exchangeQueryBuilder->build();
  81.         return $this->exchangeRateProvider->getExchangeRate($query);
  82.     }
  83. }