public function testProviderFactoryAddProvider() { $factory = new ProviderFactory(); $factory->addProvider($this->provider); $provider = $factory->get($this->providerName); $this->assertInstanceOf(self::PROVIDER_INTERFACE, $provider); }
/** * Convert value in different currency * @param ICurrency|string $from ICurrency object or currency code * @param ICurrency|string $to ICurrency object or currency code * @param float $value value to convert in currency $from * @param string|null $provider provider name * @param \DateTime|bool|null $rateDate Date for rate (default - today, false - any date) * @throws Exception\ProviderNotFoundException * @throws Exception\RateNotFoundException * @throws Exception\CurrencyNotFoundException * @return float */ public function convert($from, $to, $value, $provider = null, $rateDate = null) { $to = $this->getCurrency($to); $from = $this->getCurrency($from); $providers = $provider === null ? $this->providerFactory->getAll() : [$this->providerFactory->get($provider)]; $providers = array_filter($providers); if (!count($providers)) { throw new ProviderNotFoundException($provider); } $date = $rateDate === false ? null : ($rateDate instanceof \DateTime ? $rateDate : new \DateTime()); $date && $date->setTime(0, 0, 0); $foundValue = null; $errorParams = ['currency' => null, 'provider' => null]; foreach ($providers as $provider) { /** @var ICurrencyRateProvider $provider */ if (!$provider->getBaseCurrency()) { throw new ProviderNotFoundException($provider->getName()); } $valueBase = $this->getValueBase($value, $from, $provider, $date); if ($valueBase === null) { $errorParams['currency'] = $from; $errorParams['provider'] = $provider; continue; } $toRate = $this->getRate($to, $provider, $date); if ($toRate === null) { $errorParams['currency'] = $to; $errorParams['provider'] = $provider; continue; } $foundValue = $toRate * $valueBase; if ($foundValue !== null) { break; } } if ($foundValue === null) { throw new RateNotFoundException($errorParams['currency'], $errorParams['provider'], $date); } return $foundValue; }