This commit is contained in:
jorgecc
2024-04-06 12:37:23 -03:00
parent b1a90af079
commit 7d957752be
3 changed files with 22 additions and 16 deletions
+2
View File
@@ -116,6 +116,8 @@ var_dump(Services_JSON::encode($obj)); // encode an object
## Changelog
* 2.4
* update requirements to php 7.4
* 2.3.2
* Added flag to decode() DECODE_NO_QUOTE
* 2.3.1
+1 -1
View File
@@ -25,7 +25,7 @@
}
],
"require": {
"php": ">=7.2.5"
"php": ">=7.4"
},
"suggest": {
"ext-mbstring": "*"
+19 -15
View File
@@ -1,4 +1,5 @@
<?php
<?php /** @noinspection UnknownInspectionInspection */
/**
* @noinspection TypeUnsafeArraySearchInspection
* @noinspection PhpComposerExtensionStubsInspection
@@ -93,11 +94,11 @@ class Services_JSON
public const DECODE_FIX_ROOT = 128;
public const DECODE_NO_QUOTE = 256;
protected static $use = 0;
protected static int $use = 0;
// private - cache the mbstring lookup results..
protected static $_mb_strlen = false;
protected static $_mb_substr = false;
protected static $_mb_convert_encoding = false;
protected static bool $_mb_strlen = false;
protected static bool $_mb_substr = false;
protected static bool $_mb_convert_encoding = false;
/**
* constructs a new JSON instance. We force this library as static
@@ -128,7 +129,7 @@ class Services_JSON
* @return string UTF-8 character
* @access private
*/
protected static function utf162utf8($utf16): string
protected static function utf162utf8(string $utf16): string
{
if (self::$_mb_convert_encoding) {
return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
@@ -162,7 +163,7 @@ class Services_JSON
* @return string UTF-16 character
* @access private
*/
public static function utf82utf16($utf8): string
public static function utf82utf16(string $utf8): string
{
if (self::$_mb_convert_encoding) {
return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
@@ -204,7 +205,7 @@ class Services_JSON
}
/**
* encodes an arbitrary variable into JSON format without JSON Header - warning - may allow XSS!!!!)
* encodes an arbitrary variable into JSON format without JSON Header - warning - may allow XSS!!!!
*
* @param mixed $var any number, boolean, string, array, or object to be encoded.
* see argument 1 to Services_JSON() above for array-parsing behavior.
@@ -425,13 +426,13 @@ class Services_JSON
* @return string JSON-formatted name-value pair, like '"name":value'
* @access private
*/
protected static function name_value($name, $value)
protected static function name_value(string $name, $value)
{
$encoded_value = self::_encode($value);
if (self::isError($encoded_value)) {
return $encoded_value;
}
return self::_encode((string)$name) . ':' . $encoded_value;
return self::_encode($name) . ':' . $encoded_value;
}
/**
@@ -442,7 +443,7 @@ class Services_JSON
* @return string string value stripped of comments and whitespace
* @access private
*/
protected static function reduce_string($str): string
protected static function reduce_string(string $str): string
{
$str = preg_replace([// eliminate single line comments in '// ...' form
'#^\s*//(.+)$#m', // eliminate multi-line comments in '/* ... */' form, at start of string
@@ -480,10 +481,13 @@ class Services_JSON
* in ASCII or UTF-8 format!<br>
* @access public
*/
public static function decode($str, int $use = 0)
public static function decode(string $str, int $use = 0)
{
if ($use & self::DECODE_FIX_ROOT) {
$str = trim($str);
if($str==="") {
throw new RuntimeException("no value to decode");
}
$firstChar = $str[0];
if ($firstChar !== '{' && $firstChar !== '[') {
// fixing a malformed json-u, if the json-u doesn't start with { [ and ends with ] } then it wraps it
@@ -734,7 +738,7 @@ class Services_JSON
* @param string $str
* @return integer length
*/
protected static function strlen8($str): int
protected static function strlen8(string $str): int
{
if (self::$_mb_strlen) {
return mb_strlen($str, "8bit");
@@ -746,10 +750,10 @@ class Services_JSON
* Returns part of a string, interpreting $start and $length as number of bytes.
* @param string $string
* @param integer $start start
* @param integer $length length
* @param integer|bool $length length
* @return string length
*/
protected static function substr8($string, $start, $length = false): string
protected static function substr8(string $string, int $start, $length = false): string
{
if ($length === false) {
$length = self::strlen8($string) - $start;