Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Serializer\Encoder; Chris@0: Chris@0: use Symfony\Component\Serializer\Exception\RuntimeException; Chris@0: Chris@0: /** Chris@0: * Encoder delegating the decoding to a chain of encoders. Chris@0: * Chris@0: * @author Jordi Boggiano Chris@0: * @author Johannes M. Schmitt Chris@0: * @author Lukas Kahwe Smith Chris@14: * Chris@14: * @final since version 3.3. Chris@0: */ Chris@14: class ChainEncoder implements EncoderInterface /*, ContextAwareEncoderInterface*/ Chris@0: { Chris@17: protected $encoders = []; Chris@17: protected $encoderByFormat = []; Chris@0: Chris@17: public function __construct(array $encoders = []) Chris@0: { Chris@0: $this->encoders = $encoders; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: final public function encode($data, $format, array $context = []) Chris@0: { Chris@14: return $this->getEncoder($format, $context)->encode($data, $format, $context); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: public function supportsEncoding($format/*, array $context = []*/) Chris@0: { Chris@17: $context = \func_num_args() > 1 ? func_get_arg(1) : []; Chris@14: Chris@0: try { Chris@14: $this->getEncoder($format, $context); Chris@0: } catch (RuntimeException $e) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether the normalization is needed for the given format. Chris@0: * Chris@0: * @param string $format Chris@14: * @param array $context Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@17: public function needsNormalization($format/*, array $context = []*/) Chris@0: { Chris@17: $context = \func_num_args() > 1 ? func_get_arg(1) : []; Chris@14: $encoder = $this->getEncoder($format, $context); Chris@0: Chris@0: if (!$encoder instanceof NormalizationAwareInterface) { Chris@0: return true; Chris@0: } Chris@0: Chris@0: if ($encoder instanceof self) { Chris@14: return $encoder->needsNormalization($format, $context); Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the encoder supporting the format. Chris@0: * Chris@0: * @param string $format Chris@14: * @param array $context Chris@0: * Chris@0: * @return EncoderInterface Chris@0: * Chris@0: * @throws RuntimeException if no encoder is found Chris@0: */ Chris@14: private function getEncoder($format, array $context) Chris@0: { Chris@0: if (isset($this->encoderByFormat[$format]) Chris@0: && isset($this->encoders[$this->encoderByFormat[$format]]) Chris@0: ) { Chris@0: return $this->encoders[$this->encoderByFormat[$format]]; Chris@0: } Chris@0: Chris@0: foreach ($this->encoders as $i => $encoder) { Chris@14: if ($encoder->supportsEncoding($format, $context)) { Chris@0: $this->encoderByFormat[$format] = $i; Chris@0: Chris@0: return $encoder; Chris@0: } Chris@0: } Chris@0: Chris@0: throw new RuntimeException(sprintf('No encoder found for format "%s".', $format)); Chris@0: } Chris@0: }