Skip to content

Instantly share code, notes, and snippets.

@revenkroz
Last active December 9, 2020 15:58
Show Gist options
  • Save revenkroz/a077974d1f1adb34de4c1da263f12c90 to your computer and use it in GitHub Desktop.
Save revenkroz/a077974d1f1adb34de4c1da263f12c90 to your computer and use it in GitHub Desktop.
<?php declare(strict_types=1);
class Converter
{
private const URL = 'http://www.cbr.ru/scripts/XML_daily.asp';
private const CURRENCY_ID = 'R01239';
/** @var DOMXPath|null */
private $xpath = null;
public function __construct()
{
\libxml_use_internal_errors(true);
$doc = new DOMDocument();
$content = file_get_contents(self::URL);
$content = mb_convert_encoding($content, 'utf-8', 'windows-1251');
$doc->loadXML($content);
$this->xpath = new DOMXPath($doc);
}
public function convert(float $euro): float
{
$currency = $this->xpath->evaluate(sprintf('string(//Valute[@ID="%s"]/Value)', self::CURRENCY_ID));
$currency = (float) str_replace(',', '.', $currency);
return round($currency * $euro, 2);
}
}
$c = new Converter();
var_dump($c->convert(10.1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment