annotate vendor/symfony/serializer/Encoder/ChainDecoder.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
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 * Decoder delegating the decoding to a chain of decoders.
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@14 22 *
Chris@14 23 * @final since version 3.3.
Chris@0 24 */
Chris@14 25 class ChainDecoder implements DecoderInterface /*, ContextAwareDecoderInterface*/
Chris@0 26 {
Chris@17 27 protected $decoders = [];
Chris@17 28 protected $decoderByFormat = [];
Chris@0 29
Chris@17 30 public function __construct(array $decoders = [])
Chris@0 31 {
Chris@0 32 $this->decoders = $decoders;
Chris@0 33 }
Chris@0 34
Chris@0 35 /**
Chris@0 36 * {@inheritdoc}
Chris@0 37 */
Chris@17 38 final public function decode($data, $format, array $context = [])
Chris@0 39 {
Chris@14 40 return $this->getDecoder($format, $context)->decode($data, $format, $context);
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * {@inheritdoc}
Chris@0 45 */
Chris@17 46 public function supportsDecoding($format/*, array $context = []*/)
Chris@0 47 {
Chris@17 48 $context = \func_num_args() > 1 ? func_get_arg(1) : [];
Chris@14 49
Chris@0 50 try {
Chris@14 51 $this->getDecoder($format, $context);
Chris@0 52 } catch (RuntimeException $e) {
Chris@0 53 return false;
Chris@0 54 }
Chris@0 55
Chris@0 56 return true;
Chris@0 57 }
Chris@0 58
Chris@0 59 /**
Chris@0 60 * Gets the decoder supporting the format.
Chris@0 61 *
Chris@0 62 * @param string $format
Chris@14 63 * @param array $context
Chris@0 64 *
Chris@0 65 * @return DecoderInterface
Chris@0 66 *
Chris@14 67 * @throws RuntimeException if no decoder is found
Chris@0 68 */
Chris@14 69 private function getDecoder($format, array $context)
Chris@0 70 {
Chris@0 71 if (isset($this->decoderByFormat[$format])
Chris@0 72 && isset($this->decoders[$this->decoderByFormat[$format]])
Chris@0 73 ) {
Chris@0 74 return $this->decoders[$this->decoderByFormat[$format]];
Chris@0 75 }
Chris@0 76
Chris@0 77 foreach ($this->decoders as $i => $decoder) {
Chris@14 78 if ($decoder->supportsDecoding($format, $context)) {
Chris@0 79 $this->decoderByFormat[$format] = $i;
Chris@0 80
Chris@0 81 return $decoder;
Chris@0 82 }
Chris@0 83 }
Chris@0 84
Chris@0 85 throw new RuntimeException(sprintf('No decoder found for format "%s".', $format));
Chris@0 86 }
Chris@0 87 }