Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

Commit bf3852a

Browse files
committed
format codes, update some
1 parent 229e02b commit bf3852a

File tree

8 files changed

+418
-327
lines changed

8 files changed

+418
-327
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"keywords": [
66
"library",
77
"tool",
8+
"string",
89
"php"
910
],
1011
"homepage": "https://github.com/php-toolkit/str-utils",
@@ -17,7 +18,8 @@
1718
}
1819
],
1920
"require": {
20-
"php": ">7.1.0"
21+
"php": ">7.1.0",
22+
"ext-mbstring": "*"
2123
},
2224
"autoload": {
2325
"psr-4": {

src/JsonHelper.php

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ class JsonHelper
2121
*/
2222
public static function encode($data): string
2323
{
24-
if (PHP_VERSION_ID >= 50400) {
25-
return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
26-
}
27-
28-
return json_encode($data);
24+
return \json_encode($data, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE);
2925
}
3026

3127
/**
@@ -34,7 +30,7 @@ public static function encode($data): string
3430
* @return array|mixed|null|\stdClass|string
3531
* @throws \InvalidArgumentException
3632
*/
37-
public static function parse(string $data, $toArray = true)
33+
public static function parse(string $data, bool $toArray = true)
3834
{
3935
if (is_file($data)) {
4036
return self::parseFile($data, $toArray);
@@ -44,18 +40,18 @@ public static function parse(string $data, $toArray = true)
4440
}
4541

4642
/**
47-
* @param $file
43+
* @param string $file
4844
* @param bool|true $toArray
4945
* @return mixed|null|string
5046
* @throws \InvalidArgumentException
5147
*/
52-
public static function parseFile($file, $toArray = true)
48+
public static function parseFile(string $file, $toArray = true)
5349
{
54-
if (!is_file($file)) {
50+
if (!\is_file($file)) {
5551
throw new \InvalidArgumentException("File not found or does not exist resources: {$file}");
5652
}
5753

58-
$string = file_get_contents($file);
54+
$string = \file_get_contents($file);
5955

6056
return self::parseString($string, $toArray);
6157
}
@@ -71,7 +67,7 @@ public static function parseString(string $string, bool $toArray = true)
7167
return $toArray ? [] : new \stdClass();
7268
}
7369

74-
$string = (string)preg_replace([
70+
$string = (string)\preg_replace([
7571
// 去掉所有多行注释/* .... */
7672
'/\/\*.*?\*\/\s*/is',
7773
// 去掉所有单行注释//....
@@ -81,7 +77,7 @@ public static function parseString(string $string, bool $toArray = true)
8177
], ['', '', ' '], trim($string));
8278

8379
// json_last_error() === JSON_ERROR_NONE
84-
return json_decode($string, $toArray);
80+
return \json_decode($string, $toArray);
8581
}
8682

8783
/**
@@ -100,17 +96,17 @@ public static function format($input, $output = false, array $options = [])
10096
return false;
10197
}
10298

103-
$data = trim($input);
99+
$data = \trim($input);
104100

105-
if (file_exists($input)) {
106-
$data = file_get_contents($input);
101+
if (\file_exists($input)) {
102+
$data = \file_get_contents($input);
107103
}
108104

109105
if (!$data) {
110106
return false;
111107
}
112108

113-
$data = preg_replace([
109+
$data = \preg_replace([
114110
// 去掉所有多行注释/* .... */
115111
'/\/\*.*?\*\/\s*/is',
116112
// 去掉所有单行注释//....
@@ -124,17 +120,16 @@ public static function format($input, $output = false, array $options = [])
124120
}
125121

126122
$default = ['type' => 'min'];
127-
$options = array_merge($default, $options);
123+
$options = \array_merge($default, $options);
128124

129-
if (file_exists($input) && (empty($options['file']) || !is_file($options['file']))) {
125+
if (\file_exists($input) && (empty($options['file']) || !\is_file($options['file']))) {
130126
$dir = \dirname($input);
131-
$name = basename($input, '.json');
127+
$name = \basename($input, '.json');
132128
$file = $dir . '/' . $name . '.' . $options['type'] . '.json';
133129
$options['file'] = $file;
134130
}
135131

136132
static::saveAs($data, $options['file'], $options['type']);
137-
138133
return $data;
139134
}
140135

@@ -150,18 +145,18 @@ public static function saveAs(string $data, string $output, array $options = [])
150145
$options = array_merge($default, $options);
151146
$dir = \dirname($output);
152147

153-
if (!file_exists($dir)) {
154-
trigger_error('设置的json文件输出' . $dir . '目录不存在!');
148+
if (!\file_exists($dir)) {
149+
throw new \RuntimeException('设置的json文件输出' . $dir . '目录不存在!');
155150
}
156151

157-
$name = basename($output, '.json');
152+
$name = \basename($output, '.json');
158153
$file = $dir . '/' . $name . '.' . $options['type'] . '.json';
159154

160155
if ($options['type '] === 'min') {
161156
// 去掉空白
162-
$data = (string)preg_replace('/(?!\w)\s*?(?!\w)/i', '', $data);
157+
$data = (string)\preg_replace('/(?!\w)\s*?(?!\w)/i', '', $data);
163158
}
164159

165-
return file_put_contents($file, $data);
160+
return \file_put_contents($file, $data);
166161
}
167162
}

src/Str.php

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,56 +9,9 @@
99
namespace Toolkit\StrUtil;
1010

1111
/**
12-
* Class Str
13-
* alias of the StringHelper
12+
* Class Str alias of the StringHelper
1413
* @package Toolkit\StrUtil
1514
*/
16-
class Str extends StringHelper
15+
final class Str extends StringHelper
1716
{
18-
/**
19-
* @param string $string
20-
* @param string $prefix
21-
* @param string $suffix
22-
* @return string
23-
*/
24-
public static function optional(string $string, string $prefix = ' ', string $suffix = ''): string
25-
{
26-
if (empty($string)) {
27-
return '';
28-
}
29-
30-
return $prefix . $string . $suffix;
31-
}
32-
33-
/**
34-
* @param string $string
35-
* @param string|array $needle
36-
* @return bool
37-
*/
38-
public static function contains(string $string, $needle): bool
39-
{
40-
return self::has($string, $needle);
41-
}
42-
43-
/**
44-
* @param string $string
45-
* @param string|array $needle
46-
* @return bool
47-
*/
48-
public static function has(string $string, $needle): bool
49-
{
50-
if (\is_string($needle)) {
51-
return stripos($string, $needle) !== false;
52-
}
53-
54-
if (\is_array($needle)) {
55-
foreach ((array)$needle as $item) {
56-
if (stripos($string, $item) !== false) {
57-
return true;
58-
}
59-
}
60-
}
61-
62-
return false;
63-
}
6417
}

src/StrBuffer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class StrBuffer
1919
*/
2020
private $body;
2121

22-
public function __construct($content = '')
22+
public function __construct(string $content = '')
2323
{
2424
$this->body = $content;
2525
}

0 commit comments

Comments
 (0)