This repository was archived by the owner on Mar 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathParser.php
More file actions
48 lines (42 loc) · 1.47 KB
/
Copy pathParser.php
File metadata and controls
48 lines (42 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
declare(strict_types=1);
namespace SimPod\SmsManager;
use Psr\Http\Message\ResponseInterface;
use SimpleXMLElement;
use SimPod\SmsManager\Exception\XmlParsingFailed;
use Throwable;
use function libxml_disable_entity_loader;
use function libxml_use_internal_errors;
use const LIBXML_NONET;
class Parser
{
/**
* @param mixed[] $config
*/
public static function parseXmlResponseBody(?ResponseInterface $response, array $config = []) : SimpleXMLElement
{
$disableEntities = libxml_disable_entity_loader();
$internalErrors = libxml_use_internal_errors(true);
try {
// Allow XML to be retrieved even if there is no response body
$xml = new SimpleXMLElement(
$response === null ? '<root />' : (string) $response->getBody(),
$config['libxml_options'] ?? LIBXML_NONET,
false,
$config['ns'] ?? '',
$config['ns_is_prefix'] ?? false
);
libxml_disable_entity_loader($disableEntities);
libxml_use_internal_errors($internalErrors);
} catch (Throwable $exception) {
libxml_disable_entity_loader($disableEntities);
libxml_use_internal_errors($internalErrors);
throw new XmlParsingFailed(
'Unable to parse response body into XML: ' . $exception->getMessage(),
0,
$exception
);
}
return $xml;
}
}