Skip to content

Commit

Permalink
removed redundant management of encoders and decoders, aka assume all…
Browse files Browse the repository at this point in the history
… decoders are also encoders

anything else is a total edge case that doesnt break with this change. it just means that for that edge case it will not be possible to "statically" determine if the encoder doesnt actually support encoding.
  • Loading branch information
lsmith77 committed Jun 9, 2011
1 parent 34b5a67 commit e694397
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 85 deletions.
Expand Up @@ -18,7 +18,7 @@
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
interface DecoderInterface
interface DecoderInterface extends EncoderInterface
{
/**
* Decodes a string into PHP data
Expand Down
99 changes: 15 additions & 84 deletions src/Symfony/Component/Serializer/Serializer.php
Expand Up @@ -30,9 +30,8 @@
*/
class Serializer implements SerializerInterface
{
private $normalizers = array();
private $encoders = array();
private $decoders = array();
protected $normalizers = array();
protected $encoders = array();
protected $normalizerCache = array();
protected $denormalizerCache = array();

Expand Down Expand Up @@ -112,19 +111,15 @@ public function encode($data, $format)
*/
public function decode($data, $format)
{
if (!isset($this->decoders[$format])) {
if (!isset($this->encoders[$format])) {
throw new \UnexpectedValueException('No decoder registered for the '.$format.' format');
}

return $this->decoders[$format]->decode($data, $format);
return $this->encoders[$format]->decode($data, $format);
}

/**
* Normalizes an object into a set of arrays/scalars
*
* @param object $object object to normalize
* @param string $format format name, present to give the option to normalizers to act differently based on formats
* @return array|scalar
* {@inheritdoc}
*/
public function normalizeObject($object, $format = null)
{
Expand All @@ -146,12 +141,7 @@ public function normalizeObject($object, $format = null)
}

/**
* Denormalizes data back into an object of the given class
*
* @param mixed $data data to restore
* @param string $class the expected class to instantiate
* @param string $format format name, present to give the option to normalizers to act differently based on formats
* @return object
* {@inheritdoc}
*/
public function denormalizeObject($data, $class, $format = null)
{
Expand All @@ -172,7 +162,7 @@ public function denormalizeObject($data, $class, $format = null)
}

/**
* @param NormalizerInterface $normalizer
* {@inheritdoc}
*/
public function addNormalizer(NormalizerInterface $normalizer)
{
Expand All @@ -183,24 +173,15 @@ public function addNormalizer(NormalizerInterface $normalizer)
}

/**
* @return array[]NormalizerInterface
*/
public function getNormalizers()
{
return $this->normalizers;
}

/**
* @param NormalizerInterface $normalizer
* {@inheritdoc}
*/
public function removeNormalizer(NormalizerInterface $normalizer)
{
unset($this->normalizers[array_search($normalizer, $this->normalizers, true)]);
}

/**
* @param string $format format name
* @param EncoderInterface $encoder
* {@inheritdoc}
*/
public function setEncoder($format, EncoderInterface $encoder)
{
Expand All @@ -211,84 +192,34 @@ public function setEncoder($format, EncoderInterface $encoder)
}

/**
* @param string $format format name
* @param DecoderInterface $decoder
*/
public function setDecoder($format, DecoderInterface $decoder)
{
$this->decoders[$format] = $decoder;
if ($decoder instanceof SerializerAwareInterface) {
$decoder->setSerializer($this);
}
}

/**
* @return array[]EncoderInterface
*/
public function getEncoders()
{
return $this->encoders;
}

/**
* @return array[]DecoderInterface
*/
public function getDecoders()
{
return $this->decoders;
}

/**
* @return EncoderInterface
* {@inheritdoc}
*/
public function getEncoder($format)
{
return $this->encoders[$format];
}

/**
* @return DecoderInterface
*/
public function getDecoder($format)
{
return $this->decoders[$format];
}

/**
* Checks whether the serializer has an encoder registered for the given format
*
* @param string $format format name
* @return Boolean
* {@inheritdoc}
*/
public function hasEncoder($format)
{
return isset($this->encoders[$format]);
return isset($this->encoder[$format]) && $this->encoder[$format] instanceof EncoderInterface;
}

/**
* Checks whether the serializer has a decoder registered for the given format
*
* @param string $format format name
* @return Boolean
* {@inheritdoc}
*/
public function hasDecoder($format)
{
return isset($this->decoders[$format]);
return isset($this->encoder[$format]) && $this->encoder[$format] instanceof DecoderInterface;
}

/**
* @param string $format format name
* {@inheritdoc}
*/
public function removeEncoder($format)
{
unset($this->encoders[$format]);
}

/**
* @param string $format format name
*/
public function removeDecoder($format)
{
unset($this->decoders[$format]);
}
}

0 comments on commit e694397

Please sign in to comment.