浏览代码

restores UnsupportedFormatException (closes #8)

Johannes M. Schmitt 12 年之前
父节点
当前提交
2d181a95f7
共有 2 个文件被更改,包括 14 次插入1 次删除
  1. 12 0
      src/JMS/Serializer/Serializer.php
  2. 2 1
      tests/JMS/Serializer/Tests/SerializerBuilderTest.php

+ 12 - 0
src/JMS/Serializer/Serializer.php

@@ -40,7 +40,11 @@ class Serializer implements SerializerInterface
     private $objectConstructor;
     private $dispatcher;
     private $typeParser;
+
+    /** @var \PhpCollection\MapInterface */
     private $serializationVisitors;
+
+    /** @var \PhpCollection\MapInterface */
     private $deserializationVisitors;
     private $exclusionStrategy;
     private $serializeNull;
@@ -111,6 +115,10 @@ class Serializer implements SerializerInterface
 
     public function serialize($data, $format)
     {
+        if ( ! $this->serializationVisitors->containsKey($format)) {
+            throw new UnsupportedFormatException(sprintf('The format "%s" is not supported for serialization.', $format));
+        }
+
         $visitor = $this->serializationVisitors->get($format)->get();
         $visitor->setSerializeNull($this->serializeNull);
         $visitor->setNavigator($navigator = new GraphNavigator(GraphNavigator::DIRECTION_SERIALIZATION, $this->factory, $format, $this->handlerRegistry, $this->objectConstructor, $this->exclusionStrategy, $this->dispatcher));
@@ -121,6 +129,10 @@ class Serializer implements SerializerInterface
 
     public function deserialize($data, $type, $format)
     {
+        if ( ! $this->deserializationVisitors->containsKey($format)) {
+            throw new UnsupportedFormatException(sprintf('The format "%s" is not supported for deserialization.', $format));
+        }
+
         $visitor = $this->deserializationVisitors->get($format)->get();
         $visitor->setNavigator($navigator = new GraphNavigator(GraphNavigator::DIRECTION_DESERIALIZATION, $this->factory, $format, $this->handlerRegistry, $this->objectConstructor, $this->exclusionStrategy, $this->dispatcher));
         $navigatorResult = $navigator->accept($visitor->prepare($data), $this->typeParser->parse($type), $visitor);

+ 2 - 1
tests/JMS/Serializer/Tests/SerializerBuilderTest.php

@@ -62,7 +62,8 @@ class SerializerBuilderTest extends \PHPUnit_Framework_TestCase
     }
 
     /**
-     * @expectedException RuntimeException
+     * @expectedException JMS\Serializer\Exception\UnsupportedFormatException
+     * @expectedExceptionMessage The format "xml" is not supported for serialization.
      */
     public function testDoesNotAddOtherVisitorsWhenConfiguredExplicitly()
     {