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