Advertisement
bagnz0r

Untitled

Oct 27th, 2011
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.56 KB | None | 0 0
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2.  
  3. /**
  4.  *
  5.  * RPC Object Module for Kohana 3.2
  6.  * The RPC object class
  7.  *
  8.  * @author bagnz0r (http://bagnohax.pl)
  9.  * @package RpcObject
  10.  *
  11.  */
  12.  
  13. class RpcObject {
  14.    
  15.     /**
  16.      * Object data that will be serialized
  17.      *
  18.      * @var object
  19.      */
  20.     public $data;
  21.    
  22.     /**
  23.      * Empty constructor does nothing
  24.      */
  25.     public function __construct() {}
  26.    
  27.     /**
  28.      * An overload that will deserialize string and build data out of it
  29.      *
  30.      * @param string $string
  31.      * @param string $deserializer For example: 'json' or 'xml'
  32.      */
  33.     public function __construct($string, $deserializer = 'json')
  34.     {
  35.         // Construct serializer/deserializer
  36.         $class = 'rpcserializer' . $deserializer;
  37.        
  38.         // Deserialize data
  39.         try {
  40.             $this->data = $class::deserialize($string);
  41.         }
  42.         catch (Exception $e)
  43.         {
  44.             throw new Kohana_Exception('RpcObject: Could not deserialize data! Message: ' . $e->getMessage());
  45.         }
  46.     }
  47.    
  48.     /**
  49.      * Serialize data as XML
  50.      *
  51.      * @return string
  52.      */
  53.     public function __toXml()
  54.     {
  55.         try {
  56.             return RpcXmlSerializer::serialize($this->data);
  57.         }
  58.         catch (Exception $e)
  59.         {
  60.             throw new Kohana_Exception('RpcObject: Could not serialize data! Message: ' . $e->getMessage());
  61.         }
  62.     }
  63.    
  64.     /**
  65.      * Serialize data as JSON
  66.      *
  67.      * @return string
  68.      */
  69.     public function __toJson()
  70.     {
  71.         try {
  72.             return RpcJsonSerializer::serialize($this->data);
  73.         }
  74.         catch (Exception $e)
  75.         {
  76.             throw new Kohana_Exception('RpcObject: Could not serialize data! Message: ' . $e->getMessage());
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement