Skip to content

Commit 9825292

Browse files
committed
update format
1 parent 9c28c13 commit 9825292

File tree

7 files changed

+350
-202
lines changed

7 files changed

+350
-202
lines changed

.php_cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ return PhpCsFixer\Config::create()
1515
'array_syntax' => [
1616
'syntax' => 'short'
1717
],
18+
'list_syntax' => [
19+
'syntax' => 'short'
20+
],
1821
'class_attributes_separation' => true,
1922
'declare_strict_types' => true,
20-
'global_namespace_import' => true,
23+
'global_namespace_import' => [
24+
'import_constants' => true,
25+
'import_functions' => true,
26+
],
2127
'header_comment' => [
2228
'comment_type' => 'PHPDoc',
2329
'header' => $header,

src/Str/HtmlHelper.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99

1010
namespace Toolkit\Stdlib\Str;
1111

12+
use function is_string;
13+
use function htmlspecialchars;
14+
use function htmlentities;
15+
use function strpos;
16+
use function is_array;
17+
use function htmlspecialchars_decode;
18+
use function html_entity_decode;
19+
use function preg_match_all;
20+
use function array_key_exists;
21+
1222
/**
1323
* Class HtmlHelper
1424
* @package Toolkit\Stdlib\Str
@@ -24,7 +34,7 @@ class HtmlHelper
2434
*/
2535
public static function encode($text, $charset = 'utf-8'): string
2636
{
27-
return \htmlspecialchars($text, ENT_QUOTES, $charset);
37+
return htmlspecialchars($text, ENT_QUOTES, $charset);
2838
}
2939

3040
/**
@@ -35,7 +45,7 @@ public static function encode($text, $charset = 'utf-8'): string
3545
*/
3646
public static function decode($text): string
3747
{
38-
return \htmlspecialchars_decode($text, ENT_QUOTES);
48+
return htmlspecialchars_decode($text, ENT_QUOTES);
3949
}
4050

4151
/**
@@ -50,13 +60,13 @@ public static function encodeArray($data, $charset = 'utf-8'): array
5060
$d = [];
5161

5262
foreach ($data as $key => $value) {
53-
if (\is_string($key)) {
54-
$key = \htmlspecialchars($key, ENT_QUOTES, $charset);
63+
if (is_string($key)) {
64+
$key = htmlspecialchars($key, ENT_QUOTES, $charset);
5565
}
5666

57-
if (\is_string($value)) {
58-
$value = \htmlspecialchars($value, ENT_QUOTES, $charset);
59-
} elseif (\is_array($value)) {
67+
if (is_string($value)) {
68+
$value = htmlspecialchars($value, ENT_QUOTES, $charset);
69+
} elseif (is_array($value)) {
6070
$value = static::encodeArray($value);
6171
}
6272

@@ -81,7 +91,7 @@ public static function encodeArray($data, $charset = 'utf-8'): array
8191
*/
8292
public static function escape($data, int $type = 0, $encoding = 'UTF-8')
8393
{
84-
if (\is_array($data)) {
94+
if (is_array($data)) {
8595
foreach ($data as $k => $v) {
8696
$data[$k] = self::escape($data, $type, $encoding);
8797
}
@@ -91,13 +101,13 @@ public static function escape($data, int $type = 0, $encoding = 'UTF-8')
91101

92102
// 默认使用 htmlspecialchars()
93103
if (!$type) {
94-
$data = \htmlspecialchars($data, \ENT_QUOTES, $encoding);
104+
$data = htmlspecialchars($data, \ENT_QUOTES, $encoding);
95105
} else {
96-
$data = \htmlentities($data, \ENT_QUOTES, $encoding);
106+
$data = htmlentities($data, \ENT_QUOTES, $encoding);
97107
}
98108

99109
//如‘志’这样的16进制的html字符,为了防止这样的字符被错误转译,使用正则进行匹配,把这样的字符又转换回来。
100-
if (\strpos($data, '&#')) {
110+
if (strpos($data, '&#')) {
101111
$data = \preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $data);
102112
}
103113

@@ -113,14 +123,14 @@ public static function escape($data, int $type = 0, $encoding = 'UTF-8')
113123
*/
114124
public static function unescap($data, $type = 0, $encoding = 'UTF-8')
115125
{
116-
if (\is_array($data)) {
126+
if (is_array($data)) {
117127
foreach ($data as $k => $v) {
118128
$data[$k] = self::unescap($data, $type, $encoding);
119129
}
120130
} elseif (!$type) {//默认使用 htmlspecialchars_decode()
121-
$data = \htmlspecialchars_decode($data, \ENT_QUOTES);
131+
$data = htmlspecialchars_decode($data, \ENT_QUOTES);
122132
} else {
123-
$data = \html_entity_decode($data, \ENT_QUOTES, $encoding);
133+
$data = html_entity_decode($data, \ENT_QUOTES, $encoding);
124134
}
125135

126136
return $data;
@@ -176,12 +186,12 @@ public static function matchImages(string $html, bool $onlySrc = true): array
176186
// $preg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*>/i';
177187
$preg = '/<img.+src=\"(:?.+.+\.(?:jpg|gif|bmp|bnp|png)\"?).+>/i';
178188

179-
if (!\preg_match_all($preg, trim($html), $images)) {
189+
if (!preg_match_all($preg, trim($html), $images)) {
180190
return [];
181191
}
182192

183193
if ($onlySrc) {
184-
return \array_key_exists(1, $images) ? $images[1] : [];
194+
return array_key_exists(1, $images) ? $images[1] : [];
185195
}
186196

187197
return $images;

src/Str/JsonHelper.php

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
use RuntimeException;
1313
use InvalidArgumentException;
1414
use stdClass;
15+
use function json_encode;
16+
use function json_decode;
17+
use function is_string;
18+
use function file_get_contents;
19+
use function dirname;
20+
use function file_exists;
21+
use function basename;
22+
use function preg_replace;
23+
use function file_put_contents;
24+
use const JSON_PRETTY_PRINT;
25+
use const JSON_UNESCAPED_SLASHES;
26+
use const JSON_UNESCAPED_UNICODE;
1527

1628
/**
1729
* Class JsonHelper
@@ -26,9 +38,9 @@ class JsonHelper
2638
*/
2739
public static function prettyJSON(
2840
$data,
29-
int $flags = \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES
41+
int $flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
3042
) {
31-
return \json_encode($data, $flags);
43+
return json_encode($data, $flags);
3244
}
3345

3446
/**
@@ -38,7 +50,7 @@ public static function prettyJSON(
3850
*/
3951
public static function encode($data): string
4052
{
41-
return \json_encode($data, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE);
53+
return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
4254
}
4355

4456
/**
@@ -68,7 +80,7 @@ public static function parseFile(string $file, $toArray = true)
6880
throw new InvalidArgumentException("File not found or does not exist resources: {$file}");
6981
}
7082

71-
$string = \file_get_contents($file);
83+
$string = file_get_contents($file);
7284

7385
return self::parseString($string, $toArray);
7486
}
@@ -84,7 +96,7 @@ public static function parseString(string $string, bool $toArray = true)
8496
return $toArray ? [] : new stdClass();
8597
}
8698

87-
$string = (string)\preg_replace([
99+
$string = (string)preg_replace([
88100
// 去掉所有多行注释/* .... */
89101
'/\/\*.*?\*\/\s*/is',
90102
// 去掉所有单行注释//....
@@ -94,7 +106,7 @@ public static function parseString(string $string, bool $toArray = true)
94106
], ['', '', ' '], trim($string));
95107

96108
// json_last_error() === JSON_ERROR_NONE
97-
return \json_decode($string, $toArray);
109+
return json_decode($string, $toArray);
98110
}
99111

100112
/**
@@ -109,21 +121,21 @@ public static function parseString(string $string, bool $toArray = true)
109121
*/
110122
public static function format($input, $output = false, array $options = [])
111123
{
112-
if (!\is_string($input)) {
124+
if (!is_string($input)) {
113125
return false;
114126
}
115127

116128
$data = \trim($input);
117129

118-
if (\file_exists($input)) {
119-
$data = \file_get_contents($input);
130+
if (file_exists($input)) {
131+
$data = file_get_contents($input);
120132
}
121133

122134
if (!$data) {
123135
return false;
124136
}
125137

126-
$data = \preg_replace([
138+
$data = preg_replace([
127139
// 去掉所有多行注释/* .... */
128140
'/\/\*.*?\*\/\s*/is',
129141
// 去掉所有单行注释//....
@@ -139,9 +151,9 @@ public static function format($input, $output = false, array $options = [])
139151
$default = ['type' => 'min'];
140152
$options = \array_merge($default, $options);
141153

142-
if (\file_exists($input) && (empty($options['file']) || !\is_file($options['file']))) {
143-
$dir = \dirname($input);
144-
$name = \basename($input, '.json');
154+
if (file_exists($input) && (empty($options['file']) || !\is_file($options['file']))) {
155+
$dir = dirname($input);
156+
$name = basename($input, '.json');
145157
$file = $dir . '/' . $name . '.' . $options['type'] . '.json';
146158
// save to options
147159
$options['file'] = $file;
@@ -161,20 +173,20 @@ public static function saveAs(string $data, string $output, array $options = [])
161173
{
162174
$default = ['type' => 'min', 'file' => ''];
163175
$options = array_merge($default, $options);
164-
$saveDir = \dirname($output);
176+
$saveDir = dirname($output);
165177

166-
if (!\file_exists($saveDir)) {
178+
if (!file_exists($saveDir)) {
167179
throw new RuntimeException('设置的json文件输出' . $saveDir . '目录不存在!');
168180
}
169181

170-
$name = \basename($output, '.json');
182+
$name = basename($output, '.json');
171183
$file = $saveDir . '/' . $name . '.' . $options['type'] . '.json';
172184

173185
// 去掉空白
174186
if ($options['type '] === 'min') {
175-
$data = \preg_replace('/(?!\w)\s*?(?!\w)/i', '', $data);
187+
$data = preg_replace('/(?!\w)\s*?(?!\w)/i', '', $data);
176188
}
177189

178-
return \file_put_contents($file, $data);
190+
return file_put_contents($file, $data);
179191
}
180192
}

0 commit comments

Comments
 (0)