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