Jelajahi Sumber

added Exception's from SerializerBundle

Lukas Kahwe Smith 14 tahun lalu
induk
melakukan
46da5ff069

+ 2 - 1
src/Symfony/Component/Serializer/Encoder/XmlEncoder.php

@@ -3,6 +3,7 @@
 namespace Symfony\Component\Serializer\Encoder;
 
 use Symfony\Component\Serializer\SerializerInterface;
+use Symfony\Component\Serializer\Exception\UnexpectedValueException;
 
 /*
  * This file is part of the Symfony framework.
@@ -268,7 +269,7 @@ class XmlEncoder extends SerializerAwareEncoder implements DecoderInterface, Nor
 
             return $this->appendNode($parentNode, $data, 'data');
         }
-        throw new \UnexpectedValueException('An unexpected value could not be serialized: '.var_export($data, true));
+        throw new UnexpectedValueException('An unexpected value could not be serialized: '.var_export($data, true));
     }
 
     /**

+ 21 - 0
src/Symfony/Component/Serializer/Exception/Exception.php

@@ -0,0 +1,21 @@
+<?php
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Symfony\Component\Serializer\Exception;
+
+/**
+ * Base exception
+ *
+ * @author Johannes M. Schmitt <schmittjoh@gmail.com>
+ */
+interface Exception
+{
+}

+ 21 - 0
src/Symfony/Component/Serializer/Exception/InvalidArgumentException.php

@@ -0,0 +1,21 @@
+<?php
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Symfony\Component\Serializer\Exception;
+
+/**
+ * InvalidArgumentException
+ *
+ * @author Johannes M. Schmitt <schmittjoh@gmail.com>
+ */
+class InvalidArgumentException extends \InvalidArgumentException implements Exception
+{
+}

+ 21 - 0
src/Symfony/Component/Serializer/Exception/LogicException.php

@@ -0,0 +1,21 @@
+<?php
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Symfony\Component\Serializer\Exception;
+
+/**
+ * LogicException
+ *
+ * @author Lukas Kahwe Smith <smith@pooteeweet.org>
+ */
+class LogicException extends \LogicException implements Exception
+{
+}

+ 21 - 0
src/Symfony/Component/Serializer/Exception/RuntimeException.php

@@ -0,0 +1,21 @@
+<?php
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Symfony\Component\Serializer\Exception;
+
+/**
+ * RuntimeException
+ *
+ * @author Johannes M. Schmitt <schmittjoh@gmail.com>
+ */
+class RuntimeException extends \RuntimeException implements Exception
+{
+}

+ 21 - 0
src/Symfony/Component/Serializer/Exception/UnexpectedValueException.php

@@ -0,0 +1,21 @@
+<?php
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Symfony\Component\Serializer\Exception;
+
+/**
+ * UnexpectedValueException
+ *
+ * @author Lukas Kahwe Smith <smith@pooteeweet.org>
+ */
+class UnexpectedValueException extends \UnexpectedValueException implements Exception
+{
+}

+ 21 - 0
src/Symfony/Component/Serializer/Exception/UnsupportedException.php

@@ -0,0 +1,21 @@
+<?php
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Symfony\Component\Serializer\Exception;
+
+/**
+ * UnsupportedException
+ *
+ * @author Johannes M. Schmitt <schmittjoh@gmail.com>
+ */
+class UnsupportedException extends InvalidArgumentException
+{
+}

+ 2 - 1
src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php

@@ -3,6 +3,7 @@
 namespace Symfony\Component\Serializer\Normalizer;
 
 use Symfony\Component\Serializer\SerializerInterface;
+use Symfony\Component\Serializer\Exception\RuntimeException;
 
 /*
  * This file is part of the Symfony framework.
@@ -80,7 +81,7 @@ class GetSetMethodNormalizer extends SerializerAwareNormalizer
                     // don't run set for a parameter passed to the constructor
                     unset($data[$paramName]);
                 } else if (!$constructorParameter->isOptional()) {
-                    throw new \RuntimeException(
+                    throw new RuntimeException(
                         'Cannot create an instance of ' . $class .
                         ' from serialized data because its constructor requires ' .
                         'parameter "' . $constructorParameter->getName() .

+ 13 - 8
src/Symfony/Component/Serializer/Serializer.php

@@ -6,6 +6,9 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
 use Symfony\Component\Serializer\Encoder\EncoderInterface;
 use Symfony\Component\Serializer\Encoder\DecoderInterface;
 use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
+use Symfony\Component\Serializer\Exception\RuntimeException;
+use Symfony\Component\Serializer\Exception\LogicException;
+use Symfony\Component\Serializer\Exception\UnexpectedValueException;
 
 /*
  * This file is part of the Symfony framework.
@@ -41,7 +44,7 @@ class Serializer implements SerializerInterface
     public function serialize($data, $format)
     {
         if (!isset($this->encoders[$format])) {
-            throw new \UnexpectedValueException('No encoder registered for the '.$format.' format');
+            throw new UnexpectedValueException('No encoder registered for the '.$format.' format');
         }
         if (!$this->encoders[$format] instanceof NormalizationAwareInterface) {
             $data = $this->normalize($data);
@@ -83,7 +86,7 @@ class Serializer implements SerializerInterface
 
             return $data;
         }
-        throw new \UnexpectedValueException('An unexpected value could not be normalized: '.var_export($data, true));
+        throw new UnexpectedValueException('An unexpected value could not be normalized: '.var_export($data, true));
     }
 
     /**
@@ -128,7 +131,7 @@ class Serializer implements SerializerInterface
     private function normalizeObject($object, $format = null)
     {
         if (!$this->normalizers) {
-            throw new \LogicException('You must register at least one normalizer to be able to normalize objects.');
+            throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
         }
         $class = get_class($object);
         if (isset($this->normalizerCache[$class][$format])) {
@@ -141,7 +144,7 @@ class Serializer implements SerializerInterface
                 return $normalizer->normalize($object, $format);
             }
         }
-        throw new \UnexpectedValueException('Could not normalize object of type '.$class.', no supporting normalizer found.');
+        throw new UnexpectedValueException('Could not normalize object of type '.$class.', no supporting normalizer found.');
     }
 
     /**
@@ -155,7 +158,7 @@ class Serializer implements SerializerInterface
     private function denormalizeObject($data, $class, $format = null)
     {
         if (!$this->normalizers) {
-            throw new \LogicException('You must register at least one normalizer to be able to denormalize objects.');
+            throw new LogicException('You must register at least one normalizer to be able to denormalize objects.');
         }
         if (isset($this->denormalizerCache[$class][$format])) {
             return $this->denormalizerCache[$class][$format]->denormalize($data, $class, $format);
@@ -167,7 +170,7 @@ class Serializer implements SerializerInterface
                 return $normalizer->denormalize($data, $class, $format);
             }
         }
-        throw new \UnexpectedValueException('Could not denormalize object of type '.$class.', no supporting normalizer found.');
+        throw new UnexpectedValueException('Could not denormalize object of type '.$class.', no supporting normalizer found.');
     }
 
     /**
@@ -205,9 +208,11 @@ class Serializer implements SerializerInterface
      */
     public function getEncoder($format)
     {
-        if (isset($this->encoders[$format])) {
-            return $this->encoders[$format];
+        if (!isset($this->encoders[$format])) {
+            throw new RuntimeException(sprintf('No encoder found for format "%s".', $format));
         }
+
+        return $this->encoders[$format];
     }
 
     /**