Skip to content

Commit

Permalink
[Serializer] Added hasEncoder and getEncoder to the SerializerInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek authored and fabpot committed Jan 25, 2011
1 parent 6f5c2e2 commit 08f8b22
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/Symfony/Component/Serializer/Serializer.php
Expand Up @@ -105,7 +105,7 @@ public function normalize($data, $format)
*/
public function encode($data, $format)
{
if (!isset($this->encoders[$format])) {
if (!$this->hasEncoder($format)) {
throw new \UnexpectedValueException('No encoder registered for the '.$format.' format');
}
return $this->encoders[$format]->encode($data, $format);
Expand All @@ -116,7 +116,7 @@ public function encode($data, $format)
*/
public function decode($data, $format)
{
if (!isset($this->encoders[$format])) {
if (!$this->hasEncoder($format)) {
throw new \UnexpectedValueException('No encoder registered to decode the '.$format.' format');
}
return $this->encoders[$format]->decode($data, $format);
Expand All @@ -138,17 +138,42 @@ public function removeNormalizer(NormalizerInterface $normalizer)
unset($this->normalizers[array_search($normalizer, $this->normalizers, true)]);
}

/**
* {@inheritdoc}
*/
public function addEncoder($format, EncoderInterface $encoder)
{
$this->encoders[$format] = $encoder;
$encoder->setSerializer($this);
}

/**
* {@inheritdoc}
*/
public function getEncoders()
{
return $this->encoders;
}

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

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

/**
* {@inheritdoc}
*/
public function removeEncoder($format)
{
unset($this->encoders[$format]);
Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/Serializer/SerializerInterface.php
Expand Up @@ -2,6 +2,8 @@

namespace Symfony\Component\Serializer;

use Symfony\Component\Serializer\Encoder\EncoderInterface;

/*
* This file is part of the Symfony framework.
*
Expand Down Expand Up @@ -73,4 +75,34 @@ function encode($data, $format);
* @return mixed
*/
function decode($data, $format);

/**
* @param string $format format name
* @param EncoderInterface $encoder
*/
function addEncoder($format, EncoderInterface $encoder);

/**
* @param string $format format name
* @return EncoderInterface
*/
function getEncoders();

/**
* @return array[]EncoderInterface
*/
function getEncoder($format);

/**
* Checks whether the serializer has an encoder registered for the given format
*
* @param string $format format name
* @return Boolean
*/
function hasEncoder($format);

/**
* @param string $format format name
*/
function removeEncoder($format);
}

0 comments on commit 08f8b22

Please sign in to comment.