annotate vendor/symfony/serializer/Encoder/ChainEncoder.php @ 8:50b0d041100e

Further files for download
author Chris Cannam
date Mon, 05 Feb 2018 10:56:40 +0000
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Serializer\Encoder;
Chris@0 13
Chris@0 14 use Symfony\Component\Serializer\Exception\RuntimeException;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Encoder delegating the decoding to a chain of encoders.
Chris@0 18 *
Chris@0 19 * @author Jordi Boggiano <j.boggiano@seld.be>
Chris@0 20 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
Chris@0 21 * @author Lukas Kahwe Smith <smith@pooteeweet.org>
Chris@0 22 */
Chris@0 23 class ChainEncoder implements EncoderInterface
Chris@0 24 {
Chris@0 25 protected $encoders = array();
Chris@0 26 protected $encoderByFormat = array();
Chris@0 27
Chris@0 28 public function __construct(array $encoders = array())
Chris@0 29 {
Chris@0 30 $this->encoders = $encoders;
Chris@0 31 }
Chris@0 32
Chris@0 33 /**
Chris@0 34 * {@inheritdoc}
Chris@0 35 */
Chris@0 36 final public function encode($data, $format, array $context = array())
Chris@0 37 {
Chris@0 38 return $this->getEncoder($format)->encode($data, $format, $context);
Chris@0 39 }
Chris@0 40
Chris@0 41 /**
Chris@0 42 * {@inheritdoc}
Chris@0 43 */
Chris@0 44 public function supportsEncoding($format)
Chris@0 45 {
Chris@0 46 try {
Chris@0 47 $this->getEncoder($format);
Chris@0 48 } catch (RuntimeException $e) {
Chris@0 49 return false;
Chris@0 50 }
Chris@0 51
Chris@0 52 return true;
Chris@0 53 }
Chris@0 54
Chris@0 55 /**
Chris@0 56 * Checks whether the normalization is needed for the given format.
Chris@0 57 *
Chris@0 58 * @param string $format
Chris@0 59 *
Chris@0 60 * @return bool
Chris@0 61 */
Chris@0 62 public function needsNormalization($format)
Chris@0 63 {
Chris@0 64 $encoder = $this->getEncoder($format);
Chris@0 65
Chris@0 66 if (!$encoder instanceof NormalizationAwareInterface) {
Chris@0 67 return true;
Chris@0 68 }
Chris@0 69
Chris@0 70 if ($encoder instanceof self) {
Chris@0 71 return $encoder->needsNormalization($format);
Chris@0 72 }
Chris@0 73
Chris@0 74 return false;
Chris@0 75 }
Chris@0 76
Chris@0 77 /**
Chris@0 78 * Gets the encoder supporting the format.
Chris@0 79 *
Chris@0 80 * @param string $format
Chris@0 81 *
Chris@0 82 * @return EncoderInterface
Chris@0 83 *
Chris@0 84 * @throws RuntimeException if no encoder is found
Chris@0 85 */
Chris@0 86 private function getEncoder($format)
Chris@0 87 {
Chris@0 88 if (isset($this->encoderByFormat[$format])
Chris@0 89 && isset($this->encoders[$this->encoderByFormat[$format]])
Chris@0 90 ) {
Chris@0 91 return $this->encoders[$this->encoderByFormat[$format]];
Chris@0 92 }
Chris@0 93
Chris@0 94 foreach ($this->encoders as $i => $encoder) {
Chris@0 95 if ($encoder->supportsEncoding($format)) {
Chris@0 96 $this->encoderByFormat[$format] = $i;
Chris@0 97
Chris@0 98 return $encoder;
Chris@0 99 }
Chris@0 100 }
Chris@0 101
Chris@0 102 throw new RuntimeException(sprintf('No encoder found for format "%s".', $format));
Chris@0 103 }
Chris@0 104 }