Last active
December 9, 2020 15:58
-
-
Save revenkroz/a077974d1f1adb34de4c1da263f12c90 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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