libjson.php
author Dan
Sun, 04 Jan 2009 12:19:27 -0500
changeset 42 c24a2311f232
parent 8 0acb8d9a3194
permissions -rw-r--r--
Removed a stray debug message
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     1
<?php
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     2
/**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     3
 * Zend Framework
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     4
 *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     5
 * LICENSE
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     6
 *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     7
 * This source file is subject to the new BSD license that is bundled
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     8
 * with this package in the file LICENSE.txt.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     9
 * It is also available through the world-wide-web at this URL:
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    10
 * http://framework.zend.com/license/new-bsd
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    11
 * If you did not receive a copy of the license and are unable to
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    12
 * obtain it through the world-wide-web, please send an email
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    13
 * to license@zend.com so we can send you a copy immediately.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    14
 *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    15
 * @category   Zend
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    16
 * @package    Zend_Json
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    17
 * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    18
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    19
 */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    20
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    21
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    22
/**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    23
 * Encode PHP constructs to JSON
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    24
 *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    25
 * @category   Zend
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    26
 * @package    Zend_Json
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    27
 * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    28
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    29
 */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    30
class Zend_Json_Encoder
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    31
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    32
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    33
     * Whether or not to check for possible cycling
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    34
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    35
     * @var boolean
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    36
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    37
    protected $_cycleCheck;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    38
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    39
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    40
     * Array of visited objects; used to prevent cycling.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    41
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    42
     * @var array
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    43
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    44
    protected $_visited = array();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    45
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    46
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    47
     * Constructor
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    48
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    49
     * @param boolean $cycleCheck Whether or not to check for recursion when encoding
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    50
     * @return void
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    51
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    52
    protected function __construct($cycleCheck = false)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    53
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    54
        $this->_cycleCheck = $cycleCheck;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    55
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    56
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    57
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    58
     * Use the JSON encoding scheme for the value specified
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    59
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    60
     * @param mixed $value The value to be encoded
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    61
     * @param boolean $cycleCheck Whether or not to check for possible object recursion when encoding
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    62
     * @return string  The encoded value
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    63
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    64
    public static function encode($value, $cycleCheck = false)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    65
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    66
        $encoder = new Zend_Json_Encoder(($cycleCheck) ? true : false);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    67
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    68
        return $encoder->_encodeValue($value);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    69
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    70
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    71
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    72
     * Recursive driver which determines the type of value to be encoded
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    73
     * and then dispatches to the appropriate method. $values are either
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    74
     *    - objects (returns from {@link _encodeObject()})
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    75
     *    - arrays (returns from {@link _encodeArray()})
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    76
     *    - basic datums (e.g. numbers or strings) (returns from {@link _encodeDatum()})
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    77
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    78
     * @param $value mixed The value to be encoded
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    79
     * @return string Encoded value
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    80
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    81
    protected function _encodeValue(&$value)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    82
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    83
        if (is_object($value)) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    84
            return $this->_encodeObject($value);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    85
        } else if (is_array($value)) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    86
            return $this->_encodeArray($value);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    87
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    88
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    89
        return $this->_encodeDatum($value);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    90
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    91
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    92
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    93
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    94
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    95
     * Encode an object to JSON by encoding each of the public properties
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    96
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    97
     * A special property is added to the JSON object called '__className'
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    98
     * that contains the name of the class of $value. This is used to decode
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    99
     * the object on the client into a specific class.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   100
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   101
     * @param $value object
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   102
     * @return string
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   103
     * @throws Zend_Json_Exception If recursive checks are enabled and the object has been serialized previously
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   104
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   105
    protected function _encodeObject(&$value)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   106
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   107
        if ($this->_cycleCheck) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   108
            if ($this->_wasVisited($value)) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   109
                throw new Zend_Json_Exception(
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   110
                    'Cycles not supported in JSON encoding, cycle introduced by '
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   111
                    . 'class "' . get_class($value) . '"'
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   112
                );
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   113
            }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   114
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   115
            $this->_visited[] = $value;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   116
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   117
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   118
        $props = '';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   119
        foreach (get_object_vars($value) as $name => $propValue) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   120
            if (isset($propValue)) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   121
                $props .= ','
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   122
                        . $this->_encodeValue($name)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   123
                        . ':'
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   124
                        . $this->_encodeValue($propValue);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   125
            }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   126
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   127
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   128
        return '{"__className":"' . get_class($value) . '"'
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   129
                . $props . '}';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   130
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   131
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   132
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   133
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   134
     * Determine if an object has been serialized already
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   135
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   136
     * @param mixed $value
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   137
     * @return boolean
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   138
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   139
    protected function _wasVisited(&$value)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   140
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   141
        if (in_array($value, $this->_visited, true)) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   142
            return true;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   143
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   144
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   145
        return false;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   146
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   147
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   148
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   149
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   150
     * JSON encode an array value
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   151
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   152
     * Recursively encodes each value of an array and returns a JSON encoded
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   153
     * array string.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   154
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   155
     * Arrays are defined as integer-indexed arrays starting at index 0, where
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   156
     * the last index is (count($array) -1); any deviation from that is
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   157
     * considered an associative array, and will be encoded as such.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   158
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   159
     * @param $array array
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   160
     * @return string
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   161
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   162
    protected function _encodeArray(&$array)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   163
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   164
        $tmpArray = array();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   165
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   166
        // Check for associative array
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   167
        if (!empty($array) && (array_keys($array) !== range(0, count($array) - 1))) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   168
            // Associative array
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   169
            $result = '{';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   170
            foreach ($array as $key => $value) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   171
                $key = (string) $key;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   172
                $tmpArray[] = $this->_encodeString($key)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   173
                            . ':'
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   174
                            . $this->_encodeValue($value);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   175
            }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   176
            $result .= implode(',', $tmpArray);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   177
            $result .= '}';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   178
        } else {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   179
            // Indexed array
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   180
            $result = '[';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   181
            $length = count($array);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   182
            for ($i = 0; $i < $length; $i++) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   183
                $tmpArray[] = $this->_encodeValue($array[$i]);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   184
            }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   185
            $result .= implode(',', $tmpArray);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   186
            $result .= ']';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   187
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   188
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   189
        return $result;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   190
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   191
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   192
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   193
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   194
     * JSON encode a basic data type (string, number, boolean, null)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   195
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   196
     * If value type is not a string, number, boolean, or null, the string
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   197
     * 'null' is returned.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   198
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   199
     * @param $value mixed
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   200
     * @return string
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   201
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   202
    protected function _encodeDatum(&$value)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   203
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   204
        $result = 'null';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   205
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   206
        if (is_int($value) || is_float($value)) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   207
            $result = (string)$value;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   208
        } elseif (is_string($value)) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   209
            $result = $this->_encodeString($value);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   210
        } elseif (is_bool($value)) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   211
            $result = $value ? 'true' : 'false';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   212
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   213
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   214
        return $result;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   215
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   216
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   217
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   218
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   219
     * JSON encode a string value by escaping characters as necessary
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   220
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   221
     * @param $value string
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   222
     * @return string
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   223
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   224
    protected function _encodeString(&$string)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   225
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   226
        // Escape these characters with a backslash:
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   227
        // " \ / \n \r \t \b \f
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   228
        $search  = array('\\', "\n", "\t", "\r", "\b", "\f", '"');
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   229
        $replace = array('\\\\', '\\n', '\\t', '\\r', '\\b', '\\f', '\"');
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   230
        $string  = str_replace($search, $replace, $string);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   231
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   232
        // Escape certain ASCII characters:
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   233
        // 0x08 => \b
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   234
        // 0x0c => \f
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   235
        $string = str_replace(array(chr(0x08), chr(0x0C)), array('\b', '\f'), $string);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   236
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   237
        return '"' . $string . '"';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   238
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   239
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   240
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   241
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   242
     * Encode the constants associated with the ReflectionClass
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   243
     * parameter. The encoding format is based on the class2 format
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   244
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   245
     * @param $cls ReflectionClass
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   246
     * @return string Encoded constant block in class2 format
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   247
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   248
    private static function _encodeConstants(ReflectionClass $cls)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   249
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   250
        $result    = "constants : {";
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   251
        $constants = $cls->getConstants();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   252
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   253
        $tmpArray = array();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   254
        if (!empty($constants)) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   255
            foreach ($constants as $key => $value) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   256
                $tmpArray[] = "$key: " . self::encode($value);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   257
            }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   258
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   259
            $result .= implode(', ', $tmpArray);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   260
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   261
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   262
        return $result . "}";
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   263
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   264
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   265
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   266
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   267
     * Encode the public methods of the ReflectionClass in the
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   268
     * class2 format
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   269
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   270
     * @param $cls ReflectionClass
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   271
     * @return string Encoded method fragment
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   272
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   273
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   274
    private static function _encodeMethods(ReflectionClass $cls)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   275
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   276
        $methods = $cls->getMethods();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   277
        $result = 'methods:{';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   278
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   279
        $started = false;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   280
        foreach ($methods as $method) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   281
            if (! $method->isPublic() || !$method->isUserDefined()) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   282
                continue;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   283
            }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   284
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   285
            if ($started) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   286
                $result .= ',';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   287
            }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   288
            $started = true;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   289
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   290
            $result .= '' . $method->getName(). ':function(';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   291
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   292
            if ('__construct' != $method->getName()) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   293
                $parameters  = $method->getParameters();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   294
                $paramCount  = count($parameters);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   295
                $argsStarted = false;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   296
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   297
                $argNames = "var argNames=[";
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   298
                foreach ($parameters as $param) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   299
                    if ($argsStarted) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   300
                        $result .= ',';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   301
                    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   302
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   303
                    $result .= $param->getName();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   304
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   305
                    if ($argsStarted) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   306
                        $argNames .= ',';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   307
                    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   308
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   309
                    $argNames .= '"' . $param->getName() . '"';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   310
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   311
                    $argsStarted = true;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   312
                }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   313
                $argNames .= "];";
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   314
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   315
                $result .= "){"
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   316
                         . $argNames
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   317
                         . 'var result = ZAjaxEngine.invokeRemoteMethod('
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   318
                         . "this, '" . $method->getName()
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   319
                         . "',argNames,arguments);"
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   320
                         . 'return(result);}';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   321
            } else {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   322
                $result .= "){}";
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   323
            }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   324
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   325
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   326
        return $result . "}";
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   327
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   328
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   329
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   330
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   331
     * Encode the public properties of the ReflectionClass in the class2
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   332
     * format.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   333
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   334
     * @param $cls ReflectionClass
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   335
     * @return string Encode properties list
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   336
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   337
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   338
    private static function _encodeVariables(ReflectionClass $cls)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   339
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   340
        $properties = $cls->getProperties();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   341
        $propValues = get_class_vars($cls->getName());
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   342
        $result = "variables:{";
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   343
        $cnt = 0;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   344
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   345
        $tmpArray = array();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   346
        foreach ($properties as $prop) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   347
            if (! $prop->isPublic()) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   348
                continue;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   349
            }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   350
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   351
            $tmpArray[] = $prop->getName()
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   352
                        . ':'
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   353
                        . self::encode($propValues[$prop->getName()]);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   354
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   355
        $result .= implode(',', $tmpArray);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   356
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   357
        return $result . "}";
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   358
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   359
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   360
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   361
     * Encodes the given $className into the class2 model of encoding PHP
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   362
     * classes into JavaScript class2 classes.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   363
     * NOTE: Currently only public methods and variables are proxied onto
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   364
     * the client machine
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   365
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   366
     * @param $className string The name of the class, the class must be
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   367
     * instantiable using a null constructor
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   368
     * @param $package string Optional package name appended to JavaScript
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   369
     * proxy class name
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   370
     * @return string The class2 (JavaScript) encoding of the class
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   371
     * @throws Zend_Json_Exception
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   372
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   373
    public static function encodeClass($className, $package = '')
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   374
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   375
        $cls = new ReflectionClass($className);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   376
        if (! $cls->isInstantiable()) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   377
            throw new Zend_Json_Exception("$className must be instantiable");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   378
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   379
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   380
        return "Class.create('$package$className',{"
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   381
                . self::_encodeConstants($cls)    .","
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   382
                . self::_encodeMethods($cls)      .","
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   383
                . self::_encodeVariables($cls)    .'});';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   384
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   385
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   386
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   387
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   388
     * Encode several classes at once
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   389
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   390
     * Returns JSON encoded classes, using {@link encodeClass()}.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   391
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   392
     * @param array $classNames
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   393
     * @param string $package
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   394
     * @return string
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   395
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   396
    public static function encodeClasses(array $classNames, $package = '')
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   397
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   398
        $result = '';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   399
        foreach ($classNames as $className) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   400
            $result .= self::encodeClass($className, $package);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   401
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   402
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   403
        return $result;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   404
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   405
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   406
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   407
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   408
/**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   409
 * Decode JSON encoded string to PHP variable constructs
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   410
 *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   411
 * @category   Zend
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   412
 * @package    Zend_Json
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   413
 * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   414
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   415
 */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   416
class Zend_Json_Decoder
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   417
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   418
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   419
     * Parse tokens used to decode the JSON object. These are not
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   420
     * for public consumption, they are just used internally to the
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   421
     * class.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   422
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   423
    const EOF          = 0;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   424
    const DATUM        = 1;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   425
    const LBRACE       = 2;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   426
    const LBRACKET     = 3;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   427
    const RBRACE       = 4;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   428
    const RBRACKET     = 5;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   429
    const COMMA        = 6;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   430
    const COLON        = 7;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   431
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   432
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   433
     * Use to maintain a "pointer" to the source being decoded
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   434
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   435
     * @var string
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   436
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   437
    protected $_source;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   438
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   439
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   440
     * Caches the source length
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   441
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   442
     * @var int
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   443
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   444
    protected $_sourceLength;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   445
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   446
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   447
     * The offset within the souce being decoded
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   448
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   449
     * @var int
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   450
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   451
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   452
    protected $_offset;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   453
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   454
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   455
     * The current token being considered in the parser cycle
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   456
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   457
     * @var int
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   458
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   459
    protected $_token;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   460
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   461
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   462
     * Flag indicating how objects should be decoded
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   463
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   464
     * @var int
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   465
     * @access protected
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   466
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   467
    protected $_decodeType;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   468
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   469
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   470
     * Constructor
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   471
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   472
     * @param string $source String source to decode
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   473
     * @param int $decodeType How objects should be decoded -- see
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   474
     * {@link Zend_Json::TYPE_ARRAY} and {@link Zend_Json::TYPE_OBJECT} for
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   475
     * valid values
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   476
     * @return void
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   477
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   478
    protected function __construct($source, $decodeType)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   479
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   480
        
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   481
        // eliminate comments
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   482
        $source = preg_replace(array(
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   483
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   484
                  // eliminate single line comments in '// ...' form
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   485
                  '#^\s*//(.+)$#m',
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   486
    
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   487
                  // eliminate multi-line comments in '/* ... */' form, at start of string
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   488
                  '#^\s*/\*(.+)\*/#Us',
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   489
    
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   490
                  // eliminate multi-line comments in '/* ... */' form, at end of string
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   491
                  '#/\*(.+)\*/\s*$#Us'
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   492
    
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   493
              ), '', $source);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   494
        
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   495
        // Set defaults
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   496
        $this->_source       = $source;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   497
        $this->_sourceLength = strlen($source);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   498
        $this->_token        = self::EOF;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   499
        $this->_offset       = 0;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   500
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   501
        // Normalize and set $decodeType
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   502
        if (!in_array($decodeType, array(Zend_Json::TYPE_ARRAY, Zend_Json::TYPE_OBJECT)))
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   503
        {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   504
            $decodeType = Zend_Json::TYPE_ARRAY;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   505
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   506
        $this->_decodeType   = $decodeType;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   507
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   508
        // Set pointer at first token
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   509
        $this->_getNextToken();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   510
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   511
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   512
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   513
     * Decode a JSON source string
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   514
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   515
     * Decodes a JSON encoded string. The value returned will be one of the
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   516
     * following:
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   517
     *        - integer
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   518
     *        - float
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   519
     *        - boolean
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   520
     *        - null
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   521
     *      - StdClass
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   522
     *      - array
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   523
     *         - array of one or more of the above types
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   524
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   525
     * By default, decoded objects will be returned as associative arrays; to
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   526
     * return a StdClass object instead, pass {@link Zend_Json::TYPE_OBJECT} to
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   527
     * the $objectDecodeType parameter.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   528
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   529
     * Throws a Zend_Json_Exception if the source string is null.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   530
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   531
     * @static
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   532
     * @access public
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   533
     * @param string $source String to be decoded
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   534
     * @param int $objectDecodeType How objects should be decoded; should be
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   535
     * either or {@link Zend_Json::TYPE_ARRAY} or
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   536
     * {@link Zend_Json::TYPE_OBJECT}; defaults to TYPE_ARRAY
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   537
     * @return mixed
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   538
     * @throws Zend_Json_Exception
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   539
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   540
    public static function decode($source = null, $objectDecodeType = Zend_Json::TYPE_ARRAY)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   541
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   542
        if (null === $source) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   543
            throw new Zend_Json_Exception('Must specify JSON encoded source for decoding');
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   544
        } elseif (!is_string($source)) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   545
            throw new Zend_Json_Exception('Can only decode JSON encoded strings');
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   546
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   547
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   548
        $decoder = new self($source, $objectDecodeType);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   549
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   550
        return $decoder->_decodeValue();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   551
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   552
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   553
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   554
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   555
     * Recursive driving rountine for supported toplevel tops
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   556
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   557
     * @return mixed
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   558
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   559
    protected function _decodeValue()
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   560
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   561
        switch ($this->_token) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   562
            case self::DATUM:
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   563
                $result  = $this->_tokenValue;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   564
                $this->_getNextToken();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   565
                return($result);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   566
                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   567
            case self::LBRACE:
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   568
                return($this->_decodeObject());
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   569
                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   570
            case self::LBRACKET:
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   571
                return($this->_decodeArray());
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   572
                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   573
            default:
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   574
                return null;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   575
                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   576
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   577
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   578
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   579
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   580
     * Decodes an object of the form:
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   581
     *  { "attribute: value, "attribute2" : value,...}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   582
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   583
     * If ZJsonEnoder or ZJAjax was used to encode the original object
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   584
     * then a special attribute called __className which specifies a class
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   585
     * name that should wrap the data contained within the encoded source.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   586
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   587
     * Decodes to either an array or StdClass object, based on the value of
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   588
     * {@link $_decodeType}. If invalid $_decodeType present, returns as an
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   589
     * array.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   590
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   591
     * @return array|StdClass
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   592
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   593
    protected function _decodeObject()
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   594
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   595
        $members = array();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   596
        $tok = $this->_getNextToken();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   597
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   598
        while ($tok && $tok != self::RBRACE) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   599
            if ($tok != self::DATUM || ! is_string($this->_tokenValue)) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   600
                throw new Zend_Json_Exception('Missing key in object encoding: ' . $this->_source);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   601
            }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   602
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   603
            $key = $this->_tokenValue;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   604
            $tok = $this->_getNextToken();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   605
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   606
            if ($tok != self::COLON) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   607
                throw new Zend_Json_Exception('Missing ":" in object encoding: ' . $this->_source);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   608
            }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   609
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   610
            $tok = $this->_getNextToken();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   611
            $members[$key] = $this->_decodeValue();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   612
            $tok = $this->_token;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   613
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   614
            if ($tok == self::RBRACE) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   615
                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   616
            }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   617
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   618
            if ($tok != self::COMMA) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   619
                throw new Zend_Json_Exception('Missing "," in object encoding: ' . $this->_source);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   620
            }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   621
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   622
            $tok = $this->_getNextToken();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   623
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   624
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   625
        switch ($this->_decodeType) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   626
            case Zend_Json::TYPE_OBJECT:
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   627
                // Create new StdClass and populate with $members
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   628
                $result = new StdClass();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   629
                foreach ($members as $key => $value) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   630
                    $result->$key = $value;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   631
                }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   632
                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   633
            case Zend_Json::TYPE_ARRAY:
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   634
            default:
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   635
                $result = $members;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   636
                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   637
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   638
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   639
        $this->_getNextToken();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   640
        return $result;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   641
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   642
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   643
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   644
     * Decodes a JSON array format:
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   645
     *    [element, element2,...,elementN]
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   646
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   647
     * @return array
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   648
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   649
    protected function _decodeArray()
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   650
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   651
        $result = array();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   652
        $starttok = $tok = $this->_getNextToken(); // Move past the '['
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   653
        $index  = 0;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   654
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   655
        while ($tok && $tok != self::RBRACKET) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   656
            $result[$index++] = $this->_decodeValue();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   657
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   658
            $tok = $this->_token;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   659
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   660
            if ($tok == self::RBRACKET || !$tok) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   661
                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   662
            }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   663
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   664
            if ($tok != self::COMMA) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   665
                throw new Zend_Json_Exception('Missing "," in array encoding: ' . $this->_source);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   666
            }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   667
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   668
            $tok = $this->_getNextToken();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   669
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   670
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   671
        $this->_getNextToken();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   672
        return($result);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   673
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   674
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   675
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   676
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   677
     * Removes whitepsace characters from the source input
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   678
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   679
    protected function _eatWhitespace()
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   680
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   681
        if (preg_match(
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   682
                '/([\t\b\f\n\r ])*/s',
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   683
                $this->_source,
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   684
                $matches,
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   685
                PREG_OFFSET_CAPTURE,
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   686
                $this->_offset)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   687
            && $matches[0][1] == $this->_offset)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   688
        {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   689
            $this->_offset += strlen($matches[0][0]);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   690
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   691
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   692
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   693
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   694
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   695
     * Retrieves the next token from the source stream
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   696
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   697
     * @return int Token constant value specified in class definition
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   698
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   699
    protected function _getNextToken()
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   700
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   701
        $this->_token      = self::EOF;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   702
        $this->_tokenValue = null;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   703
        $this->_eatWhitespace();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   704
        
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   705
        if ($this->_offset >= $this->_sourceLength) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   706
            return(self::EOF);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   707
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   708
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   709
        $str        = $this->_source;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   710
        $str_length = $this->_sourceLength;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   711
        $i          = $this->_offset;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   712
        $start      = $i;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   713
        
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   714
        switch ($str{$i}) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   715
            case '{':
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   716
               $this->_token = self::LBRACE;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   717
               break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   718
            case '}':
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   719
                $this->_token = self::RBRACE;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   720
                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   721
            case '[':
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   722
                $this->_token = self::LBRACKET;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   723
                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   724
            case ']':
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   725
                $this->_token = self::RBRACKET;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   726
                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   727
            case ',':
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   728
                $this->_token = self::COMMA;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   729
                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   730
            case ':':
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   731
                $this->_token = self::COLON;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   732
                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   733
            case  '"':
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   734
                $result = '';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   735
                do {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   736
                    $i++;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   737
                    if ($i >= $str_length) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   738
                        break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   739
                    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   740
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   741
                    $chr = $str{$i};
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   742
                    if ($chr == '\\') {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   743
                        $i++;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   744
                        if ($i >= $str_length) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   745
                            break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   746
                        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   747
                        $chr = $str{$i};
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   748
                        switch ($chr) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   749
                            case '"' :
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   750
                                $result .= '"';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   751
                                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   752
                            case '\\':
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   753
                                $result .= '\\';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   754
                                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   755
                            case '/' :
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   756
                                $result .= '/';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   757
                                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   758
                            case 'b' :
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   759
                                $result .= chr(8);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   760
                                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   761
                            case 'f' :
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   762
                                $result .= chr(12);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   763
                                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   764
                            case 'n' :
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   765
                                $result .= chr(10);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   766
                                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   767
                            case 'r' :
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   768
                                $result .= chr(13);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   769
                                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   770
                            case 't' :
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   771
                                $result .= chr(9);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   772
                                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   773
                            case '\'' :
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   774
                                $result .= '\'';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   775
                                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   776
                            default:
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   777
                                throw new Zend_Json_Exception("Illegal escape "
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   778
                                    .  "sequence '" . $chr . "'");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   779
                            }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   780
                    } elseif ($chr == '"') {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   781
                        break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   782
                    } else {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   783
                        $result .= $chr;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   784
                    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   785
                } while ($i < $str_length);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   786
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   787
                $this->_token = self::DATUM;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   788
                //$this->_tokenValue = substr($str, $start + 1, $i - $start - 1);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   789
                $this->_tokenValue = $result;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   790
                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   791
            case  "'":
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   792
                $result = '';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   793
                do {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   794
                    $i++;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   795
                    if ($i >= $str_length) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   796
                        break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   797
                    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   798
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   799
                    $chr = $str{$i};
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   800
                    if ($chr == '\\') {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   801
                        $i++;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   802
                        if ($i >= $str_length) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   803
                            break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   804
                        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   805
                        $chr = $str{$i};
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   806
                        switch ($chr) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   807
                            case "'" :
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   808
                                $result .= "'";
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   809
                                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   810
                            case '\\':
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   811
                                $result .= '\\';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   812
                                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   813
                            case '/' :
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   814
                                $result .= '/';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   815
                                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   816
                            case 'b' :
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   817
                                $result .= chr(8);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   818
                                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   819
                            case 'f' :
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   820
                                $result .= chr(12);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   821
                                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   822
                            case 'n' :
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   823
                                $result .= chr(10);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   824
                                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   825
                            case 'r' :
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   826
                                $result .= chr(13);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   827
                                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   828
                            case 't' :
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   829
                                $result .= chr(9);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   830
                                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   831
                            case '"' :
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   832
                                $result .= '"';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   833
                                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   834
                            default:
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   835
                                throw new Zend_Json_Exception("Illegal escape "
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   836
                                    .  "sequence '" . $chr . "'");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   837
                            }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   838
                    } elseif ($chr == "'") {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   839
                        break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   840
                    } else {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   841
                        $result .= $chr;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   842
                    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   843
                } while ($i < $str_length);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   844
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   845
                $this->_token = self::DATUM;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   846
                //$this->_tokenValue = substr($str, $start + 1, $i - $start - 1);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   847
                $this->_tokenValue = $result;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   848
                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   849
            case 't':
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   850
                if (($i+ 3) < $str_length && substr($str, $start, 4) == "true") {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   851
                    $this->_token = self::DATUM;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   852
                }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   853
                $this->_tokenValue = true;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   854
                $i += 3;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   855
                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   856
            case 'f':
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   857
                if (($i+ 4) < $str_length && substr($str, $start, 5) == "false") {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   858
                    $this->_token = self::DATUM;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   859
                }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   860
                $this->_tokenValue = false;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   861
                $i += 4;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   862
                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   863
            case 'n':
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   864
                if (($i+ 3) < $str_length && substr($str, $start, 4) == "null") {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   865
                    $this->_token = self::DATUM;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   866
                }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   867
                $this->_tokenValue = NULL;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   868
                $i += 3;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   869
                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   870
              case ' ':
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   871
                break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   872
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   873
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   874
        if ($this->_token != self::EOF) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   875
            $this->_offset = $i + 1; // Consume the last token character
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   876
            return($this->_token);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   877
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   878
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   879
        $chr = $str{$i};
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   880
        if ($chr == '-' || $chr == '.' || ($chr >= '0' && $chr <= '9')) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   881
            if (preg_match('/-?([0-9])*(\.[0-9]*)?((e|E)((-|\+)?)[0-9]+)?/s',
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   882
                $str, $matches, PREG_OFFSET_CAPTURE, $start) && $matches[0][1] == $start) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   883
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   884
                $datum = $matches[0][0];
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   885
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   886
                if (is_numeric($datum)) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   887
                    if (preg_match('/^0\d+$/', $datum)) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   888
                        throw new Zend_Json_Exception("Octal notation not supported by JSON (value: $datum)");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   889
                    } else {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   890
                        $val  = intval($datum);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   891
                        $fVal = floatval($datum);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   892
                        $this->_tokenValue = ($val == $fVal ? $val : $fVal);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   893
                    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   894
                } else {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   895
                    throw new Zend_Json_Exception("Illegal number format: $datum");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   896
                }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   897
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   898
                $this->_token = self::DATUM;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   899
                $this->_offset = $start + strlen($datum);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   900
            }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   901
        } else {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   902
            throw new Zend_Json_Exception("Illegal Token at pos $i: $chr\nContext:\n--------------------------------------------------" . substr($str, $i) . "\n--------------------------------------------------");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   903
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   904
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   905
        return($this->_token);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   906
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   907
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   908
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   909
/**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   910
 * Zend Framework
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   911
 *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   912
 * LICENSE
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   913
 *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   914
 * This source file is subject to the new BSD license that is bundled
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   915
 * with this package in the file LICENSE.txt.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   916
 * It is also available through the world-wide-web at this URL:
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   917
 * http://framework.zend.com/license/new-bsd
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   918
 * If you did not receive a copy of the license and are unable to
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   919
 * obtain it through the world-wide-web, please send an email
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   920
 * to license@zend.com so we can send you a copy immediately.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   921
 *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   922
 * @category   Zend
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   923
 * @package    Zend_Json
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   924
 * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   925
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   926
 */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   927
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   928
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   929
/**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   930
 * @category   Zend
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   931
 * @package    Zend_Json
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   932
 * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   933
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   934
 */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   935
class Zend_Json_Exception extends Zend_Exception
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   936
{}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   937
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   938
/**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   939
 * @category   Zend
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   940
 * @package    Zend
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   941
 * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   942
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   943
 */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   944
class Zend_Exception extends Exception
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   945
{}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   946
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   947
/**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   948
 * Class for encoding to and decoding from JSON.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   949
 *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   950
 * @category   Zend
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   951
 * @package    Zend_Json
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   952
 * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   953
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   954
 */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   955
class Zend_Json
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   956
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   957
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   958
     * How objects should be encoded -- arrays or as StdClass. TYPE_ARRAY is 1
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   959
     * so that it is a boolean true value, allowing it to be used with
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   960
     * ext/json's functions.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   961
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   962
    const TYPE_ARRAY  = 1;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   963
    const TYPE_OBJECT = 0;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   964
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   965
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   966
     * @var bool
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   967
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   968
    public static $useBuiltinEncoderDecoder = true;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   969
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   970
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   971
     * Decodes the given $encodedValue string which is
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   972
     * encoded in the JSON format
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   973
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   974
     * Uses ext/json's json_decode if available.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   975
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   976
     * @param string $encodedValue Encoded in JSON format
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   977
     * @param int $objectDecodeType Optional; flag indicating how to decode
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   978
     * objects. See {@link ZJsonDecoder::decode()} for details.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   979
     * @return mixed
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   980
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   981
    public static function decode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   982
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   983
        if (function_exists('json_decode') && self::$useBuiltinEncoderDecoder !== true) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   984
            return json_decode($encodedValue, $objectDecodeType);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   985
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   986
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   987
        return Zend_Json_Decoder::decode($encodedValue, $objectDecodeType);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   988
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   989
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   990
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   991
    /**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   992
     * Encode the mixed $valueToEncode into the JSON format
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   993
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   994
     * Encodes using ext/json's json_encode() if available.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   995
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   996
     * NOTE: Object should not contain cycles; the JSON format
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   997
     * does not allow object reference.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   998
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   999
     * NOTE: Only public variables will be encoded
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1000
     *
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1001
     * @param mixed $valueToEncode
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1002
     * @param boolean $cycleCheck Optional; whether or not to check for object recursion; off by default
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1003
     * @return string JSON encoded object
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1004
     */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1005
    public static function encode($valueToEncode, $cycleCheck = false)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1006
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1007
        if (function_exists('json_encode') && self::$useBuiltinEncoderDecoder !== true) {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1008
            return json_encode($valueToEncode);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1009
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1010
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1011
        return Zend_Json_Encoder::encode($valueToEncode, $cycleCheck);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1012
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1013
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1014
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1015
/**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1016
 * Wrapper for JSON encoding.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1017
 * @param mixed Variable to encode
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1018
 * @return string JSON-encoded string
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1019
 */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1020
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1021
function eb_json_encode($data)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1022
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1023
  return Zend_Json::encode($data, true);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1024
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1025
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1026
/**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1027
 * Wrapper for JSON decoding.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1028
 * @param string JSON-encoded string
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1029
 * @return mixed Decoded value
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1030
 */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1031
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1032
function eb_json_decode($data)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1033
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1034
  return Zend_Json::decode($data, Zend_Json::TYPE_ARRAY);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1035
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1036
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
  1037
?>