8000 refactored encoder handling to use the supports*() methods to determi… · symfony/symfony@d021dc8 · GitHub
[go: up one dir, main page]

Skip to content

Commit d021dc8

Browse files
committed
refactored encoder handling to use the supports*() methods to determine which encoder handles what format
1 parent f8e2787 commit d021dc8

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/Symfony/Component/Serializer/Serializer.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz
4040
protected $encoders = array();
4141
protected $normalizerCache = array();
4242
protected $denormalizerCache = array();
43+
protected $encoderByFormat = array();
44+
protected $decoderByFormat = array();
4345

4446
public function __construct(array $normalizers = array(), array $encoders = array())
4547
{
@@ -267,12 +269,42 @@ public function supportsDecoding($format)
267269
/**
268270
* {@inheritdoc}
269271
*/
270-
public function getEncoder($format)
272+
private function getEncoder($format)
271273
{
272-
if (!isset($this->encoders[$format])) {
273-
throw new RuntimeException(sprintf('No encoder found for format "%s".', $format));
274+
if (isset($this->encoderByFormat[$format])
275+
&& isset($this->encoderByFormat[$format])
276+
) {
277+
return $this->encoders[$this->encoderByFormat[$format]];
274278
}
275279

276-
return $this->encoders[$format];
280+
foreach ($this->encoders as $i => $encoder) {
281+
if ($encoder->supportsEncoding($format)) {
282+
$this->encoderByFormat[$format] = $i;
283+
return $encoder;
284+
}
285+
}
286+
287+
throw new RuntimeException(sprintf('No encoder found for format "%s".', $format));
288+
}
289+
290+
/**
291+
* {@inheritdoc}
292+
*/
293+
private function getDecoder($format)
294+
{
295+
if (isset($this->decoderByFormat[$format])
296+
&& isset($this->decoderByFormat[$format])
297+
) {
298+
return $this->encoders[$this->decoderByFormat[$format]];
299+
}
300+
301+
foreach ($this->encoders as $i => $encoder) {
302+
if ($encoder->supportsDecoding($format)) {
303+
$this->decoderByFormat[$format] = $i;
304+
return $encoder;
305+
}
306+
}
307+
308+
throw new RuntimeException(sprintf('No decoder found for format "%s".', $format));
277309
}
278310
}

0 commit comments

Comments
 (0)
0