1<?php
2
3declare(strict_types = 1);
4
5namespace Elasticsearch\Common\Exceptions\Serializer;
6
7use Elasticsearch\Common\Exceptions\ElasticsearchException;
8
9/**
10 * Class JsonErrorException
11 *
12 * @category Elasticsearch
13 * @package  Elasticsearch\Common\Exceptions\Curl
14 * @author   Bez Hermoso <bezalelhermoso@gmail.com>
15 * @license  http://www.apache.org/licenses/LICENSE-2.0 Apache2
16 * @link     http://elastic.co
17 */
18class JsonErrorException extends \Exception implements ElasticsearchException
19{
20    /**
21     * @var mixed
22     */
23    private $input;
24
25    /**
26     * @var mixed
27     */
28    private $result;
29
30    private static $messages = array(
31        JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
32        JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
33        JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
34        JSON_ERROR_SYNTAX => 'Syntax error',
35        JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
36        JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded',
37        JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded',
38        JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given',
39
40        // JSON_ERROR_* constant values that are available on PHP >= 7.0
41        9 => 'Decoding of value would result in invalid PHP property name', //JSON_ERROR_INVALID_PROPERTY_NAME
42        10 => 'Attempted to decode nonexistent UTF-16 code-point' //JSON_ERROR_UTF16
43    );
44
45    public function __construct($code, $input, $result, $previous = null)
46    {
47        if (isset(self::$messages[$code]) !== true) {
48            throw new \InvalidArgumentException(sprintf('Encountered unknown JSON error code: [%d]', $code));
49        }
50
51        parent::__construct(self::$messages[$code], $code, $previous);
52        $this->input = $input;
53        $this->result = $result;
54    }
55
56    /**
57     * @return mixed
58     */
59    public function getInput()
60    {
61        return $this->input;
62    }
63
64    /**
65     * @return mixed
66     */
67    public function getResult()
68    {
69        return $this->result;
70    }
71}
72