This repository was archived by the owner on Apr 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathSmartSerializer.php
More file actions
126 lines (107 loc) · 2.95 KB
/
SmartSerializer.php
File metadata and controls
126 lines (107 loc) · 2.95 KB
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
<?php
/**
* This file is part of the Elastic OpenAPI PHP code generator.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Elastic\OpenApi\Codegen\Serializer;
use Elastic\OpenApi\Codegen\Exception\JsonErrorException;
/**
* Default serializer used by the client.
*
* @package Elastic\OpenApi\Codegen\Serializer
* @author Aurélien FOUCRET <aurelien.foucret@elastic.co>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
*/
class SmartSerializer implements SerializerInterface
{
/**
* @var string
*/
private $phpVersion;
/**
* Default constructor.
*/
public function __construct()
{
$this->phpVersion = phpversion();
}
/**
* Serialize assoc array into JSON string.
*
* @param string|array $data Assoc array to encode into JSON
*
* @return string
*/
public function serialize($data)
{
if (true === is_string($data)) {
return $data;
}
$this->prepareData($data);
$flags = JSON_PRESERVE_ZERO_FRACTION;
if (version_compare($this->phpVersion, '5.6.6', '<') || !defined('JSON_PRESERVE_ZERO_FRACTION')) {
$flags = null;
}
$data = json_encode($data, $flags);
if ('[]' === $data) {
return '{}';
}
return $data;
}
/**
* Deserialize by introspecting content_type. Tries to deserialize JSON,
* otherwise returns string.
*
* @throws JsonErrorException
*
* @param string $data JSON encoded string
* @param array $headers Response Headers
*
* @return array
*/
public function deserialize($data, $headers)
{
if (true === isset($headers['content_type']) && false === strpos($headers['content_type'], 'json')) {
return $data;
}
return $this->decode($data);
}
/**
* Prepare data for serialization :
* - Convert all empty arrays in stdClass, so we can get an object.
*
* @SuppressWarnings(PHPMD.MissingImport)
*
* @param array $data
*/
private function prepareData(&$data)
{
if (is_array($data) && empty($data)) {
$data = new \stdClass();
} elseif (is_array($data)) {
array_walk($data, [$this, __METHOD__]);
}
}
/**
* @todo For 2.0, remove the E_NOTICE check before raising the exception.
*
* @throws JsonErrorException
*
* @param $data
*
* @return array
*/
private function decode($data)
{
if (null === $data || 0 === strlen($data)) {
return '';
}
$result = json_decode($data, true);
if (JSON_ERROR_NONE !== json_last_error() && E_NOTICE === (error_reporting() & E_NOTICE)) {
throw new JsonErrorException(json_last_error(), $data, $result);
}
return $result;
}
}