setMethod(RequestMethod::GET); $xml = new \SimpleXMLElement(Rest::request($request)); if (!isset($xml->Cube)) { throw new \Exception('Invalid xml path'); } $node = $xml->Cube->Cube->Cube; self::$ecbCurrencies = []; foreach ($node as $key => $value) { self::$ecbCurrencies[strtoupper((string) $value->attributes()['currency'])] = (float) $value->attributes()['rate']; } } return self::$ecbCurrencies; } /** * Convert to EUR * * @param float $value Value to convert * @param string $from Input currency * * @return float * * @since 1.0.0 */ public static function fromToEur(float $value, string $from) : float { $currencies = self::getEcbEuroRates(); $from = strtoupper($from); if (!isset($currencies[$from])) { throw new \InvalidArgumentException('Currency doesn\'t exists'); } return $value / $currencies[$from]; } /** * Convert currency based on ECB reates * * @param float $value Value to convert * @param string $from Input currency * @param string $to Output currency * * @return float * * @since 1.0.0 */ public static function convertCurrency(float $value, string $from, string $to) : float { $currencies = self::getEcbEuroRates(); $from = strtoupper($from); $to = strtoupper($to); if ((!isset($currencies[$from]) && $from !== ISO4217CharEnum::_EUR) || (!isset($currencies[$to]) && $to !== ISO4217CharEnum::_EUR)) { throw new \InvalidArgumentException('Currency doesn\'t exists'); } if ($from !== ISO4217CharEnum::_EUR) { $value /= $currencies[$from]; } return $to === ISO4217CharEnum::_EUR ? $value : $value * $currencies[$to]; } }