Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Serializer] Added a ChainEncoder and a ChainDecoder
These classes contains the logic previously defined in the Serializer itself to handle the choice of a serializer. This allows reusing it when using only the encoding part of the component.
- Loading branch information
Showing
3 changed files
with
183 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Encoder; | ||
|
||
use Symfony\Component\Serializer\Encoder\DecoderInterface; | ||
use Symfony\Component\Serializer\Exception\RuntimeException; | ||
|
||
/** | ||
* Decoder delegating the decoding to a chain of decoders. | ||
* | ||
* @author Jordi Boggiano <j.boggiano@seld.be> | ||
* @author Johannes M. Schmitt <schmittjoh@gmail.com> | ||
* @author Lukas Kahwe Smith <smith@pooteeweet.org> | ||
*/ | ||
class ChainDecoder implements DecoderInterface | ||
{ | ||
protected $decoders = array(); | ||
protected $decoderByFormat = array(); | ||
|
||
public function __construct(array $decoders = array()) | ||
{ | ||
$this->decoders = $decoders; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
final public function decode($data, $format) | ||
{ | ||
return $this->getDecoder($format)->decode($data, $format); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsDecoding($format) | ||
{ | ||
try { | ||
$this->getDecoder($format); | ||
} catch (RuntimeException $e) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Gets the decoder supporting the format. | ||
* | ||
* @param string $format | ||
* | ||
* @return DecoderInterface | ||
* @throws RuntimeException if no decoder is found | ||
*/ | ||
private function getDecoder($format) | ||
{ | ||
if (isset($this->decoderByFormat[$format]) | ||
&& isset($this->decoders[$this->decoderByFormat[$format]]) | ||
) { | ||
return $this->decoders[$this->decoderByFormat[$format]]; | ||
} | ||
|
||
foreach ($this->decoders as $i => $decoder) { | ||
if ($decoder->supportsDecoding($format)) { | ||
$this->decoderByFormat[$format] = $i; | ||
|
||
return $decoder; | ||
} | ||
} | ||
|
||
throw new RuntimeException(sprintf('No decoder found for format "%s".', $format)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Encoder; | ||
|
||
use Symfony\Component\Serializer\Encoder\EncoderInterface; | ||
use Symfony\Component\Serializer\Exception\RuntimeException; | ||
|
||
/** | ||
* Encoder delegating the decoding to a chain of encoders. | ||
* | ||
* @author Jordi Boggiano <j.boggiano@seld.be> | ||
* @author Johannes M. Schmitt <schmittjoh@gmail.com> | ||
* @author Lukas Kahwe Smith <smith@pooteeweet.org> | ||
*/ | ||
class ChainEncoder implements EncoderInterface | ||
{ | ||
protected $encoders = array(); | ||
protected $encoderByFormat = array(); | ||
|
||
public function __construct(array $encoders = array()) | ||
{ | ||
$this->encoders = $encoders; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
final public function encode($data, $format) | ||
{ | ||
return $this->getEncoder($format)->encode($data, $format); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsEncoding($format) | ||
{ | ||
try { | ||
$this->getEncoder($format); | ||
} catch (RuntimeException $e) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Gets the encoder supporting the format. | ||
* | ||
* @param string $format | ||
* | ||
* @return EncoderInterface | ||
* @throws RuntimeException if no encoder is found | ||
*/ | ||
public function getEncoder($format) | ||
{ | ||
if (isset($this->encoderByFormat[$format]) | ||
&& isset($this->encoders[$this->encoderByFormat[$format]]) | ||
) { | ||
return $this->encoders[$this->encoderByFormat[$format]]; | ||
} | ||
|
||
foreach ($this->encoders as $i => $encoder) { | ||
if ($encoder->supportsEncoding($format)) { | ||
$this->encoderByFormat[$format] = $i; | ||
|
||
return $encoder; | ||
} | ||
} | ||
|
||
throw new RuntimeException(sprintf('No encoder found for format "%s".', $format)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters