| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Api\Parser; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\Api\Parser\Exception\ParserException; |
| 6 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\ResponseInterface; |
| 7 |
trait PayloadParserTrait |
| 8 |
{ |
| 9 |
/** |
| 10 |
* @param string $json |
| 11 |
* |
| 12 |
* @throws ParserException |
| 13 |
* |
| 14 |
* @return array |
| 15 |
*/ |
| 16 |
private function parseJson($json, $response) |
| 17 |
{ |
| 18 |
$jsonPayload = \json_decode($json, \true); |
| 19 |
if (\JSON_ERROR_NONE !== \json_last_error()) { |
| 20 |
throw new ParserException('Error parsing JSON: ' . \json_last_error_msg(), 0, null, ['response' => $response]); |
| 21 |
} |
| 22 |
return $jsonPayload; |
| 23 |
} |
| 24 |
/** |
| 25 |
* @param string $xml |
| 26 |
* |
| 27 |
* @throws ParserException |
| 28 |
* |
| 29 |
* @return \SimpleXMLElement |
| 30 |
*/ |
| 31 |
protected function parseXml($xml, $response) |
| 32 |
{ |
| 33 |
$priorSetting = \libxml_use_internal_errors(\true); |
| 34 |
try { |
| 35 |
\libxml_clear_errors(); |
| 36 |
$xmlPayload = new \SimpleXMLElement($xml); |
| 37 |
if ($error = \libxml_get_last_error()) { |
| 38 |
throw new \RuntimeException($error->message); |
| 39 |
} |
| 40 |
} catch (\Exception $e) { |
| 41 |
throw new ParserException("Error parsing XML: {$e->getMessage()}", 0, $e, ['response' => $response]); |
| 42 |
} finally { |
| 43 |
\libxml_use_internal_errors($priorSetting); |
| 44 |
} |
| 45 |
return $xmlPayload; |
| 46 |
} |
| 47 |
} |
| 48 |
|