refactor code

This commit is contained in:
joyqi 2021-08-24 18:30:04 +08:00
parent cf9776ee43
commit 23d52d44a1
3 changed files with 197 additions and 163 deletions

View File

@ -10,7 +10,7 @@
class AutoP
{
// 作为段落的标签
const BLOCK = 'p|pre|div|blockquote|form|ul|ol|dd|table|ins|h1|h2|h3|h4|h5|h6';
private const BLOCK = 'p|pre|div|blockquote|form|ul|ol|dd|table|ins|h1|h2|h3|h4|h5|h6';
/**
* 唯一id
@ -18,7 +18,7 @@ class AutoP
* @access private
* @var integer
*/
private $_uniqueId = 0;
private $uniqueId = 0;
/**
* 存储的段落
@ -26,7 +26,7 @@ class AutoP
* @access private
* @var array
*/
private $_blocks = [];
private $blocks = [];
/**
* 替换段落的回调函数
@ -35,14 +35,17 @@ class AutoP
* @param array $matches 匹配值
* @return string
*/
public function replaceBlockCallback($matches)
public function replaceBlockCallback(array $matches): string
{
$tagMatch = '|' . $matches[1] . '|';
$text = $matches[4];
switch (true) {
/** 用br处理换行 */
case false !== strpos('|li|dd|dt|td|p|a|span|cite|strong|sup|sub|small|del|u|i|b|ins|h1|h2|h3|h4|h5|h6|', $tagMatch):
case false !== strpos(
'|li|dd|dt|td|p|a|span|cite|strong|sup|sub|small|del|u|i|b|ins|h1|h2|h3|h4|h5|h6|',
$tagMatch
):
$text = nl2br(trim($text));
break;
/** 用段落处理换行 */
@ -63,7 +66,7 @@ class AutoP
$key = '<p' . $matches[2] . '/>';
}
$this->_blocks[$key] = "<{$matches[1]}{$matches[3]}>{$text}</{$matches[1]}>";
$this->blocks[$key] = "<{$matches[1]}{$matches[3]}>{$text}</{$matches[1]}>";
return $key;
}
@ -121,8 +124,8 @@ class AutoP
public function parse($text)
{
/** 重置计数器 */
$this->_uniqueId = 0;
$this->_blocks = [];
$this->uniqueId = 0;
$this->blocks = [];
/** 将已有的段落后面的换行处理掉 */
$text = preg_replace(["/<\/p>\s+<p(\s*)/is", "/\s*<br\s*\/?>\s*/is"], ["</p><p\\1", "<br />"], trim($text));
@ -173,7 +176,7 @@ class AutoP
}
$text = $this->cutByBlock($text);
$blocks = array_reverse($this->_blocks);
$blocks = array_reverse($this->blocks);
foreach ($blocks as $blockKey => $blockValue) {
$text = str_replace($blockKey, $blockValue, $text);
@ -190,7 +193,7 @@ class AutoP
*/
private function makeUniqueId()
{
return ':' . str_pad($this->_uniqueId ++, 4, '0', STR_PAD_LEFT);
return ':' . str_pad($this->uniqueId ++, 4, '0', STR_PAD_LEFT);
}
}

View File

@ -18,7 +18,7 @@ class Helper
*/
public static function security()
{
return Typecho_Widget::widget('Widget_Security');
return \Typecho\Widget::widget('Widget_Security');
}
/**

View File

@ -1,49 +1,90 @@
<?php
/**********************************
* Created on: 2007-2-2
* File Name : lib.json.php
* Copyright : 2005 Michal Migursk
* License : http://www.opensource.org/licenses/bsd-license.php
*********************************/
class Json
{
const SERVICES_JSON_SLICE = 1;
const SERVICES_JSON_IN_STR = 2;
const SERVICES_JSON_IN_ARR = 3;
const SERVICES_JSON_IN_OBJ = 4;
const SERVICES_JSON_IN_CMT = 5;
const SERVICES_JSON_LOOSE_TYPE = 16;
const SERVICES_JSON_SUPPRESS_ERRORS = 32;
const E_JSONTYPE = 'json decode error';
public const SERVICES_JSON_SLICE = 1;
public const SERVICES_JSON_IN_STR = 2;
public const SERVICES_JSON_IN_ARR = 3;
public const SERVICES_JSON_IN_OBJ = 4;
public const SERVICES_JSON_IN_CMT = 5;
public const SERVICES_JSON_LOOSE_TYPE = 16;
public const SERVICES_JSON_SUPPRESS_ERRORS = 32;
public const E_JSONTYPE = 'json decode error';
/**
* 对变量进行json编码
*
* @access public
* @param mixed $var 需要处理的对象
* @return string
*/
public static function encode($var)
{
if (function_exists('jsonencodeStr')) {
/** from php 5.1 */
return jsonencodeStr($var);
} else {
return self::encodeStr($var);
}
}
/**
* 对字符串进行json解码
*
* @access public
* @param string|null $var 需要解码的字符串
* @param boolean $assoc 是否强制解释为数组
* @return mixed
*/
public static function decode(?string $var, bool $assoc = false)
{
if (function_exists('json_decode')) {
/** from php 5.1 */
return json_decode($var, $assoc);
} else {
$result = self::decodeStr($var);
}
if ($assoc && is_object($result)) {
return (array)$result;
} elseif (!$assoc && is_array($result)) {
return (object)$result;
}
return $result;
}
/**
* 对配对值编码
*
* @access public
* @param string $name 名称
* @param mixed $value
* @return string
*/
public static function _name_value($name, $value)
private static function nameValue(string $name, $value): string
{
$encoded_value = self::_encode($value);
$encoded_value = self::encodeStr($value);
if (self::_is_error($encoded_value)) {
if (self::isError($encoded_value)) {
return $encoded_value;
}
return self::_encode(strval($name)) . ':' . $encoded_value;
return self::encodeStr(strval($name)) . ':' . $encoded_value;
}
/**
* 将对象转换为json串
*
* @access public
* @param mixed $var 需要转换的对象
* @return string
*/
public static function _encode($var)
private static function encodeStr($var)
{
switch (gettype($var)) {
case 'boolean':
@ -63,8 +104,7 @@ class Json
$ascii = '';
$strlen_var = strlen($var);
for ($c = 0; $c < $strlen_var; ++ $c) {
for ($c = 0; $c < $strlen_var; ++$c) {
$ord_var_c = ord($var[$c]);
switch (true) {
@ -102,42 +142,54 @@ class Json
break;
case (($ord_var_c & 0xF0) == 0xE0):
$char = pack('C*', $ord_var_c,
$char = pack(
'C*',
$ord_var_c,
ord($var[$c + 1]),
ord($var[$c + 2]));
ord($var[$c + 2])
);
$c += 2;
$utf16 = self::utf82utf16($char);
$ascii .= sprintf('\u%04s', bin2hex($utf16));
break;
case (($ord_var_c & 0xF8) == 0xF0):
$char = pack('C*', $ord_var_c,
$char = pack(
'C*',
$ord_var_c,
ord($var[$c + 1]),
ord($var[$c + 2]),
ord($var[$c + 3]));
ord($var[$c + 3])
);
$c += 3;
$utf16 = self::utf82utf16($char);
$ascii .= sprintf('\u%04s', bin2hex($utf16));
break;
case (($ord_var_c & 0xFC) == 0xF8):
$char = pack('C*', $ord_var_c,
$char = pack(
'C*',
$ord_var_c,
ord($var[$c + 1]),
ord($var[$c + 2]),
ord($var[$c + 3]),
ord($var[$c + 4]));
ord($var[$c + 4])
);
$c += 4;
$utf16 = self::utf82utf16($char);
$ascii .= sprintf('\u%04s', bin2hex($utf16));
break;
case (($ord_var_c & 0xFE) == 0xFC):
$char = pack('C*', $ord_var_c,
$char = pack(
'C*',
$ord_var_c,
ord($var[$c + 1]),
ord($var[$c + 2]),
ord($var[$c + 3]),
ord($var[$c + 4]),
ord($var[$c + 5]));
ord($var[$c + 5])
);
$c += 5;
$utf16 = self::utf82utf16($char);
$ascii .= sprintf('\u%04s', bin2hex($utf16));
@ -149,12 +201,14 @@ class Json
case 'array':
if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) {
$properties = array_map(['Json', '_name_value'],
$properties = array_map(
[self::class, 'nameValue'],
array_keys($var),
array_values($var));
array_values($var)
);
foreach ($properties as $property) {
if (self::_is_error($property)) {
if (self::isError($property)) {
return $property;
}
}
@ -163,10 +217,10 @@ class Json
}
// treat it like a regular array
$elements = array_map(['Json', '_encode'], $var);
$elements = array_map([self::class, 'encodeStr'], $var);
foreach ($elements as $element) {
if (self::_is_error($element)) {
if (self::isError($element)) {
return $element;
}
}
@ -176,12 +230,14 @@ class Json
case 'object':
$vars = get_object_vars($var);
$properties = array_map(['Json', '_name_value'],
$properties = array_map(
[self::class, 'nameValue'],
array_keys($vars),
array_values($vars));
array_values($vars)
);
foreach ($properties as $property) {
if (self::_is_error($property)) {
if (self::isError($property)) {
return $property;
}
}
@ -197,10 +253,10 @@ class Json
* 将utf8转换为utf16
*
* @access private
* @param string $utf8 utf8字符串
* @return string
* @param string|null $utf8 utf8字符串
* @return string|null
*/
private static function utf82utf16($utf8)
private static function utf82utf16(?string $utf8): ?string
{
if (function_exists('mb_convert_encoding')) {
return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
@ -228,72 +284,30 @@ class Json
*
* @access private
* @param mixed $data 错误对象
* @param string $code 错误代码
* @param string|null $code 错误代码
* @return boolean
*/
private static function _is_error($data, $code = null)
private static function isError($data, ?string $code = null): bool
{
if (is_object($data) && (get_class($data) == 'services_json_error' ||
is_subclass_of($data, 'services_json_error'))) {
if (
is_object($data) && (get_class($data) == 'services_json_error' ||
is_subclass_of($data, 'services_json_error'))
) {
return true;
}
return false;
}
/**
* 对变量进行json编码
*
* @access public
* @param mixed $var 需要处理的对象
* @return string
*/
public static function encode($var)
{
if (function_exists('json_encode')) {
/** from php 5.1 */
return json_encode($var);
} else {
return self::_encode($var);
}
}
/**
* 对字符串进行json解码
*
* @access public
* @param string $var 需要解码的字符串
* @param boolean $assoc 是否强制解释为数组
* @return mixed
*/
public static function decode($var, $assoc = false)
{
if (function_exists('json_decode')) {
/** from php 5.1 */
return json_decode($var, $assoc);
} else {
$result = self::_decode($var);
}
if ($assoc && is_object($result)) {
return (array)$result;
} elseif (!$assoc && is_array($result)) {
return (object)$result;
}
return $result;
}
/**
* 解码json字符串
*
* @access public
* @param string $str 解码字符串
* @return mixed
* @param string|null $str 解码字符串
* @return array|bool|float|int|stdClass|string|void|null
*/
public static function _decode($str)
private static function decodeStr(?string $str)
{
$str = self::_reduce_string($str);
$str = self::reduceString($str);
switch (strtolower($str)) {
case 'true':
@ -309,50 +323,50 @@ class Json
$m = [];
if (is_numeric($str)) {
return ((float)$str == (integer)$str)
? (integer)$str
return ((float)$str == (int)$str)
? (int)$str
: (float)$str;
} elseif (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) {
$delim = substr($str, 0, 1);
$chrs = substr($str, 1, - 1);
$chrs = substr($str, 1, -1);
$utf8 = '';
$strlen_chrs = strlen($chrs);
for ($c = 0; $c < $strlen_chrs; ++ $c) {
for ($c = 0; $c < $strlen_chrs; ++$c) {
$substr_chrs_c_2 = substr($chrs, $c, 2);
$ord_chrs_c = ord($chrs[$c]);
switch (true) {
case $substr_chrs_c_2 == '\b':
$utf8 .= chr(0x08);
++ $c;
++$c;
break;
case $substr_chrs_c_2 == '\t':
$utf8 .= chr(0x09);
++ $c;
++$c;
break;
case $substr_chrs_c_2 == '\n':
$utf8 .= chr(0x0A);
++ $c;
++$c;
break;
case $substr_chrs_c_2 == '\f':
$utf8 .= chr(0x0C);
++ $c;
++$c;
break;
case $substr_chrs_c_2 == '\r':
$utf8 .= chr(0x0D);
++ $c;
++$c;
break;
case $substr_chrs_c_2 == '\\"':
case $substr_chrs_c_2 == '\\\'':
case $substr_chrs_c_2 == '\\\\':
case $substr_chrs_c_2 == '\\/':
if (($delim == '"' && $substr_chrs_c_2 != '\\\'') ||
($delim == "'" && $substr_chrs_c_2 != '\\"')) {
$utf8 .= $chrs[++ $c];
if (
($delim == '"' && $substr_chrs_c_2 != '\\\'') ||
($delim == "'" && $substr_chrs_c_2 != '\\"')
) {
$utf8 .= $chrs[++$c];
}
break;
@ -369,7 +383,7 @@ class Json
case ($ord_chrs_c & 0xE0) == 0xC0:
$utf8 .= substr($chrs, $c, 2);
++ $c;
++$c;
break;
case ($ord_chrs_c & 0xF0) == 0xE0:
@ -391,13 +405,10 @@ class Json
$utf8 .= substr($chrs, $c, 6);
$c += 5;
break;
}
}
return $utf8;
} elseif (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) {
if ($str [0] == '[') {
$stk = [self::SERVICES_JSON_IN_ARR];
@ -407,90 +418,113 @@ class Json
$obj = new stdClass();
}
array_push($stk, ['what' => self::SERVICES_JSON_SLICE,
array_push($stk, [
'what' => self::SERVICES_JSON_SLICE,
'where' => 0,
'delim' => false]);
'delim' => false
]);
$chrs = substr($str, 1, - 1);
$chrs = self::_reduce_string($chrs);
$chrs = substr($str, 1, -1);
$chrs = self::reduceString($chrs);
if ($chrs == '') {
if (reset($stk) == self::SERVICES_JSON_IN_ARR) {
return $arr;
} else {
return $obj;
}
}
$strlen_chrs = strlen($chrs);
for ($c = 0; $c <= $strlen_chrs; ++ $c) {
for ($c = 0; $c <= $strlen_chrs; ++$c) {
$top = end($stk);
$substr_chrs_c_2 = substr($chrs, $c, 2);
if (($c == $strlen_chrs) || (($chrs [$c] == ',') && ($top['what'] == self::SERVICES_JSON_SLICE))) {
if (
($c == $strlen_chrs) || (($chrs [$c] == ',')
&& ($top['what'] == self::SERVICES_JSON_SLICE))
) {
$slice = substr($chrs, $top['where'], ($c - $top['where']));
array_push($stk, ['what' => self::SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false]);
array_push(
$stk,
[
'what' => self::SERVICES_JSON_SLICE, 'where' => ($c + 1),
'delim' => false
]
);
if (reset($stk) == self::SERVICES_JSON_IN_ARR) {
array_push($arr, self::_decode($slice));
array_push($arr, self::decodeStr($slice));
} elseif (reset($stk) == self::SERVICES_JSON_IN_OBJ) {
$parts = [];
if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) {
$key = self::_decode($parts[1]);
$val = self::_decode($parts[2]);
$key = self::decodeStr($parts[1]);
$val = self::decodeStr($parts[2]);
$obj->$key = $val;
} elseif (preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) {
$key = $parts[1];
$val = self::_decode($parts[2]);
$val = self::decodeStr($parts[2]);
$obj->$key = $val;
}
}
} elseif ((($chrs [$c] == '"') || ($chrs[$c] == "'")) && ($top['what'] != self::SERVICES_JSON_IN_STR)) {
array_push($stk, ['what' => self::SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs[$c]]);
} elseif (($chrs [$c] == $top['delim']) &&
} elseif (
(($chrs [$c] == '"') || ($chrs[$c] == "'"))
&& ($top['what'] != self::SERVICES_JSON_IN_STR)
) {
array_push(
$stk,
['what' => self::SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs[$c]]
);
} elseif (
($chrs [$c] == $top['delim']) &&
($top['what'] == self::SERVICES_JSON_IN_STR) &&
((strlen(substr($chrs, 0, $c)) - strlen(rtrim(substr($chrs, 0, $c), '\\'))) % 2 != 1)) {
((strlen(substr($chrs, 0, $c)) - strlen(rtrim(substr($chrs, 0, $c), '\\'))) % 2 != 1)
) {
array_pop($stk);
} elseif (($chrs [$c] == '[') &&
in_array($top['what'], [self::SERVICES_JSON_SLICE, self::SERVICES_JSON_IN_ARR, self::SERVICES_JSON_IN_OBJ])) {
} elseif (
($chrs [$c] == '[') &&
in_array(
$top['what'],
[self::SERVICES_JSON_SLICE, self::SERVICES_JSON_IN_ARR, self::SERVICES_JSON_IN_OBJ]
)
) {
array_push($stk, ['what' => self::SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false]);
} elseif (($chrs [$c] == ']') && ($top['what'] == self::SERVICES_JSON_IN_ARR)) {
array_pop($stk);
} elseif (($chrs [$c] == '{') &&
in_array($top['what'], [self::SERVICES_JSON_SLICE, self::SERVICES_JSON_IN_ARR, self::SERVICES_JSON_IN_OBJ])) {
} elseif (
($chrs [$c] == '{') &&
in_array(
$top['what'],
[self::SERVICES_JSON_SLICE, self::SERVICES_JSON_IN_ARR, self::SERVICES_JSON_IN_OBJ]
)
) {
array_push($stk, ['what' => self::SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false]);
} elseif (($chrs [$c] == '}') && ($top['what'] == self::SERVICES_JSON_IN_OBJ)) {
array_pop($stk);
} elseif (($substr_chrs_c_2 == '/*') &&
in_array($top['what'], [self::SERVICES_JSON_SLICE, self::SERVICES_JSON_IN_ARR, self::SERVICES_JSON_IN_OBJ])) {
} elseif (
($substr_chrs_c_2 == '/*') &&
in_array(
$top['what'],
[self::SERVICES_JSON_SLICE, self::SERVICES_JSON_IN_ARR, self::SERVICES_JSON_IN_OBJ]
)
) {
array_push($stk, ['what' => self::SERVICES_JSON_IN_CMT, 'where' => $c, 'delim' => false]);
$c ++;
$c++;
} elseif (($substr_chrs_c_2 == '*/') && ($top['what'] == self::SERVICES_JSON_IN_CMT)) {
array_pop($stk);
$c ++;
for ($i = $top['where']; $i <= $c; ++ $i)
$c++;
for ($i = $top['where']; $i <= $c; ++$i) {
$chrs = substr_replace($chrs, ' ', $i, 1);
}
}
}
if (reset($stk) == self::SERVICES_JSON_IN_ARR) {
return $arr;
} elseif (reset($stk) == self::SERVICES_JSON_IN_OBJ) {
return $obj;
}
}
}
}
@ -498,11 +532,10 @@ class Json
/**
* 清除特殊格式
*
* @access private
* @param string $str 待处理字符串
* @return string
* @param string|null $str 待处理字符串
* @return string|null
*/
private static function _reduce_string($str)
private static function reduceString(?string $str): ?string
{
$str = preg_replace([
'#^\s*//(.+)$#m',
@ -515,11 +548,10 @@ class Json
/**
* 将utf16转换为utf8
*
* @access private
* @param string $utf16 utf16字符串
* @return string
* @param string|null $utf16 utf16字符串
* @return string|null
*/
private static function utf162utf8($utf16)
private static function utf162utf8(?string $utf16): ?string
{
if (function_exists('mb_convert_encoding')) {
return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
@ -542,4 +574,3 @@ class Json
return '';
}
}