17
17
use Symfony \Component \Serializer \Encoder \DecoderInterface ;
18
18
use Symfony \Component \Serializer \Normalizer \NormalizerInterface ;
19
19
use Symfony \Component \Serializer \Normalizer \DenormalizerInterface ;
20
- use Symfony \Component \Serializer \Exception \RuntimeException ;
21
20
use Symfony \Component \Serializer \Exception \LogicException ;
22
21
use Symfony \Component \Serializer \Exception \UnexpectedValueException ;
23
22
@@ -144,55 +143,68 @@ public function denormalize($data, $type, $format = null, array $context = array
144
143
*/
145
144
public function supportsNormalization ($ data , $ format = null )
146
145
{
147
- try {
148
- $ this ->getNormalizer ($ data , $ format );
149
- } catch (RuntimeException $ e ) {
150
- return false ;
151
- }
152
-
153
- return true ;
146
+ return (bool ) $ this ->getNormalizer ($ data , $ format );
154
147
}
155
148
156
149
/**
157
150
* {@inheritdoc}
158
151
*/
159
152
public function supportsDenormalization ($ data , $ type , $ format = null )
160
153
{
161
- try {
162
- $ this ->getDenormalizer ($ data , $ type , $ format = null );
163
- } catch (RuntimeException $ e ) {
164
- return false ;
165
- }
166
-
167
- return true ;
154
+ return (bool ) $ this ->getDenormalizer ($ data , $ type , $ format );
168
155
}
169
156
170
157
/**
171
- * {@inheritdoc}
158
+ * Returns a matching normalizer.
159
+ *
160
+ * @param object $data The object to get the serializer for
161
+ * @param string $format format name, present to give the option to normalizers to act differently based on formats
162
+ *
163
+ * @return NormalizerInterface|null
172
164
*/
173
- private function getNormalizer ($ data , $ format = null )
165
+ private function getNormalizer ($ data , $ format )
174
166
{
167
+
168
+ $ class = get_class ($ data );
169
+ if (isset ($ this ->normalizerCache [$ class ][$ format ])) {
170
+ return $ this ->normalizerCache [$ class ][$ format ];
171
+ }
172
+
175
173
foreach ($ this ->normalizers as $ normalizer ) {
176
174
if ($ normalizer instanceof NormalizerInterface && $ normalizer ->supportsNormalization ($ data , $ format )) {
175
+ $ this ->normalizerCache [$ class ][$ format ] = $ normalizer ;
176
+
177
177
return $ normalizer ;
178
178
}
179
179
}
180
180
181
- throw new RuntimeException ( sprintf ( ' No normalizer found for format "%s". ' , $ format )) ;
181
+ return null ;
182
182
}
183
183
184
184
/**
185
- * {@inheritdoc}
185
+ * Returns a matching denormalizer.
186
+ *
187
+ * @param mixed $data data to restore
188
+ * @param string $class the expected class to instantiate
189
+ * @param string $format format name, present to give the option to normalizers to act differently based on formats
190
+ *
191
+ * @return DenormalizerInterface|null
186
192
*/
187
- private function getDenormalizer ($ data , $ type , $ format = null )
193
+ private function getDenormalizer ($ data , $ class , $ format )
188
194
{
195
+ if (isset ($ this ->denormalizerCache [$ class ][$ format ])) {
196
+ return $ this ->denormalizerCache [$ class ][$ format ];
197
+ }
198
+
189
199
foreach ($ this ->normalizers as $ normalizer ) {
190
- if ($ normalizer instanceof DenormalizerInterface && $ normalizer ->supportsDenormalization ($ data , $ type , $ format )) {
200
+ if ($ normalizer instanceof DenormalizerInterface && $ normalizer ->supportsDenormalization ($ data , $ class , $ format )) {
201
+ $ this ->denormalizerCache [$ class ][$ format ] = $ normalizer ;
202
+
191
203
return $ normalizer ;
192
204
}
193
205
}
194
206
195
- throw new RuntimeException ( sprintf ( ' No denormalizer found for format "%s". ' , $ format )) ;
207
+ return null ;
196
208
}
197
209
198
210
/**
@@ -223,27 +235,16 @@ final public function decode($data, $format, array $context = array())
223
235
* @throws LogicException
224
236
* @throws UnexpectedValueException
225
237
*/
226
- private function normalizeObject ($ object , $ format = null , array $ context = array ())
238
+ private function normalizeObject ($ object , $ format , array $ context = array ())
227
239
{
228
240
if (!$ this ->normalizers ) {
229
241
throw new LogicException ('You must register at least one normalizer to be able to normalize objects. ' );
230
242
}
231
243
232
- $ class = get_class ($ object );
233
- if (isset ($ this ->normalizerCache [$ class ][$ format ])) {
234
- return $ this ->normalizerCache [$ class ][$ format ]->normalize ($ object , $ format , $ context );
235
- }
236
-
237
- foreach ($ this ->normalizers as $ normalizer ) {
238
- if ($ normalizer instanceof NormalizerInterface
239
- && $ normalizer ->supportsNormalization ($ object , $ format )) {
240
- $ this ->normalizerCache [$ class ][$ format ] = $ normalizer ;
241
-
242
- return $ normalizer ->normalize ($ object , $ format , $ context );
243
- }
244
+ if ($ normalizer = $ this ->getNormalizer ($ object , $ format )) {
245
+ return $ normalizer ->normalize ($ object , $ format , $ context );
244
246
}
245
-
246
- throw new UnexpectedValueException (sprintf ('Could not normalize object of type %s, no supporting normalizer found. ' , $ class ));
247
+ throw new UnexpectedValueException (sprintf ('Could not normalize object of type %s, no supporting normalizer found. ' , get_class ($ object )));
247
248
}
248
249
249
250
/**
@@ -259,25 +260,15 @@ private function normalizeObject($object, $format = null, array $context = array
259
260
* @throws LogicException
260
261
* @throws UnexpectedValueException
261
262
*/
262
- private function denormalizeObject ($ data , $ class , $ format = null , array $ context = array ())
263
+ private function denormalizeObject ($ data , $ class , $ format , array $ context = array ())
263
264
{
264
265
if (!$ this ->normalizers ) {
265
266
throw new LogicException ('You must register at least one normalizer to be able to denormalize objects. ' );
266
267
}
267
268
268
- if (isset ($ this ->denormalizerCache [$ class ][$ format ])) {
269
- return $ this ->denormalizerCache [$ class ][$ format ]->denormalize ($ data , $ class , $ format , $ context );
270
- }
271
-
272
- foreach ($ this ->normalizers as $ normalizer ) {
273
- if ($ normalizer instanceof DenormalizerInterface
274
- && $ normalizer ->supportsDenormalization ($ data , $ class , $ format )) {
275
- $ this ->denormalizerCache [$ class ][$ format ] = $ normalizer ;
276
-
277
- return $ normalizer ->denormalize ($ data , $ class , $ format , $ context );
278
- }
269
+ if ($ normalizer = $ this ->getDenormalizer ($ data , $ class , $ format )) {
270
+ return $ normalizer ->denormalize ($ data , $ class , $ format , $ context );
279
271
}
280
-
281
272
throw new UnexpectedValueException (sprintf ('Could not denormalize object of type %s, no supporting normalizer found. ' , $ class ));
282
273
}
283
274
0 commit comments