import v1.1.0_beta1 | 2009-08-21

This commit is contained in:
2019-07-17 22:16:19 +02:00
parent 2c1152f0d3
commit 8dee6b1a10
2306 changed files with 251360 additions and 23428 deletions

View File

@ -23,12 +23,6 @@
*/
require_once 'Zend/Json.php';
/**
* @see Zend_Json_Exception
*/
require_once 'Zend/Json/Exception.php';
/**
* Decode JSON encoded string to PHP variable constructs
*
@ -102,8 +96,8 @@ class Zend_Json_Decoder
protected function __construct($source, $decodeType)
{
// Set defaults
$this->_source = $source;
$this->_sourceLength = strlen($source);
$this->_source = self::decodeUnicodeString($source);
$this->_sourceLength = strlen($this->_source);
$this->_token = self::EOF;
$this->_offset = 0;
@ -149,8 +143,10 @@ class Zend_Json_Decoder
public static function decode($source = null, $objectDecodeType = Zend_Json::TYPE_ARRAY)
{
if (null === $source) {
require_once 'Zend/Json/Exception.php';
throw new Zend_Json_Exception('Must specify JSON encoded source for decoding');
} elseif (!is_string($source)) {
require_once 'Zend/Json/Exception.php';
throw new Zend_Json_Exception('Can only decode JSON encoded strings');
}
@ -189,7 +185,7 @@ class Zend_Json_Decoder
* Decodes an object of the form:
* { "attribute: value, "attribute2" : value,...}
*
* If Zend_Json_Encoder was used to encode the original object then
* If Zend_Json_Encoder 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.
*
@ -206,6 +202,7 @@ class Zend_Json_Decoder
while ($tok && $tok != self::RBRACE) {
if ($tok != self::DATUM || ! is_string($this->_tokenValue)) {
require_once 'Zend/Json/Exception.php';
throw new Zend_Json_Exception('Missing key in object encoding: ' . $this->_source);
}
@ -213,6 +210,7 @@ class Zend_Json_Decoder
$tok = $this->_getNextToken();
if ($tok != self::COLON) {
require_once 'Zend/Json/Exception.php';
throw new Zend_Json_Exception('Missing ":" in object encoding: ' . $this->_source);
}
@ -225,6 +223,7 @@ class Zend_Json_Decoder
}
if ($tok != self::COMMA) {
require_once 'Zend/Json/Exception.php';
throw new Zend_Json_Exception('Missing "," in object encoding: ' . $this->_source);
}
@ -271,6 +270,7 @@ class Zend_Json_Decoder
}
if ($tok != self::COMMA) {
require_once 'Zend/Json/Exception.php';
throw new Zend_Json_Exception('Missing "," in array encoding: ' . $this->_source);
}
@ -348,6 +348,7 @@ class Zend_Json_Decoder
}
$chr = $str{$i};
if ($chr == '\\') {
$i++;
if ($i >= $str_length) {
@ -383,10 +384,11 @@ class Zend_Json_Decoder
$result .= '\'';
break;
default:
require_once 'Zend/Json/Exception.php';
throw new Zend_Json_Exception("Illegal escape "
. "sequence '" . $chr . "'");
}
} elseif ($chr == '"') {
}
} elseif($chr == '"') {
break;
} else {
$result .= $chr;
@ -434,6 +436,7 @@ class Zend_Json_Decoder
if (is_numeric($datum)) {
if (preg_match('/^0\d+$/', $datum)) {
require_once 'Zend/Json/Exception.php';
throw new Zend_Json_Exception("Octal notation not supported by JSON (value: $datum)");
} else {
$val = intval($datum);
@ -441,6 +444,7 @@ class Zend_Json_Decoder
$this->_tokenValue = ($val == $fVal ? $val : $fVal);
}
} else {
require_once 'Zend/Json/Exception.php';
throw new Zend_Json_Exception("Illegal number format: $datum");
}
@ -448,10 +452,126 @@ class Zend_Json_Decoder
$this->_offset = $start + strlen($datum);
}
} else {
require_once 'Zend/Json/Exception.php';
throw new Zend_Json_Exception('Illegal Token');
}
return($this->_token);
}
/**
* Decode Unicode Characters from \u0000 ASCII syntax.
*
* This algorithm was originally developed for the
* Solar Framework by Paul M. Jones
*
* @link http://solarphp.com/
* @link http://svn.solarphp.com/core/trunk/Solar/Json.php
* @param string $value
* @return string
*/
public static function decodeUnicodeString($chrs)
{
$delim = substr($chrs, 0, 1);
$utf8 = '';
$strlen_chrs = strlen($chrs);
for($i = 0; $i < $strlen_chrs; $i++) {
$substr_chrs_c_2 = substr($chrs, $i, 2);
$ord_chrs_c = ord($chrs[$i]);
switch (true) {
case preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $i, 6)):
// single, escaped unicode character
$utf16 = chr(hexdec(substr($chrs, ($i + 2), 2)))
. chr(hexdec(substr($chrs, ($i + 4), 2)));
$utf8 .= self::_utf162utf8($utf16);
$i += 5;
break;
case ($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F):
$utf8 .= $chrs{$i};
break;
case ($ord_chrs_c & 0xE0) == 0xC0:
// characters U-00000080 - U-000007FF, mask 110XXXXX
//see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$utf8 .= substr($chrs, $i, 2);
++$i;
break;
case ($ord_chrs_c & 0xF0) == 0xE0:
// characters U-00000800 - U-0000FFFF, mask 1110XXXX
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$utf8 .= substr($chrs, $i, 3);
$i += 2;
break;
case ($ord_chrs_c & 0xF8) == 0xF0:
// characters U-00010000 - U-001FFFFF, mask 11110XXX
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$utf8 .= substr($chrs, $i, 4);
$i += 3;
break;
case ($ord_chrs_c & 0xFC) == 0xF8:
// characters U-00200000 - U-03FFFFFF, mask 111110XX
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$utf8 .= substr($chrs, $i, 5);
$i += 4;
break;
case ($ord_chrs_c & 0xFE) == 0xFC:
// characters U-04000000 - U-7FFFFFFF, mask 1111110X
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$utf8 .= substr($chrs, $i, 6);
$i += 5;
break;
}
}
return $utf8;
}
/**
* Convert a string from one UTF-16 char to one UTF-8 char.
*
* Normally should be handled by mb_convert_encoding, but
* provides a slower PHP-only method for installations
* that lack the multibye string extension.
*
* This method is from the Solar Framework by Paul M. Jones
*
* @link http://solarphp.com
* @param string $utf16 UTF-16 character
* @return string UTF-8 character
*/
protected static function _utf162utf8($utf16)
{
// Check for mb extension otherwise do by hand.
if( function_exists('mb_convert_encoding') ) {
return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
}
$bytes = (ord($utf16{0}) << 8) | ord($utf16{1});
switch (true) {
case ((0x7F & $bytes) == $bytes):
// this case should never be reached, because we are in ASCII range
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr(0x7F & $bytes);
case (0x07FF & $bytes) == $bytes:
// return a 2-byte UTF-8 character
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr(0xC0 | (($bytes >> 6) & 0x1F))
. chr(0x80 | ($bytes & 0x3F));
case (0xFFFF & $bytes) == $bytes:
// return a 3-byte UTF-8 character
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr(0xE0 | (($bytes >> 12) & 0x0F))
. chr(0x80 | (($bytes >> 6) & 0x3F))
. chr(0x80 | ($bytes & 0x3F));
}
// ignoring UTF-32 for now, sorry
return '';
}
}