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\Serializers;
20
21use Elasticsearch\Common\Exceptions;
22use Elasticsearch\Common\Exceptions\Serializer\JsonErrorException;
23
24if (!defined('JSON_INVALID_UTF8_SUBSTITUTE')) {
25    //PHP < 7.2 Define it as 0 so it does nothing
26    define('JSON_INVALID_UTF8_SUBSTITUTE', 0);
27}
28
29class SmartSerializer implements SerializerInterface
30{
31    /**
32     * {@inheritdoc}
33     */
34    public function serialize($data): string
35    {
36        if (is_string($data) === true) {
37            return $data;
38        } else {
39            $data = json_encode($data, JSON_PRESERVE_ZERO_FRACTION + JSON_INVALID_UTF8_SUBSTITUTE);
40            if ($data === false) {
41                throw new Exceptions\RuntimeException("Failed to JSON encode: ".json_last_error_msg());
42            }
43            if ($data === '[]') {
44                return '{}';
45            } else {
46                return $data;
47            }
48        }
49    }
50
51    /**
52     * {@inheritdoc}
53     */
54    public function deserialize(?string $data, array $headers)
55    {
56        if (isset($headers['content_type']) === true) {
57            if (strpos($headers['content_type'], 'json') !== false) {
58                return $this->decode($data);
59            } else {
60                //Not json, return as string
61                return $data;
62            }
63        } else {
64            //No content headers, assume json
65            return $this->decode($data);
66        }
67    }
68
69    /**
70     * @todo For 2.0, remove the E_NOTICE check before raising the exception.
71     *
72     * @param string|null $data
73     *
74     * @return array
75     * @throws JsonErrorException
76     */
77    private function decode(?string $data): array
78    {
79        if ($data === null || strlen($data) === 0) {
80            return [];
81        }
82
83        if (version_compare(PHP_VERSION, '7.3.0') >= 0) {
84            try {
85                $result = json_decode($data, true, 512, JSON_THROW_ON_ERROR);
86                return $result;
87            } catch (\JsonException $e) {
88                $result = $result ?? [];
89                throw new JsonErrorException($e->getCode(), $data, $result);
90            }
91        }
92
93        $result = @json_decode($data, true);
94        // Throw exception only if E_NOTICE is on to maintain backwards-compatibility on systems that silently ignore E_NOTICEs.
95        if (json_last_error() !== JSON_ERROR_NONE && (error_reporting() & E_NOTICE) === E_NOTICE) {
96            throw new JsonErrorException(json_last_error(), $data, $result);
97        }
98        return $result;
99    }
100}
101