1 | <?php |
||
33 | class NativeJsonSerializer implements SerializerInterface |
||
34 | { |
||
35 | |||
36 | /** |
||
37 | * @inheritdoc |
||
38 | */ |
||
39 | 5 | public function serialize($data, array $params = array()) |
|
40 | { |
||
41 | 5 | $forceObject = $this->forceObject($params); |
|
42 | |||
43 | 5 | return json_encode($data, $forceObject); |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * @inheritdoc |
||
48 | */ |
||
49 | 5 | public function deserialize($data, array $params = array()) |
|
50 | { |
||
51 | 5 | $assoc = $this->useAssoc($params); |
|
52 | 5 | $decodedJson = json_decode($data, $assoc); |
|
53 | |||
54 | 5 | if ($decodedJson !== null) { |
|
55 | 3 | return $this->createGateway($assoc, $decodedJson); |
|
56 | } |
||
57 | |||
58 | // json_last_error_msg is only present in 5.5 and higher. |
||
59 | 2 | if (version_compare(PHP_VERSION, '5.5.0', '>=')) { |
|
60 | 2 | $jsonLastError = json_last_error(); |
|
61 | |||
62 | if ( |
||
63 | 2 | JSON_ERROR_NONE === $jsonLastError |
|
64 | 2 | && ($data === '' || '{}' == $data) |
|
65 | 2 | ) { |
|
66 | 1 | return $this->createGateway($assoc, array()); |
|
67 | } |
||
68 | |||
69 | 1 | throw new DeserializationFailureException(json_last_error_msg(), $jsonLastError); |
|
70 | } |
||
71 | |||
72 | if ($data === '') { |
||
73 | return $this->createGateway($assoc, array()); |
||
74 | } |
||
75 | |||
76 | // Fall through for versions below 5.5 |
||
77 | throw new DeserializationFailureException('JSON syntax error in: ' . $data); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Decides whether to use assoc or stdClass. |
||
82 | * |
||
83 | * @param array $params The array of params. |
||
84 | * |
||
85 | * @return boolean |
||
86 | * @author Mario Mueller |
||
87 | */ |
||
88 | 5 | private function useAssoc($params) |
|
89 | { |
||
90 | 5 | $assoc = true; |
|
91 | 5 | if (isset($params['assoc']) && is_bool($params['assoc'])) { |
|
92 | 3 | $assoc = $params['assoc']; |
|
93 | 3 | } |
|
94 | |||
95 | 5 | return $assoc; |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * Decides whether to force objects. |
||
100 | * |
||
101 | * @param array $params The array of params. |
||
102 | * |
||
103 | * @return integer |
||
104 | * @author Patrick Pokatilo <[email protected]> |
||
105 | */ |
||
106 | 5 | private function forceObject($params) |
|
115 | |||
116 | /** |
||
117 | * Creates the correct gateway based on assoc being true or false. |
||
118 | * |
||
119 | * The information if we need assoc could also be determined by looking |
||
120 | * at the type of $decodedJson, but we thing a boolean is faster and the |
||
121 | * decision has already been made before. |
||
122 | * |
||
123 | * @author Mario Mueller |
||
124 | * |
||
125 | * @param boolean $assoc Should we use assoc? |
||
126 | * @param array|\stdClass $decodedJson |
||
127 | * |
||
128 | * @return \Elastification\Client\Serializer\Gateway\GatewayInterface |
||
129 | */ |
||
130 | 4 | private function createGateway($assoc, $decodedJson) |
|
140 | } |
||
141 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.