Skip to content

Commit 9ceb1ca

Browse files
committed
添加 sanitize 数据清洗方法
1 parent 7deb3b4 commit 9ceb1ca

18 files changed

+498
-2
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Table of Contents
1818
* [starts_with](#starts_with)
1919
* [ends_with](#ends_with)
2020
* [contains](#contains)
21+
* [sanitize](#sanitize)
2122
* [JSON](#json)
2223
* [is_json](#is_json)
2324
* [文件目录](#文件目录)
@@ -78,6 +79,33 @@ Table of Contents
7879
echo contains('Hello', ['hll', 'ell']); // true
7980
echo contains('Hello', ['hll', '']); // false
8081

82+
### sanitize
83+
84+
`sanitize` 使用对应过滤标识进行数据清洗,如:
85+
86+
echo sanitize('!100a019.01a', 'int'); // 10001901
87+
echo sanitize('{"data":123}', 'string'); // {"data":123}
88+
echo sanitize('some(one)@exa\\mple.com', 'email'); // [email protected]
89+
90+
可用的过滤标识有:
91+
92+
标识 | 描述
93+
-------------|--------------------------------------------------------
94+
int | 整数
95+
absint | 绝对值
96+
float | 小数
97+
alnum | 字母和数字
98+
alpha | 字母
99+
email | 邮箱
100+
url | URL
101+
trim | 同 trim()
102+
string | 字符串
103+
strip_tags | 同 strip_tags()
104+
special | 将特殊字符转换为 HTML Entity Name:如 `<` 转为 `&#60;`
105+
special_full | 将特殊字符转换为 HTML Entity Number:如 `<` 转为 `&lt;`
106+
lower | 转为小写
107+
upper | 转为大写
108+
81109
## JSON
82110

83111
### is_json

composer.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
],
1818
"require": {
1919
"php": ">=5.5.0",
20-
"ext-mbstring": "*"
20+
"ext-mbstring": "*",
21+
"ext-json": "*",
22+
"ext-ctype": "*"
2123
},
2224
"autoload": {
2325
"files": [
@@ -28,5 +30,12 @@
2830
"psr-4": {
2931
"Soli\\Tests\\": "tests/"
3032
}
31-
}
33+
},
34+
"extra": {
35+
"branch-alias": {
36+
"dev-master": "1.2-dev"
37+
}
38+
},
39+
"minimum-stability": "dev",
40+
"prefer-stable": true
3241
}

src/helpers.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,69 @@ function env_file($envFile = '.env')
245245
return $envFile;
246246
}
247247
}
248+
249+
if (!function_exists('sanitize')) { // @codeCoverageIgnore
250+
/**
251+
* 使用对应过滤标识进行过滤
252+
*
253+
*<code>
254+
* echo sanitize('!100a019.01a', 'int'); // 10001901
255+
* echo sanitize('{"data":123}', 'string'); // {&#34;data&#34;:123}
256+
* echo sanitize('some(one)@exa\\mple.com', 'email'); // [email protected]
257+
*</code>
258+
*
259+
* @param mixed $value
260+
* @param string $filter
261+
* @return mixed
262+
* @throws \InvalidArgumentException
263+
*/
264+
function sanitize($value, $filter)
265+
{
266+
switch ($filter) {
267+
case 'int':
268+
return intval(filter_var($value, FILTER_SANITIZE_NUMBER_INT));
269+
270+
case 'absint':
271+
return abs(intval(filter_var($value, FILTER_SANITIZE_NUMBER_INT)));
272+
273+
case 'float':
274+
return doubleval(filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT, ['flags' => FILTER_FLAG_ALLOW_FRACTION]));
275+
276+
case 'alnum':
277+
return preg_replace('/[^A-Za-z0-9]/', '', $value);
278+
279+
case 'alpha':
280+
return preg_replace('/[^A-Za-z]/', '', $value);
281+
282+
case 'email':
283+
return filter_var($value, FILTER_SANITIZE_EMAIL, FILTER_FLAG_EMAIL_UNICODE);
284+
285+
case 'url':
286+
return filter_var($value, FILTER_SANITIZE_URL);
287+
288+
case 'trim':
289+
return trim($value);
290+
291+
case 'string':
292+
return filter_var($value, FILTER_SANITIZE_STRING);
293+
294+
case 'strip_tags':
295+
return strip_tags($value);
296+
297+
case 'special':
298+
return filter_var($value, FILTER_SANITIZE_SPECIAL_CHARS);
299+
300+
case 'special_full':
301+
return filter_var($value, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
302+
303+
case 'lower':
304+
return mb_strtolower($value, 'UTF-8');
305+
306+
case 'upper':
307+
return mb_strtoupper($value, 'UTF-8');
308+
309+
default:
310+
throw new \InvalidArgumentException("Sanitize filter $filter is not supported");
311+
}
312+
}
313+
}

tests/Sanitize/AbsIntTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Soli\Tests\Sanitize;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class AbsIntTest extends TestCase
8+
{
9+
/**
10+
* @dataProvider getData
11+
*/
12+
public function testSanitize($value, $expected)
13+
{
14+
$actual = sanitize($value, "absint");
15+
$this->assertEquals($expected, $actual);
16+
}
17+
18+
public function getData()
19+
{
20+
return [
21+
[-125, 125],
22+
['-125', 125],
23+
['-!1a2b5', 125],
24+
];
25+
}
26+
}

tests/Sanitize/AlnumTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Soli\Tests\Sanitize;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class AlnumTest extends TestCase
8+
{
9+
/**
10+
* @dataProvider getData
11+
*/
12+
public function testSanitize($value, $expected)
13+
{
14+
$actual = sanitize($value, "alnum");
15+
$this->assertEquals($expected, $actual);
16+
}
17+
18+
public function getData()
19+
{
20+
return [
21+
['0', 0],
22+
['', null],
23+
['?a&5xka\tŧ?1-s.Xa[\n', 'a5xkat1sXan'],
24+
];
25+
}
26+
}

tests/Sanitize/AlphaTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Soli\Tests\Sanitize;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class AlphaTest extends TestCase
8+
{
9+
/**
10+
* @dataProvider getData
11+
*/
12+
public function testSanitize($value, $expected)
13+
{
14+
$actual = sanitize($value, "alpha");
15+
$this->assertEquals($expected, $actual);
16+
}
17+
18+
public function getData()
19+
{
20+
return [
21+
['0', ''],
22+
['', null],
23+
['a5@xkat1s!Xan', 'axkatsXan'],
24+
];
25+
}
26+
}

tests/Sanitize/EmailTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Soli\Tests\Sanitize;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class EmailTest extends TestCase
8+
{
9+
/**
10+
* @dataProvider getData
11+
*/
12+
public function testSanitize($value, $expected)
13+
{
14+
$actual = sanitize($value, "email");
15+
$this->assertEquals($expected, $actual);
16+
}
17+
18+
public function getData()
19+
{
20+
return [
21+
['some(one)@exa\\mple.com', '[email protected]'],
22+
[
23+
'!(first.guy)
24+
@*my-domain**##.com.rx//', '!first.guy@*my-domain**##.com.rx',
25+
],
26+
];
27+
}
28+
}

tests/Sanitize/FloatTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Soli\Tests\Sanitize;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class FloatTest extends TestCase
8+
{
9+
/**
10+
* @dataProvider getData
11+
*/
12+
public function testSanitize($value, $expected)
13+
{
14+
$actual = sanitize($value, "float");
15+
$this->assertEquals($expected, $actual);
16+
}
17+
18+
public function getData()
19+
{
20+
return [
21+
['1000.01', 1000.01],
22+
[0xFFA, 0xFFA],
23+
['lol', 0.0],
24+
];
25+
}
26+
}

tests/Sanitize/IntTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Soli\Tests\Sanitize;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class IntTest extends TestCase
8+
{
9+
/**
10+
* @dataProvider getData
11+
*/
12+
public function testSanitize($value, $expected)
13+
{
14+
$actual = sanitize($value, "int");
15+
$this->assertEquals($expected, $actual);
16+
}
17+
18+
public function getData()
19+
{
20+
return [
21+
[1000, 1000],
22+
[0xFFA, 0xFFA],
23+
['1000', '1000'],
24+
['lol', 0],
25+
['!100a019.01a', 10001901],
26+
];
27+
}
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Soli\Tests\Sanitize;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class InvalidArgumentExceptionTest extends TestCase
8+
{
9+
/**
10+
* @expectedException \Exception
11+
* @expectedExceptionMessageRegExp /Sanitize filter \w+ is not supported/
12+
*/
13+
public function testSanitizeInvalidArgumentException()
14+
{
15+
sanitize("100", "InvalidArgument");
16+
}
17+
}

0 commit comments

Comments
 (0)