comparison vendor/symfony/serializer/Encoder/ChainEncoder.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 * Encoder delegating the decoding to a chain of encoders. 17 * Encoder delegating the decoding to a chain of encoders.
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 ChainEncoder implements EncoderInterface 25 class ChainEncoder implements EncoderInterface /*, ContextAwareEncoderInterface*/
24 { 26 {
25 protected $encoders = array(); 27 protected $encoders = array();
26 protected $encoderByFormat = array(); 28 protected $encoderByFormat = array();
27 29
28 public function __construct(array $encoders = array()) 30 public function __construct(array $encoders = array())
33 /** 35 /**
34 * {@inheritdoc} 36 * {@inheritdoc}
35 */ 37 */
36 final public function encode($data, $format, array $context = array()) 38 final public function encode($data, $format, array $context = array())
37 { 39 {
38 return $this->getEncoder($format)->encode($data, $format, $context); 40 return $this->getEncoder($format, $context)->encode($data, $format, $context);
39 } 41 }
40 42
41 /** 43 /**
42 * {@inheritdoc} 44 * {@inheritdoc}
43 */ 45 */
44 public function supportsEncoding($format) 46 public function supportsEncoding($format/*, array $context = array()*/)
45 { 47 {
48 $context = func_num_args() > 1 ? func_get_arg(1) : array();
49
46 try { 50 try {
47 $this->getEncoder($format); 51 $this->getEncoder($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 * Checks whether the normalization is needed for the given format. 60 * Checks whether the normalization is needed for the given format.
57 * 61 *
58 * @param string $format 62 * @param string $format
63 * @param array $context
59 * 64 *
60 * @return bool 65 * @return bool
61 */ 66 */
62 public function needsNormalization($format) 67 public function needsNormalization($format/*, array $context = array()*/)
63 { 68 {
64 $encoder = $this->getEncoder($format); 69 $context = func_num_args() > 1 ? func_get_arg(1) : array();
70 $encoder = $this->getEncoder($format, $context);
65 71
66 if (!$encoder instanceof NormalizationAwareInterface) { 72 if (!$encoder instanceof NormalizationAwareInterface) {
67 return true; 73 return true;
68 } 74 }
69 75
70 if ($encoder instanceof self) { 76 if ($encoder instanceof self) {
71 return $encoder->needsNormalization($format); 77 return $encoder->needsNormalization($format, $context);
72 } 78 }
73 79
74 return false; 80 return false;
75 } 81 }
76 82
77 /** 83 /**
78 * Gets the encoder supporting the format. 84 * Gets the encoder supporting the format.
79 * 85 *
80 * @param string $format 86 * @param string $format
87 * @param array $context
81 * 88 *
82 * @return EncoderInterface 89 * @return EncoderInterface
83 * 90 *
84 * @throws RuntimeException if no encoder is found 91 * @throws RuntimeException if no encoder is found
85 */ 92 */
86 private function getEncoder($format) 93 private function getEncoder($format, array $context)
87 { 94 {
88 if (isset($this->encoderByFormat[$format]) 95 if (isset($this->encoderByFormat[$format])
89 && isset($this->encoders[$this->encoderByFormat[$format]]) 96 && isset($this->encoders[$this->encoderByFormat[$format]])
90 ) { 97 ) {
91 return $this->encoders[$this->encoderByFormat[$format]]; 98 return $this->encoders[$this->encoderByFormat[$format]];
92 } 99 }
93 100
94 foreach ($this->encoders as $i => $encoder) { 101 foreach ($this->encoders as $i => $encoder) {
95 if ($encoder->supportsEncoding($format)) { 102 if ($encoder->supportsEncoding($format, $context)) {
96 $this->encoderByFormat[$format] = $i; 103 $this->encoderByFormat[$format] = $i;
97 104
98 return $encoder; 105 return $encoder;
99 } 106 }
100 } 107 }