Xoad 0.5.0.0 > Classes / Serializer.class.php

Questions? Need Help? Want to share? » PHP Forum
Script Name:
Xoad 0.5.0.0

Download:
xoad-0.5.0.0.zip

Category:
Development Tools

Archive Content:

Xoad 0.5.0.0

Xoad 0.5.0.0 > Classes

Xoad 0.5.0.0 > Classes > Events > Storage

Xoad 0.5.0.0 > Classes > Events

Xoad 0.5.0.0 > Classes

Xoad 0.5.0.0 > Config

Xoad 0.5.0.0

Xoad 0.5.0.0 > Docs > Source

Xoad 0.5.0.0 > Docs > Source > Media

Xoad 0.5.0.0 > Docs > Source > Media > Images

Xoad 0.5.0.0 > Docs > Source > Media > Lib

Xoad 0.5.0.0 > Docs > Source > Media

Xoad 0.5.0.0 > Docs > Source

Xoad 0.5.0.0 > Docs > Source > XOAD > XOAD Cache

Xoad 0.5.0.0 > Docs > Source > XOAD

Xoad 0.5.0.0 > Docs > Source > XOAD > XOAD Events

Xoad 0.5.0.0 > Docs > Source > XOAD > XOAD HTML

Xoad 0.5.0.0 > Docs > Source > XOAD

Xoad 0.5.0.0 > Docs > Source > Filesource

Xoad 0.5.0.0 > Docs > Tutorials

Xoad 0.5.0.0 > Docs > Tutorials > Images

Xoad 0.5.0.0 > Docs > Tutorials > Style

Xoad 0.5.0.0 > Examples > Basic

Xoad 0.5.0.0 > Examples > Chat

Xoad 0.5.0.0 > Examples > Chat > Images

Xoad 0.5.0.0 > Examples > Chat

Xoad 0.5.0.0 > Examples > Chat > Var

Xoad 0.5.0.0 > Examples > ChatAdvanced

Xoad 0.5.0.0 > Examples > ChatAdvanced > Images > Default

Xoad 0.5.0.0 > Examples > ChatAdvanced

Xoad 0.5.0.0 > Examples > ChatAdvanced > Smilies

Xoad 0.5.0.0 > Examples > ChatAdvanced > Style

Xoad 0.5.0.0 > Examples > Events

Xoad 0.5.0.0 > Examples > Exam

Xoad 0.5.0.0 > Examples > Explorer

Xoad 0.5.0.0 > Examples > Explorer > Images

Xoad 0.5.0.0 > Examples > Explorer

Xoad 0.5.0.0 > Examples > Explorer > Style

Xoad 0.5.0.0 > Examples > Forms

Xoad 0.5.0.0 > Examples > Html > Content

Xoad 0.5.0.0 > Examples > Html

Xoad 0.5.0.0 > Extensions > Cache

Xoad 0.5.0.0 > Extensions > Cache > Classes

Xoad 0.5.0.0 > Extensions > Cache > Classes > Storage

Xoad 0.5.0.0 > Extensions > Cache > Classes

Xoad 0.5.0.0 > Extensions > Html > Classes > DOM

Xoad 0.5.0.0 > Extensions > Html > Classes

Xoad 0.5.0.0 > Extensions > Html

Xoad 0.5.0.0 > Extensions > Html > Js

Xoad 0.5.0.0

Xoad 0.5.0.0 > Js

Xoad 0.5.0.0

Xoad 0.5.0.0 > Tests

Xoad 0.5.0.0

Serializer.class.php:


<?php
/**
 * XOAD Serializer file.
 *
 * <p>This file defines the {@link XOAD_Serializer} Class.</p>
 * <p>Example:</p>
 * <code>
 * <?php
 *
 * require_once('xoad.php');
 *
 * $arr = array(1,  2,  "String",  array("Nested",  3,  4,  array(5,  6)));
 *
 * ?>
 * <script type="text/javascript">
 *
 * var arr = <?= XOAD_Serializer::serialize($arr) ?>;
 *
 * </script>
 * </code>
 *
 * @author    Stanimir Angeloff
 *
 * @package    XOAD
 *
 * @version    0.5.0.0
 *
 */

/**
 * XOAD Serializer Class.
 *
 * <p>This class is used to serialize a PHP variable
 * into a {@link http://www.json.org JSON} string.</p>
 * <p>Example:</p>
 * <code>
 * <?php
 *
 * require_once('xoad.php');
 *
 * $arr = array(1,  2,  "String",  array("Nested",  3,  4,  array(5,  6)));
 *
 * ?>
 * <script type="text/javascript">
 *
 * var arr = <?= XOAD_Serializer::serialize($arr) ?>;
 *
 * </script>
 * </code>
 *
 * @author        Stanimir Angeloff
 *
 * @package        XOAD
 *
 * @version        0.5.0.0
 *
 */
class XOAD_Serializer extends XOAD_Observable
{
    
/**
     * Serializes a PHP variable into a {@link http://www.json.org JSON} string.
     *
     * <p>Example:</p>
     * <code>
     * <script type="text/javascript">
     * <?php require_once('xoad.php'); ?>
     *
     * var arr = <?= XOAD_Serializer::serialize(array(1,  2,  "string",  array("Nested"))) ?>;
     *
     * alert(arr);
     *
     * </script>
     * </code>
     *
     * @access    public
     *
     * @param    mixed    $var    Variable to serialize.
     *
     * @return    string    {@link http://www.json.org JSON} string that
     *                    represents the variable.
     *
     * @static
     *
     */
    
function serialize(&$var)
    {
        
$type XOAD_Utilities::getType($var);

        if (
$type == 'bool') {

            if (
$var) {

                return 
"true";

            } else {

                return 
"false";
            }

        } else if (
$type == 'int') {

            return 
sprintf('%d',  $var);

        } else if (
$type == 'float') {

            return 
sprintf('%f',  $var);

        } else if (
$type == 'string') {

            if (
strlen($var) >= strlen(XOAD_SERIALIZER_SKIP_STRING)) {

                if (
strcasecmp(substr($var,  0,  strlen(XOAD_SERIALIZER_SKIP_STRING)),  XOAD_SERIALIZER_SKIP_STRING) == 0) {

                    return 
substr($var,  strlen(XOAD_SERIALIZER_SKIP_STRING),  strlen($var) - strlen(XOAD_SERIALIZER_SKIP_STRING));
                }
            }

            
// This code is based on morris_hirsch's
            // comment in utf8_decode function documentation.
            //
            // http://bg.php.net/utf8_decode
            //
            // Thank you.
            //

            
$ascii '';

            
$length strlen($var);

            for (
$iterator 0$iterator $length$iterator ++) {

                
$char $var{$iterator};

                
$charCode ord($char);

                if (
$charCode == 0x08) {

                    
$ascii .= '\b';

                } else if (
$charCode == 0x09) {

                    
$ascii .= '\t';

                } else if (
$charCode == 0x0A) {

                    
$ascii .= '\n';

                } else if (
$charCode == 0x0C) {

                    
$ascii .= '\f';

                } else if (
$charCode == 0x0D) {

                    
$ascii .= '\r';

                } else if ((
$charCode == 0x22) || ($charCode == 0x2F) || ($charCode == 0x5C)) {

                    
$ascii .= '\\' $var{$iterator};

                } else if (
$charCode 128) {

                    
$ascii .= $char;

                } else if (
$charCode >> == 6) {

                    
$byteOne = ($charCode 31);

                    
$iterator ++;

                    
$char $var{$iterator};

                    
$charCode ord($char);

                    
$byteTwo = ($charCode 63);

                    
$charCode = ($byteOne 64) + $byteTwo;

                    
$ascii .= sprintf('\u%04s',  dechex($charCode));

                } else if (
$charCode >> == 14) {

                    
$byteOne = ($charCode 31);

                    
$iterator ++;

                    
$char $var{$iterator};

                    
$charCode ord($char);

                    
$byteTwo = ($charCode 63);

                    
$iterator ++;

                    
$char $var{$iterator};

                    
$charCode ord($char);

                    
$byteThree = ($charCode 63);

                    
$charCode = ((($byteOne 64) + $byteTwo) * 64) + $byteThree;

                    
$ascii .= sprintf('\u%04s',  dechex($charCode));

                } else if (
$charCode >> == 30) {

                    
$byteOne = ($charCode 31);

                    
$iterator ++;

                    
$char $var{$iterator};

                    
$charCode ord($char);

                    
$byteTwo = ($charCode 63);

                    
$iterator ++;

                    
$char $var{$iterator};

                    
$charCode ord($char);

                    
$byteThree = ($charCode 63);

                    
$iterator ++;

                    
$char $var{$iterator};

                    
$charCode ord($char);

                    
$byteFour = ($charCode 63);

                    
$charCode = ((((($byteOne 64) + $byteTwo) * 64) + $byteThree) * 64) + $byteFour;

                    
$ascii .= sprintf('\u%04s',  dechex($charCode));
                }
            }

            return (
'"' $ascii '"');

        } else if (
$type == 's_array') {

            
$index 0;

            
$length sizeof($var);

            
$returnValue '[';

            foreach (
$var as $value) {

                
$returnValue .= XOAD_Serializer::serialize($value);

                if (
$index $length 1) {

                    
$returnValue .= ', ';
                }

                
$index ++;
            }

            
$returnValue .= ']';

            return 
$returnValue;

        } else if (
$type == 'a_array') {

            
$index 0;

            
$length sizeof($var);

            
$returnValue '{';

            foreach (
$var as $key => $value) {

                
$returnValue .= XOAD_Serializer::serialize($key);

                
$returnValue .= ':';

                
$returnValue .= XOAD_Serializer::serialize($value);

                if (
$index $length 1) {

                    
$returnValue .= ', ';
                }

                
$index ++;
            }

            
$returnValue .= '}';

            return 
$returnValue;

        } else if (
$type == 'object') {

            
$objectVars get_object_vars($var);

            return 
XOAD_Serializer::serialize($objectVars);
        }

        return 
"null";
    }

    
/**
     * Adds a {@link XOAD_Serializer} events observer.
     *
     * @access    public
     *
     * @param    mixed    $observer    The observer object to add (must extend {@link XOAD_Observer}).
     *
     * @return    string    true on success,  false otherwise.
     *
     * @static
     *
     */
    
function addObserver(&$observer)
    {
        return 
parent::addObserver($observer,  'XOAD_Serializer');
    }

    
/**
     *
     * @access    public
     *
     * @return    bool
     *
     */
    
function notifyObservers($event 'default',  $arg null)
    {
        return 
parent::notifyObservers($event,  $arg,  'XOAD_Serializer');
    }
}
?>


Other Development Tools Scripts:

WebMaster Resources Home

©RingsWorld.com