Location: PHPKode > scripts > MultiAjax > MultiAjax.class.php
<?php
/**
 * MultiAjax class library is a PHP solution for smart work with AJAX.
 * It supports timeouts, encodings, queue, session limit and batch mode.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * @author    Vadym Timofeyev <hide@address.com> http://weblancer.net/users/tvv/
 * @copyright 2007 Vadym Timofeyev
 * @license   http://www.gnu.org/licenses/lgpl-3.0.txt
 * @version   1.00
 * @since     PHP 5.0
 * @example   examples/example.php How to use MultiAjax class library.
 */
class MultiAjax {
    /**
     * Internal serialization separator.
     */
    private $serialization_separator = "__\t__";

    /**
     * HTTP request parameter for sirialized data.
     */
    private $request_parameter = 'q';

    /**
     * MultiAjax dispatcher checks AJAX request and dispatchs it.
     * @return string Serialized finished result.
     */
    public function dispatch() {
        if (isset($_REQUEST[$this->request_parameter]) && is_array($batch = $this->_unserialize($_REQUEST[$this->request_parameter]))) {
            $arr = array();
            foreach ($batch as $call) {
                list($data, $handler) = $call;
                $arr[] = $this->$handler($data);
            }
            return $this->_serialize($arr);
        }
        return null;
    }

    /**
     * Serialize data.
     * @param mixed $value Input data.
     * @return string Serialized string.
     */
    private function _serialize($value) {
        // Check array
        if (is_array($value)) {
            $s = '';
            $i = 0;
            foreach ($value as $v) {
                $s .= ':' . $this->_serialize($v);
                $i++;
            }
            return "a:$i$s";
        }

        // Check object
        if (is_object($value)) {
            $s = '';
            $i = 0;
            foreach ($value as $k => $v) {
                $s .= ":$k:" . $this->_serialize($v);
                $i++;
            }
            return "o:$i$s";
        }

        // Check string
        return $value ? 's:' . urlencode(str_replace(':', $this->serialization_separator, $value)) : 'n';
    }

    /**
     * Unserialize data. Divide lexems of input serialized string.
     * @param string $value Serialized string.
     * @return mixed Unserialized data.
     */
    private function _unserialize($value) {
        return $this->_unserialize_internal($arr = split(':', $value), $i = 0);
    }

    /**
     * Private method for unserialization.
     * @param array $arr Lexemes array for serialized data.
     * @param integer $idx Current index of lexemes array.
     * @return Unserialized data.
     */
    private function _unserialize_internal(&$arr, &$idx) {
        switch ($arr[$idx++]) {
            case 's':
                return str_replace($this->serialization_separator, ':', $arr[$idx++]);
            case 'a':
                $a = array();
                for ($i = $arr[$idx++]; $i > 0; $i--) {
                    $a[] = $this->_unserialize_internal($arr, $idx);
                }
                return $a;
            case 'o':
                $a = new StdClass();
                for ($i = $arr[$idx++]; $i > 0; $i--) {
                    $key = $this->_unserialize_internal($arr, $idx);
                    $a->$key = $this->_unserialize_internal($arr, $idx);
                }
                return $a;
            default:
                return null;
        }
    }
}
?>
Return current item: MultiAjax