Horde_Serialize now throws exceptions
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 4 Mar 2010 19:37:57 +0000 (12:37 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 4 Mar 2010 19:38:08 +0000 (12:38 -0700)
framework/Serialize/lib/Horde/Serialize.php
framework/Serialize/lib/Horde/Serialize/Exception.php [new file with mode: 0644]
framework/Serialize/package.xml
framework/Serialize/test/Horde/Serialize/json_encode_decode.phpt

index 217285e..b5cd84e 100644 (file)
@@ -8,6 +8,7 @@
  * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  *
  * @author   Stephane Huther <shuther1@free.fr>
+ * @author   Michael Slusarz <slusarz@curecanti.org>
  * @package  Horde_Serialize
  * @category Horde
  */
@@ -48,7 +49,7 @@ class Horde_Serialize
      *                       requires.
      *
      * @return string  The serialized data.
-     *                 Returns PEAR_Error on error.
+     * @throws Horde_Serialize_Exception
      */
     static public function serialize($data, $mode = array(self::BASIC),
                                      $params = null)
@@ -61,12 +62,9 @@ class Horde_Serialize
         foreach ($mode as $val) {
             /* Check to make sure the mode is supported. */
             if (!self::hasCapability($val)) {
-                return PEAR::raiseError('Unsupported serialization type');
+                throw new Horde_Serialize_Exception('Unsupported serialization type');
             }
             $data = self::_serialize($data, $val, $params);
-            if ($data instanceOf PEAR_Error) {
-                break;
-            }
         }
 
         return $data;
@@ -86,7 +84,7 @@ class Horde_Serialize
      *                       method requires.
      *
      * @return string  The unserialized data.
-     *                 Returns PEAR_Error on error.
+     * @throws Horde_Serialize_Exception
      */
     static public function unserialize($data, $mode = self::BASIC,
                                        $params = null)
@@ -99,12 +97,9 @@ class Horde_Serialize
         foreach ($mode as $val) {
             /* Check to make sure the mode is supported. */
             if (!self::hasCapability($val)) {
-                return PEAR::raiseError('Unsupported unserialization type');
+                throw new Horde_Serialize_Exception('Unsupported unserialization type');
             }
             $data = self::_unserialize($data, $val, $params);
-            if (is_a($data, 'PEAR_Error')) {
-                break;
-            }
         }
 
         return $data;
@@ -170,7 +165,8 @@ class Horde_Serialize
      * @param mixed $params  Any additional parameters the serialization method
      *                       requires.
      *
-     * @return string  A serialized string or PEAR_Error on error.
+     * @return string  A serialized string.
+     * @throws Horde_Serialize_Exception
      */
     static protected function _serialize($data, $mode, $params = null)
     {
@@ -276,7 +272,7 @@ class Horde_Serialize
         }
 
         if ($data === false) {
-            return PEAR::raiseError('Serialization failed.');
+            throw new Horde_Serialize_Exception('Serialization failed.');
         }
         return $data;
     }
@@ -291,7 +287,8 @@ class Horde_Serialize
      * @param mixed $params  Any additional parameters the unserialization
      *                       method requires.
      *
-     * @return mixed  Unserialized data on success or PEAR_Error on error.
+     * @return mixed  Unserialized data.
+     * @throws Horde_Serialize_Exception
      */
     static protected function _unserialize(&$data, $mode, $params = null)
     {
@@ -371,8 +368,9 @@ class Horde_Serialize
         }
 
         if ($data === false) {
-            return PEAR::raiseError('Unserialization failed.');
+            throw new Horde_Serialize_Exception('Unserialization failed.');
         }
+
         return $data;
     }
 
diff --git a/framework/Serialize/lib/Horde/Serialize/Exception.php b/framework/Serialize/lib/Horde/Serialize/Exception.php
new file mode 100644 (file)
index 0000000..e94dc2b
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+/**
+ * Exception handler for the Horde_Serialize package.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @package  Horde_Serialize
+ */
+class Horde_Serialize_Exception extends Horde_Exception_Prior
+{
+}
index 9b266d8..b2f1f12 100644 (file)
@@ -31,13 +31,17 @@ http://pear.php.net/dtd/package-2.0.xsd">
   <api>beta</api>
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Add support for limited error handling for json encoding errors
+ <notes>* Throw exceptions rather than returning PEAR_Errors.
+ * Add support for limited error handling for json encoding errors
  (requires PHP 5.3+).
  * Initial Horde 4 package.</notes>
  <contents>
   <dir name="/">
    <dir name="lib">
     <dir name="Horde">
+     <dir name="Serialize">
+      <file name="Exception.php" role="php" />
+     </dir> <!-- /lib/Horde/Serialize -->
      <file name="Serialize.php" role="php" />
     </dir> <!-- /lib/Horde -->
    </dir> <!-- /lib/ -->
@@ -69,6 +73,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
     <min>1.5.0</min>
    </pearinstaller>
    <package>
+    <name>Exception</name>
+    <channel>pear.horde.org</channel>
+   </package>
+   <package>
     <name>Util</name>
     <channel>pear.horde.org</channel>
    </package>
@@ -76,6 +84,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
  </dependencies>
  <phprelease>
   <filelist>
+   <install name="lib/Horde/Serialize/Exception.php" as="Horde/Serialize/Exception.php" />
    <install name="lib/Horde/Serialize.php" as="Horde/Serialize.php" />
   </filelist>
  </phprelease>
index 50b3898..71ee6cb 100644 (file)
@@ -5,7 +5,6 @@ JSON encode/decode tests.
 
 error_reporting(E_ALL);
 require dirname(__FILE__) . '/../../../lib/Horde/Serialize.php';
-require 'PEAR.php';
 
 function out($str)
 {