comparison vendor/symfony/serializer/Encoder/ChainDecoder.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
17 * Decoder delegating the decoding to a chain of decoders. 17 * Decoder delegating the decoding to a chain of decoders.
18 * 18 *
19 * @author Jordi Boggiano <j.boggiano@seld.be> 19 * @author Jordi Boggiano <j.boggiano@seld.be>
20 * @author Johannes M. Schmitt <schmittjoh@gmail.com> 20 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
21 * @author Lukas Kahwe Smith <smith@pooteeweet.org> 21 * @author Lukas Kahwe Smith <smith@pooteeweet.org>
22 *
23 * @final since version 3.3.
22 */ 24 */
23 class ChainDecoder implements DecoderInterface 25 class ChainDecoder implements DecoderInterface /*, ContextAwareDecoderInterface*/
24 { 26 {
25 protected $decoders = array(); 27 protected $decoders = array();
26 protected $decoderByFormat = array(); 28 protected $decoderByFormat = array();
27 29
28 public function __construct(array $decoders = array()) 30 public function __construct(array $decoders = array())
33 /** 35 /**
34 * {@inheritdoc} 36 * {@inheritdoc}
35 */ 37 */
36 final public function decode($data, $format, array $context = array()) 38 final public function decode($data, $format, array $context = array())
37 { 39 {
38 return $this->getDecoder($format)->decode($data, $format, $context); 40 return $this->getDecoder($format, $context)->decode($data, $format, $context);
39 } 41 }
40 42
41 /** 43 /**
42 * {@inheritdoc} 44 * {@inheritdoc}
43 */ 45 */
44 public function supportsDecoding($format) 46 public function supportsDecoding($format/*, array $context = array()*/)
45 { 47 {
48 $context = func_num_args() > 1 ? func_get_arg(1) : array();
49
46 try { 50 try {
47 $this->getDecoder($format); 51 $this->getDecoder($format, $context);
48 } catch (RuntimeException $e) { 52 } catch (RuntimeException $e) {
49 return false; 53 return false;
50 } 54 }
51 55
52 return true; 56 return true;
54 58
55 /** 59 /**
56 * Gets the decoder supporting the format. 60 * Gets the decoder supporting the format.
57 * 61 *
58 * @param string $format 62 * @param string $format
63 * @param array $context
59 * 64 *
60 * @return DecoderInterface 65 * @return DecoderInterface
61 * 66 *
62 * @throws RuntimeException If no decoder is found. 67 * @throws RuntimeException if no decoder is found
63 */ 68 */
64 private function getDecoder($format) 69 private function getDecoder($format, array $context)
65 { 70 {
66 if (isset($this->decoderByFormat[$format]) 71 if (isset($this->decoderByFormat[$format])
67 && isset($this->decoders[$this->decoderByFormat[$format]]) 72 && isset($this->decoders[$this->decoderByFormat[$format]])
68 ) { 73 ) {
69 return $this->decoders[$this->decoderByFormat[$format]]; 74 return $this->decoders[$this->decoderByFormat[$format]];
70 } 75 }
71 76
72 foreach ($this->decoders as $i => $decoder) { 77 foreach ($this->decoders as $i => $decoder) {
73 if ($decoder->supportsDecoding($format)) { 78 if ($decoder->supportsDecoding($format, $context)) {
74 $this->decoderByFormat[$format] = $i; 79 $this->decoderByFormat[$format] = $i;
75 80
76 return $decoder; 81 return $decoder;
77 } 82 }
78 } 83 }