Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws\Api\Parser;
3
 
4
use Aws\Api\Parser\Exception\ParserException;
5
use Psr\Http\Message\ResponseInterface;
6
 
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
 
20
        if (JSON_ERROR_NONE !== json_last_error()) {
21
            throw new ParserException(
22
                'Error parsing JSON: ' . json_last_error_msg(),
23
                0,
24
                null,
25
                ['response' => $response]
26
            );
27
        }
28
 
29
        return $jsonPayload;
30
    }
31
 
32
    /**
33
     * @param string $xml
34
     *
35
     * @throws ParserException
36
     *
37
     * @return \SimpleXMLElement
38
     */
39
    protected function parseXml($xml, $response)
40
    {
41
        $priorSetting = libxml_use_internal_errors(true);
42
        try {
43
            libxml_clear_errors();
44
            $xmlPayload = new \SimpleXMLElement($xml);
45
            if ($error = libxml_get_last_error()) {
46
                throw new \RuntimeException($error->message);
47
            }
48
        } catch (\Exception $e) {
49
            throw new ParserException(
50
                "Error parsing XML: {$e->getMessage()}",
51
                0,
52
                $e,
53
                ['response' => $response]
54
            );
55
        } finally {
56
            libxml_use_internal_errors($priorSetting);
57
        }
58
 
59
        return $xmlPayload;
60
    }
61
}