XML_Wddx
[ class tree: XML_Wddx ] [ index: XML_Wddx ] [ all elements ]

Source for file Wddx.php

Documentation is available at Wddx.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors:  Alan Knowles <alan@akbkhome.com>                           |
  17. // +----------------------------------------------------------------------+
  18. //
  19. //
  20.  
  21.  
  22. /**
  23. * XML_Wddx : WDDX serializer and deserializer (works with or without the wddx extension)
  24. *
  25. @abstract
  26. *  serialization is done by   $string = XML_Wddx::serialize($data);
  27. *  deserialization is done by $data   = XML_Wddx::deserialize($string);
  28. *
  29. @version    $Id: Wddx.php,v 1.6 2004/08/13 01:40:37 alan_k Exp $
  30. */
  31.  
  32. require_once 'XML/Parser.php';
  33.  
  34. class XML_Wddx extends XML_Parser {
  35.  
  36.  
  37.     /**
  38.     * 
  39.     *
  40.     * serialize a value
  41.     * usage:
  42.     *       echo XML_Wddx::serialize($array);
  43.     * 
  44.     * @param   mixed    value to serialize
  45.     * 
  46.     *
  47.     * @return   string   Serialize data.
  48.     * @access   public
  49.     * @static
  50.     */
  51.   
  52.  
  53.     function serialize($value
  54.     {
  55.         $x = new XML_Wddx;
  56.         return  "<wddxPacket version='1.0'><header/><data>\n"
  57.             $x->indent(1trim($x->_serializeValue($value)) "\n"
  58.             $x->indent(-1"</data></wddxPacket>\n";
  59.     }
  60.     
  61.     /**
  62.     * 
  63.     *
  64.     * de-serialize a value (uses wddx_deserialize if it is built in..)
  65.     * usage:
  66.     *       echo XML_Wddx::deserialize($some_wddx_data);
  67.     * 
  68.     * @param   mixed    value to serialize
  69.     * 
  70.     *
  71.     * @return   mixed   deserialized data..
  72.     * @access   public
  73.     * @static
  74.  
  75.     */
  76.     
  77.     function deserialize($data
  78.     {
  79.         if (function_exists('wddx_deserialize')) {
  80.             return wddx_deserialize($data);
  81.         }
  82.         $t &new XML_Wddx;
  83.         $t->XML_Parser();
  84.         
  85.         $t->parseString($data);
  86.         return $t->result['data'];
  87.     }
  88.     
  89.     
  90.     /**
  91.     * The core method.. that serializes data.
  92.     * 
  93.     * @param   mixed  value to serialize
  94.     *
  95.     * @return   string   serialized value.
  96.     * @access   private
  97.     * @see      see also methods.....
  98.     */
  99.   
  100.     
  101.     function _serializeValue($value
  102.     {
  103.         switch (gettype($value)) {
  104.             case 'string':
  105.                 if is_numeric ($value&&  (intval(0+$value==  $value) )   {
  106.                     return "<number>$value</number>";
  107.                 }    
  108.                 //$this->indent(1);
  109.                 return  preg_match('/[^a-z0-9_ ]/i',$value
  110.                     "\n".$this->indent(0).'<string><![CDATA['.$value."]]></string>\n" 
  111.                     "<string>$value</string>";
  112.                 //$this->indent(-1);
  113.                 
  114.             case 'integer':
  115.             case 'float':
  116.             case 'double':
  117.                 return "<number>$value</number>";
  118.                 
  119.             case 'boolean':
  120.                 return  sprintf("<boolean value='%s'/>",$value 'true':'false');
  121.  
  122.             case 'object':
  123.                 // sleep - ignored ATM
  124.                 $ret "\n".$this->indent()."<struct>\n".
  125.                     $this->indent(1)."<var name='php_class_name'><string>".get_class($value)."</string></var>\n";
  126.                 
  127.                 foreach(get_object_vars($valueas $k=>$v{
  128.                     $ret .= $this->indent(0).sprintf("<var name='%s'>",$k);
  129.                     $this->indent(1);
  130.                     $ret .= $this->_serializeValue($v);
  131.                     $this->indent(-1);
  132.                     $ret .= ($ret{strlen($ret)-1== "\n"$this->indent('';
  133.                     $ret .= "</var>\n";
  134.                 }
  135.                 
  136.                 $this->indent(-1);
  137.                 return $ret .  $this->indent("</struct>\n"
  138.                 
  139.             case 'array':
  140.  
  141.                 $is_struct (array_keys($value!== range(0,sizeof($value)-1));
  142.                 $ret "\n".$this->indent();
  143.                 $ret .= $is_struct "<struct>\n" sprintf("<array length='%d'>",count($value))"\n";
  144.                 $this->indent(1);
  145.                 foreach($value as $k=>$v{
  146.                     $ret .= $this->indent(0);
  147.                     $ret .= $is_struct sprintf("<var name='%s'>",$k'<var>';
  148.                     $this->indent(1);
  149.                     $ret .= $this->_serializeValue($v;
  150.                     $this->indent(-1);
  151.                     $ret .= ($ret{strlen($ret)-1== "\n"$this->indent('';
  152.                     $ret .= "</var>\n";
  153.                 }
  154.                 
  155.                 $ret .= $this->indent(-1);
  156.                 $ret .= $is_struct '</struct>' '</array>';
  157.                 return $ret "\n";
  158.             case 'resource'// BIG KLUDGE!!!!
  159.             case 'NULL':
  160.                 return  "<null/>";
  161.                
  162.             default:
  163.                 echo "not handled " gettype($value);
  164.                 exit;
  165.  
  166.         }
  167.         
  168.     }
  169.     
  170.         
  171.     /**
  172.     * Current indent level.
  173.     *
  174.     * @var int level
  175.     * @access private
  176.     */
  177.     var $_indent = 0;
  178.     
  179.     
  180.     
  181.     /**
  182.     * get an indent string
  183.     * 
  184.     * @param   int change (indent increment or decrement)
  185.     * 
  186.     * @return   string spaces
  187.     * @access   private
  188.     */
  189.   
  190.     function indent($add=0
  191.     {
  192.         $this->_indent += $add;
  193.         if ($add < 0// should not happen!!
  194.             $add = 0;
  195.         }
  196.         return str_repeat('  ',$this->_indent);
  197.     }
  198.     
  199.     
  200.       
  201.     /**
  202.     * expat start handler.
  203.     * 
  204.     * @return   none 
  205.     * @access   private
  206.     * @see      XML_Parser:startHandler
  207.     */
  208.     function startHandler($xp$element$attribs
  209.     {    
  210.         $ent = array('type'=>strtolower($element));
  211.        // echo "S:";print_r(func_get_args());
  212.       
  213.         switch (strtolower($element)) {
  214.             case 'wddxpacket':
  215.             case 'header':
  216.                 break;
  217.             case 'string':
  218.             case 'binary':
  219.                 $ent['data''';
  220.                 array_push($this->_stack,$ent);
  221.                 break;
  222.             
  223.             case 'number':
  224.                 $ent['data'= 0;
  225.                 array_push($this->_stack,$ent);
  226.                 break;
  227.             case 'boolean':
  228.                 $ent['data'= false;
  229.                 array_push($this->_stack,$ent);
  230.                 break;
  231.             case 'null':
  232.                 $ent['data'= null;
  233.                 array_push($this->_stack,$ent);
  234.                 break;
  235.                 
  236.                 
  237.             case 'char':
  238.                 if (isset($attribs['CODE'])) {
  239.                     $e $this->_stackTop();
  240.                     $e['data'.= chr(hexdec($attribs['CODE']));
  241.                     $this->_stackTop($e);
  242.                 }
  243.                 break;
  244.             
  245.             case 'struct':
  246.             case 'array':
  247.                 $ent['data'= array();
  248.                 array_push($this->_stack,$ent);
  249.                 break;
  250.             case 'var':
  251.                 $ent['name'@$attribs['NAME'];
  252.                 
  253.                 array_push($this->_stack,$ent);
  254.                 break;
  255.             case 'recordset':
  256.                 break; // not handled yet...
  257.         }
  258.         
  259.         //echo "STACK:";print_r($this->stack);
  260.         //echo "S:";print_r(func_get_args());
  261.     }
  262.     /**
  263.     * expat end handler.
  264.     * 
  265.     * @return   none 
  266.     * @access   private
  267.     * @see      XML_Parser:startHandler
  268.     */
  269.     function endHandler($xp$element
  270.     {        
  271.         //echo "E:";print_r(func_get_args());
  272.     
  273.         if (!count($this->_stack)) {
  274.             return;
  275.         }
  276.         $parent = null;
  277.         switch (strtolower($element)) {
  278.             case 'packet':
  279.       
  280.             
  281.             case 'char':
  282.             case 'recordset':
  283.                 return;
  284.                 
  285.                 
  286.             case 'string':
  287.             case 'binary':
  288.             case 'number':
  289.             case 'boolean':
  290.             case 'null':
  291.             case 'array':
  292.             case 'struct':
  293.             case 'var':
  294.                 
  295.  
  296.                 $ent array_pop($this->_stack);
  297.                 $parent = false;
  298.                 $parent $this->_stackTop();
  299.                 if (!$parent{
  300.                     $this->result $ent;
  301.                     break;
  302.                 }
  303.                 
  304.                 // if this is a struct + php_class_name is set...
  305.                 if (($ent['type'== 'struct'&& isset($ent['data']['php_class_name'])) {
  306.                     $class $ent['data']['php_class_name'];
  307.                     $obj = new $class;
  308.                     unset($ent['data']['php_class_name']);
  309.                     foreach($ent['data'as $k=>$v{
  310.                     
  311.                         $obj->$k $v;
  312.                     }
  313.                     $ent['data'$obj;
  314.                 }
  315.                 
  316.                 
  317.                 // add ent to parent...
  318.                 
  319.                 if ($parent['type'== 'var'{
  320.                     $parent['data'$ent['data'];
  321.                     break;
  322.                 }
  323.                 
  324.                 if ($ent['type'== 'var'{
  325.                     if ($parent['type'== 'struct'{
  326.                         if ($ent['name']{
  327.                             $parent['data'][$ent['name']] $ent['data'];
  328.                         else {
  329.                             $parent['data'][$ent['data'];
  330.                         }
  331.                         break;
  332.                     }
  333.                 }
  334.                 if ($parent['type'== 'array'{
  335.                     $parent['data'][$ent['data'];
  336.                     break;
  337.                 }
  338.                 
  339.                 $parent['data'$ent['data'];
  340.                 
  341.                 
  342.                 break;
  343.                 
  344.         
  345.         // put it back .. 
  346.         $this->_stackTop($parent);
  347.     
  348.        // echo "STACK:";print_r($this->stack);
  349.     
  350.     
  351.         //echo "E:";print_r(func_get_args());
  352.     }
  353.     
  354.     /**
  355.     * expat cdata handler.
  356.     * 
  357.     * @return   none 
  358.     * @access   private
  359.     * @see      XML_Parser:cdataHandler
  360.     */
  361.     function cdataHandler($xp$cdata)      
  362.     {    
  363.         //$ent = array('type'=>false);
  364.         if (!count($this->_stack)) {
  365.             return;
  366.         }
  367.     
  368.         $ent $this->_stackTop();
  369.         
  370.         //var_dump($ent);
  371.         switch($ent['type']{
  372.             case 'string':
  373.             case 'binary':
  374.                 $ent['data'.= $cdata;
  375.                 break;
  376.             case 'number':
  377.                 $ent['data'$cdata;
  378.                 break;
  379.             case 'boolean':
  380.                 $ent['data'$cdata == 'true' ? true : false;
  381.                 break;
  382.                 
  383.             case 'datetime'// not really handled...
  384.                 $ent['data'$cdata;
  385.                 break;
  386.             default:
  387.                 return;
  388.             
  389.                 
  390.                 
  391.         }
  392.         $this->_stackTop($ent);
  393.         //echo "C:";print_r(func_get_args());
  394.         //echo "STACK: "; print_r($this->stack);
  395.         //echo "C:";print_r(func_get_args());
  396.     }
  397.     /**
  398.     * expat default handler.
  399.     * 
  400.     * @return   none 
  401.     * @access   private
  402.     * @see      XML_Parser::defaultHandler
  403.     */
  404.     function defaultHandler($xp$cdata
  405.     {
  406.         //echo "D:";print_r(func_get_args());
  407.     }
  408.     /**
  409.     * Current indent level.
  410.     *
  411.     * @var array stack
  412.     * @access private
  413.     */
  414.      
  415.     var $_stack = array();
  416.      /**
  417.     * get/set top of stack values..
  418.     * 
  419.     * @param   array optional  (array if it is to be changed..)
  420.     * @return   array|none  (empty parameter = get)
  421.     * @access   private
  422.     */
  423.     function _stackTop($ent = null
  424.     {
  425.         
  426.         if ($ent != null{
  427.             $this->_stack[count($this->_stack)-1$ent;
  428.             return;
  429.         }
  430.         if (count($this->_stack)) {
  431.             return  $this->_stack[count($this->_stack)-1];
  432.         
  433.         }
  434.         return;
  435.     }
  436.             
  437.     
  438.     
  439.     
  440. }
  441. /*
  442. // test...
  443. $o = new StdClass;
  444. $o->x = "vvvv";
  445. $ar = array(
  446.     'a' => 1,
  447.     'b' => "TESTING \n 123\n",
  448.     'c' => $o,
  449.     'd' => array('x','y','z')
  450. );
  451. print_r(XML_Wddx::serialize($ar));
  452.  
  453. echo wddx_serialize_value($ar);
  454.  
  455. */

Documentation generated on Mon, 11 Mar 2019 13:54:39 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.