ChainEncoder.php in Zircon Profile 8
File
vendor/symfony/serializer/Encoder/ChainEncoder.php
View source
<?php
namespace Symfony\Component\Serializer\Encoder;
use Symfony\Component\Serializer\Exception\RuntimeException;
class ChainEncoder implements EncoderInterface {
protected $encoders = array();
protected $encoderByFormat = array();
public function __construct(array $encoders = array()) {
$this->encoders = $encoders;
}
public final function encode($data, $format, array $context = array()) {
return $this
->getEncoder($format)
->encode($data, $format, $context);
}
public function supportsEncoding($format) {
try {
$this
->getEncoder($format);
} catch (RuntimeException $e) {
return false;
}
return true;
}
public function needsNormalization($format) {
$encoder = $this
->getEncoder($format);
if (!$encoder instanceof NormalizationAwareInterface) {
return true;
}
if ($encoder instanceof self) {
return $encoder
->needsNormalization($format);
}
return false;
}
private function getEncoder($format) {
if (isset($this->encoderByFormat[$format]) && isset($this->encoders[$this->encoderByFormat[$format]])) {
return $this->encoders[$this->encoderByFormat[$format]];
}
foreach ($this->encoders as $i => $encoder) {
if ($encoder
->supportsEncoding($format)) {
$this->encoderByFormat[$format] = $i;
return $encoder;
}
}
throw new RuntimeException(sprintf('No encoder found for format "%s".', $format));
}
}
Classes
Name |
Description |
ChainEncoder |
Encoder delegating the decoding to a chain of encoders. |