summaryrefslogtreecommitdiff
path: root/vendor/fguillot/json-rpc/src/JsonRPC/Response/ResponseParser.php
blob: 02d449bae5dabd557cf7d24da32174794bf2eb1c (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php

namespace JsonRPC\Response;

use BadFunctionCallException;
use InvalidArgumentException;
use Exception;
use JsonRPC\Exception\InvalidJsonFormatException;
use JsonRPC\Exception\InvalidJsonRpcFormatException;
use JsonRPC\Exception\ResponseException;
use JsonRPC\Validator\JsonFormatValidator;

/**
 * Class ResponseParser
 *
 * @package JsonRPC\Request
 * @author  Frederic Guillot
 */
class ResponseParser
{
    /**
     * Payload
     *
     * @access private
     * @var mixed
     */
    private $payload;

    /**
     * Do not immediately throw an exception on error. Return it instead.
     *
     * @var bool
     */
    private $returnException = false;

    /**
     * Get new object instance
     *
     * @static
     * @access public
     * @return ResponseParser
     */
    public static function create()
    {
        return new static();
    }

    /**
     * Set Return Exception Or Throw It
     *
     * @param $returnException
     * @return ResponseParser
     */
    public function withReturnException($returnException)
    {
        $this->returnException = $returnException;
        return $this;
    }

    /**
     * Set payload
     *
     * @access public
     * @param  mixed $payload
     * @return $this
     */
    public function withPayload($payload)
    {
        $this->payload = $payload;
        return $this;
    }

    /**
     * Parse response
     *
     * @return array|Exception|null
     * @throws InvalidJsonFormatException
     * @throws BadFunctionCallException
     * @throws InvalidJsonRpcFormatException
     * @throws InvalidArgumentException
     * @throws Exception
     * @throws ResponseException
     */
    public function parse()
    {
        JsonFormatValidator::validate($this->payload);

        if ($this->isBatchResponse()) {
            $results = array();

            foreach ($this->payload as $response) {
                $results[] = self::create()
                    ->withReturnException($this->returnException)
                    ->withPayload($response)
                    ->parse();
            }

            return $results;
        }

        if (isset($this->payload['error']['code'])) {
            try {
                $this->handleExceptions();
            } catch (Exception $e) {
                if ($this->returnException) {
                    return $e;
                }
                throw $e;
            }
        }

        return isset($this->payload['result']) ? $this->payload['result'] : null;
    }

    /**
     * Handle exceptions
     *
     * @access private
     * @throws InvalidJsonFormatException
     * @throws InvalidJsonRpcFormatException
     * @throws ResponseException
     */
    private function handleExceptions()
    {
        switch ($this->payload['error']['code']) {
            case -32700:
                throw new InvalidJsonFormatException('Parse error: '.$this->payload['error']['message']);
            case -32600:
                throw new InvalidJsonRpcFormatException('Invalid Request: '.$this->payload['error']['message']);
            case -32601:
                throw new BadFunctionCallException('Procedure not found: '.$this->payload['error']['message']);
            case -32602:
                throw new InvalidArgumentException('Invalid arguments: '.$this->payload['error']['message']);
            default:
                throw new ResponseException(
                    $this->payload['error']['message'],
                    $this->payload['error']['code'],
                    null,
                    isset($this->payload['error']['data']) ? $this->payload['error']['data'] : null
                );
        }
    }

    /**
     * Return true if we have a batch response
     *
     * @access private
     * @return boolean
     */
    private function isBatchResponse()
    {
        return array_keys($this->payload) === range(0, count($this->payload) - 1);
    }
}