more encoder lazy loading tweaks

This commit is contained in:
Lukas Kahwe Smith 2011-06-09 14:18:47 +02:00
parent 4b3f6215b7
commit f67b3f508e
2 changed files with 57 additions and 7 deletions

View File

@ -62,10 +62,12 @@ class Serializer implements SerializerInterface
*/ */
public final function serialize($data, $format) public final function serialize($data, $format)
{ {
$encoder = $this->getEncoder($format); if (!$this->supportsSerialization($format)) {
if (!isset($encoder)) { throw new UnexpectedValueException('Serialization for the format '.$format.' is not supported');
throw new UnexpectedValueException('No encoder registered for the '.$format.' format');
} }
$encoder = $this->getEncoder($format);
if (!$encoder instanceof NormalizationAwareInterface) { if (!$encoder instanceof NormalizationAwareInterface) {
$data = $this->normalize($data); $data = $this->normalize($data);
} }
@ -78,6 +80,10 @@ class Serializer implements SerializerInterface
*/ */
public final function deserialize($data, $type, $format) public final function deserialize($data, $type, $format)
{ {
if (!$this->supportsDeserialization($format)) {
throw new UnexpectedValueException('Deserialization for the format '.$format.' is not supported');
}
$data = $this->decode($data, $format); $data = $this->decode($data, $format);
return $this->denormalize($data, $type, $format); return $this->denormalize($data, $type, $format);
@ -193,7 +199,7 @@ class Serializer implements SerializerInterface
*/ */
public function supportsSerialization($format) public function supportsSerialization($format)
{ {
return isset($this->encoders[$format]); return $this->supportsEncoding($format);
} }
/** /**
@ -201,7 +207,35 @@ class Serializer implements SerializerInterface
*/ */
public function supportsDeserialization($format) public function supportsDeserialization($format)
{ {
return isset($this->encoders[$format]) && $this->encoders[$format] instanceof DecoderInterface; return $this->supportsDecoding($format);
}
/**
* {@inheritdoc}
*/
public function supportsEncoding($format)
{
try {
$encoder = $this->getEncoder($format);
} catch (\RuntimeException $e) {
return false;
}
return $encoder instanceof EncoderInterface;
}
/**
* {@inheritdoc}
*/
public function supportsDecoding($format)
{
try {
$encoder = $this->getEncoder($format);
} catch (\RuntimeException $e) {
return false;
}
return $encoder instanceof DecoderInterface;
} }
/** /**

View File

@ -84,7 +84,7 @@ interface SerializerInterface
function decode($data, $format); function decode($data, $format);
/** /**
* Checks whether the serializer can serialize the given format * Checks whether the serializer can serialize to given format
* *
* @param string $format format name * @param string $format format name
* @return Boolean * @return Boolean
@ -92,13 +92,29 @@ interface SerializerInterface
function supportsSerialization($format); function supportsSerialization($format);
/** /**
* Checks whether the serializer can deserialize the given format * Checks whether the serializer can deserialize from given format
* *
* @param string $format format name * @param string $format format name
* @return Boolean * @return Boolean
*/ */
function supportsDeserialization($format); function supportsDeserialization($format);
/**
* Checks whether the serializer can encode to given format
*
* @param string $format format name
* @return Boolean
*/
function supportsEncoding($format);
/**
* Checks whether the serializer can decode from given format
*
* @param string $format format name
* @return Boolean
*/
function supportsDecoding($format);
/** /**
* @return EncoderInterface * @return EncoderInterface
*/ */