diff --git a/composer.json b/composer.json index 72817a3..3a72eca 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ "phpstan/phpstan": "^1.10", "phpstan/phpstan-deprecation-rules": "^1.1.3", "phpstan/phpstan-strict-rules": "^1.5", + "strictphp/http-clients": "^0.1.4", "tracy/tracy": "^2.10" }, "autoload": { @@ -41,7 +42,7 @@ "guzzlehttp/psr7": "Minimum ^2.4 for guzzle.", "h4kuna/dir": "If you want to use build-in factory.", "nette/caching": "If you have not own PSR-6 implementation.", - "ext-simplexml": "If you want to use h4kuna\\Exchange\\Driver\\Ecb." + "ext-simplexml": "If you want to use h4kuna\\Exchange\\Driver\\Ecb or h4kuna\\Exchange\\Driver\\RB." }, "autoload-dev": { "psr-4": { diff --git a/src/Driver/Ecb/Day.php b/src/Driver/Ecb/Day.php index 4350cf0..e3bf0ba 100644 --- a/src/Driver/Ecb/Day.php +++ b/src/Driver/Ecb/Day.php @@ -43,10 +43,7 @@ public function getTimeZone(): DateTimeZone public function createSourceData(ResponseInterface $response): SourceData { - $xml = simplexml_load_string($response->getBody()->getContents()); - if ($xml === false) { - throw new Exchange\Exceptions\InvalidStateException('Invalid source xml.'); - } + $xml = Exchange\Utils::createSimpleXMLElement($response); // including EUR $eur = $xml->Cube->Cube->addChild('Cube'); diff --git a/src/Driver/RB/Day.php b/src/Driver/RB/Day.php index 4743215..f80c3a1 100644 --- a/src/Driver/RB/Day.php +++ b/src/Driver/RB/Day.php @@ -52,12 +52,7 @@ public function getTimeZone(): DateTimeZone public function createSourceData(ResponseInterface $response): SourceData { - $data = $response->getBody()->getContents(); - $xml = simplexml_load_string($data); - - if ($xml === false) { - throw new InvalidStateException('Invalid source xml.'); - } + $xml = Utils::createSimpleXMLElement($response); // add CZK $czk = $xml->exchangeRateList->exchangeRates->addChild('exchangeRate'); diff --git a/src/Exceptions/XmlResponseFailedException.php b/src/Exceptions/XmlResponseFailedException.php new file mode 100644 index 0000000..0c43322 --- /dev/null +++ b/src/Exceptions/XmlResponseFailedException.php @@ -0,0 +1,11 @@ +getBody()->getContents()); + + if ($xml === false) { + throw new XmlResponseFailedException((string) $response->getStatusCode()); + } + + return $xml; + } + + public static function createTimeZone(string|DateTimeZone $timeZone): DateTimeZone { return is_string($timeZone) ? new DateTimeZone($timeZone) : $timeZone; diff --git a/tests/Fixtures/rb.failed.no.xml.html b/tests/Fixtures/rb.failed.no.xml.html new file mode 100644 index 0000000..2f73602 --- /dev/null +++ b/tests/Fixtures/rb.failed.no.xml.html @@ -0,0 +1,221 @@ + + + + + + Banka inspirovaná klienty | Raiffeisenbank + + + + + + + + + + + + + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + +
+ +
+
+
+
+

Omlouváme se, ale náš web je dočasně nedostupný.

+

+ Je nám líto, právě probíhá plánovaná odstávka systémů banky. Z toho důvodu jsou naše webové stránky, internetové i mobilní bankovnictví a další služby dočasně nedostupné. + Předpokládaný návrat k normálu je   . +

+

+ Platby kartami a výběry z bankomatů budou po dobu odstávky funkční.
+ Vklady na bankomatech provedené v době odstávky budou na účet připsány po odstávce. +

+

Děkujeme za pochopení.

+
+
+
+
+ + + + diff --git a/tests/src/UtilsTest.php b/tests/src/UtilsTest.php index 57c4ea5..a395d96 100644 --- a/tests/src/UtilsTest.php +++ b/tests/src/UtilsTest.php @@ -9,6 +9,8 @@ use DateTimeImmutable; use DateTimeInterface; use DateTimeZone; +use GuzzleHttp\Psr7\Response; +use h4kuna\Exchange\Exceptions\XmlResponseFailedException; use h4kuna\Exchange\Utils; use Tester\Assert; use Tester\TestCase; @@ -177,6 +179,15 @@ public function assertToImmutable( Assert::same($expected, Utils::toImmutable($date, new DateTimeZone('Europe/Prague'))?->format(DateTimeInterface::RFC3339)); } + + public function testCreateSimpleXMLElement(): void + { + $response = new Response(403, body: (string) file_get_contents(__DIR__ . '/../Fixtures/rb.failed.no.xml.html')); + + Assert::exception(static fn ( + ) => Utils::createSimpleXMLElement($response), XmlResponseFailedException::class, '403'); + } + } (new UtilsTest())->run();