Skip to content

Commit

Permalink
fix(SimpleXMLElement): factory throw unify exception
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Jul 22, 2024
1 parent f5ad474 commit ee1fe9e
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 11 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": {
Expand Down
5 changes: 1 addition & 4 deletions src/Driver/Ecb/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
7 changes: 1 addition & 6 deletions src/Driver/RB/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
14 changes: 14 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use h4kuna\Exchange\Exceptions\InvalidStateException;
use Nette\StaticClass;
use Nette\Utils\DateTime as NetteDateTime;
use Psr\Http\Message\ResponseInterface;
use SimpleXMLElement;

final class Utils
{
Expand All @@ -29,6 +31,18 @@ public static function stroke2point(string $str): string
}


public static function createSimpleXMLElement(ResponseInterface $response): SimpleXMLElement
{
$xml = @simplexml_load_string($response->getBody()->getContents());

if ($xml === false) {
throw new InvalidStateException('Invalid source xml.');
}

return $xml;
}


public static function createTimeZone(string|DateTimeZone $timeZone): DateTimeZone
{
return is_string($timeZone) ? new DateTimeZone($timeZone) : $timeZone;
Expand Down
Loading

0 comments on commit ee1fe9e

Please sign in to comment.