first commit
This commit is contained in:
457
customerportal/include/Zend/Json/Decoder.php
Normal file
457
customerportal/include/Zend/Json/Decoder.php
Normal file
@@ -0,0 +1,457 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
|
||||
* Zend Framework
|
||||
|
||||
*
|
||||
|
||||
* LICENSE
|
||||
|
||||
*
|
||||
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
|
||||
* with this package in the file LICENSE.txt.
|
||||
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
|
||||
*
|
||||
|
||||
* @category Zend
|
||||
|
||||
* @package Zend_Json
|
||||
|
||||
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* Zend_Json
|
||||
|
||||
*/
|
||||
|
||||
require_once 'include/Zend/Json.php';
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* Zend_Json_Exception
|
||||
|
||||
*/
|
||||
|
||||
require_once 'include/Zend/Json/Exception.php';
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* Decode JSON encoded string to PHP variable constructs
|
||||
|
||||
*
|
||||
|
||||
* @category Zend
|
||||
|
||||
* @package Zend_Json
|
||||
|
||||
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
|
||||
*/
|
||||
|
||||
class Zend_Json_Decoder
|
||||
|
||||
{
|
||||
|
||||
/**
|
||||
|
||||
* Parse tokens used to decode the JSON object. These are not
|
||||
|
||||
* for public consumption, they are just used internally to the
|
||||
|
||||
* class.
|
||||
|
||||
*/
|
||||
|
||||
const EOF = 0;
|
||||
|
||||
const DATUM = 1;
|
||||
|
||||
const LBRACE = 2;
|
||||
|
||||
const LBRACKET = 3;
|
||||
|
||||
const RBRACE = 4;
|
||||
|
||||
const RBRACKET = 5;
|
||||
|
||||
const COMMA = 6;
|
||||
|
||||
const COLON = 7;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* Use to maintain a "pointer" to the source being decoded
|
||||
|
||||
*
|
||||
|
||||
* @var string
|
||||
|
||||
*/
|
||||
|
||||
protected $_source;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* Caches the source length
|
||||
|
||||
*
|
||||
|
||||
* @var int
|
||||
|
||||
*/
|
||||
|
||||
protected $_sourceLength;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* The offset within the souce being decoded
|
||||
|
||||
*
|
||||
|
||||
* @var int
|
||||
|
||||
*
|
||||
|
||||
*/
|
||||
|
||||
protected $_offset;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* The current token being considered in the parser cycle
|
||||
|
||||
*
|
||||
|
||||
* @var int
|
||||
|
||||
*/
|
||||
|
||||
protected $_token;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* Flag indicating how objects should be decoded
|
||||
|
||||
*
|
||||
|
||||
* @var int
|
||||
|
||||
* @access protected
|
||||
|
||||
*/
|
||||
|
||||
protected $_decodeType;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* Constructor
|
||||
|
||||
*
|
||||
|
||||
* @param string $source String source to decode
|
||||
|
||||
* @param int $decodeType How objects should be decoded -- see
|
||||
|
||||
* {@link Zend_Json::TYPE_ARRAY} and {@link Zend_Json::TYPE_OBJECT} for
|
||||
|
||||
* valid values
|
||||
|
||||
* @return void
|
||||
|
||||
*/
|
||||
|
||||
protected function __construct($source, $decodeType)
|
||||
|
||||
{
|
||||
|
||||
// Set defaults
|
||||
|
||||
$this->_source = $source;
|
||||
|
||||
$this->_sourceLength = strlen($source);
|
||||
|
||||
$this->_token = self::EOF;
|
||||
|
||||
$this->_offset = 0;
|
||||
|
||||
|
||||
|
||||
// Normalize and set $decodeType
|
||||
|
||||
if (!in_array($decodeType, array(Zend_Json::TYPE_ARRAY, Zend_Json::TYPE_OBJECT)))
|
||||
|
||||
{
|
||||
|
||||
$decodeType = Zend_Json::TYPE_ARRAY;
|
||||
|
||||
}
|
||||
|
||||
$this->_decodeType = $decodeType;
|
||||
|
||||
|
||||
|
||||
// Set pointer at first token
|
||||
|
||||
$this->_getNextToken();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* Decode a JSON source string
|
||||
|
||||
*
|
||||
|
||||
* Decodes a JSON encoded string. The value returned will be one of the
|
||||
|
||||
* following:
|
||||
|
||||
* - integer
|
||||
|
||||
* - float
|
||||
|
||||
* - boolean
|
||||
|
||||
* - null
|
||||
|
||||
* - StdClass
|
||||
|
||||
* - array
|
||||
|
||||
* - array of one or more of the above types
|
||||
|
||||
*
|
||||
|
||||
* By default, decoded objects will be returned as associative arrays; to
|
||||
|
||||
* return a StdClass object instead, pass {@link Zend_Json::TYPE_OBJECT} to
|
||||
|
||||
* the $objectDecodeType parameter.
|
||||
|
||||
*
|
||||
|
||||
* Throws a Zend_Json_Exception if the source string is null.
|
||||
|
||||
*
|
||||
|
||||
* @static
|
||||
|
||||
* @access public
|
||||
|
||||
* @param string $source String to be decoded
|
||||
|
||||
* @param int $objectDecodeType How objects should be decoded; should be
|
||||
|
||||
* either or {@link Zend_Json::TYPE_ARRAY} or
|
||||
|
||||
* {@link Zend_Json::TYPE_OBJECT}; defaults to TYPE_ARRAY
|
||||
|
||||
* @return mixed
|
||||
|
||||
* @throws Zend_Json_Exception
|
||||
|
||||
*/
|
||||
|
||||
public static function decode($source = null, $objectDecodeType = Zend_Json::TYPE_ARRAY)
|
||||
|
||||
{
|
||||
|
||||
if (null === $source) {
|
||||
|
||||
throw new Zend_Json_Exception('Must specify JSON encoded source for decoding');
|
||||
|
||||
} elseif (!is_string($source)) {
|
||||
|
||||
throw new Zend_Json_Exception('Can only decode JSON encoded strings');
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
$decoder = new self($source, $objectDecodeType);
|
||||
|
||||
|
||||
|
||||
return $decoder->_decodeValue();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* Recursive driving rountine for supported toplevel tops
|
||||
|
||||
*
|
||||
|
||||
* @return mixed
|
||||
|
||||
*/
|
||||
|
||||
protected function _decodeValue()
|
||||
|
||||
{
|
||||
|
||||
switch ($this->_token) {
|
||||
|
||||
case self::DATUM:
|
||||
|
||||
$result = $this->_tokenValue;
|
||||
|
||||
$this->_getNextToken();
|
||||
|
||||
return($result);
|
||||
|
||||
break;
|
||||
|
||||
case self::LBRACE:
|
||||
|
||||
return($this->_decodeObject());
|
||||
|
||||
break;
|
||||
|
||||
case self::LBRACKET:
|
||||
|
||||
return($this->_decodeArray());
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
return null;
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* Decodes an object of the form:
|
||||
|
||||
* { "attribute: value, "attribute2" : value,...}
|
||||
|
||||
*
|
||||
|
||||
* If ZJsonEnoder or ZJAjax was used to encode the original object
|
||||
|
||||
* then a special attribute called __className which specifies a class
|
||||
|
||||
* name that should wrap the data contained within the encoded source.
|
||||
|
||||
*
|
||||
|
||||
* Decodes to either an array or StdClass object, based on the value of
|
||||
|
||||
* {@link $_decodeType}. If invalid $_decodeType present, returns as an
|
||||
|
||||
* array.
|
||||
|
||||
*
|
||||
|
||||
* @return array|StdClass
|
||||
|
||||
*/
|
||||
|
||||
protected function _decodeObject()
|
||||
|
||||
{
|
||||
|
||||
$members = array();
|
||||
|
||||
$tok = $this->_getNextToken();
|
||||
|
||||
|
||||
|
||||
while ($tok && $tok != self::RBRACE) {
|
||||
|
||||
if ($tok != self::DATUM || ! is_string($this->_tokenValue)) {
|
||||
|
||||
throw new Zend_Json_Exception('Missing key in object encoding: ' . $this->_source);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
$key = $this->_tokenValue;
|
||||
|
||||
$tok = $this->_getNextToken();
|
||||
|
||||
|
||||
|
||||
if ($tok != self::COLON) {
|
||||
|
||||
throw new Zend_Json_Exception('Missing ":" in object encoding: ' . $this->_source);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
$tok = $this->_getNextToken();
|
||||
|
||||
$members[$key] = $this->_decodeValue();
|
||||
|
||||
$tok = $this->_token;
|
||||
|
||||
|
||||
|
||||
if ($tok == self::RBRACE) {
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
if ($tok != self::COMMA) {
|
||||
|
||||
throw new Zend_Json_Exception('Missing "," in object encoding: ' . $this->_source);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user