Skip to content

Commit 41783c5

Browse files
committed
refactor: migrate to php8.1+, update some tests and logic
1 parent 654765b commit 41783c5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+533
-614
lines changed

.github/workflows/php.yml

+1-8
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,8 @@ jobs:
1717
strategy:
1818
fail-fast: true
1919
matrix:
20-
php: [7.3, 7.4, 8.0, 8.1] # 7.1, 7.2,
20+
php: [ 8.1] # 7.1, 7.2,
2121
os: [ubuntu-latest] # windows-latest, macOS-latest
22-
include:
23-
- os: 'ubuntu-latest'
24-
php: '7.2'
25-
phpunit: '8.5.13'
26-
- os: 'ubuntu-latest'
27-
php: '7.1'
28-
phpunit: '7.5.20'
2922

3023
steps:
3124
- name: Checkout

README.en.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# PHP Validate
22

33
[![License](https://img.shields.io/packagist/l/inhere/php-validate.svg?style=flat-square)](LICENSE)
4-
[![Php Version](https://img.shields.io/badge/php-%3E=7.1-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/inhere/php-validate)
4+
[![Php Version](https://img.shields.io/badge/php-%3E=8.1-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/inhere/php-validate)
55
[![Latest Stable Version](http://img.shields.io/packagist/v/inhere/php-validate.svg)](https://packagist.org/packages/inhere/php-validate)
66
[![Coverage Status](https://coveralls.io/repos/github/inhere/php-validate/badge.svg?branch=master)](https://coveralls.io/github/inhere/php-validate?branch=master)
77
[![Github Actions Status](https://github.com/inhere/php-validate/workflows/Unit-tests/badge.svg)](https://github.com/inhere/php-validate/actions)

README.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# PHP Validate
22

33
[![License](https://img.shields.io/packagist/l/inhere/php-validate.svg?style=flat-square)](LICENSE)
4-
[![Php Version](https://img.shields.io/badge/php-%3E=7.1-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/inhere/php-validate)
4+
[![Php Version](https://img.shields.io/badge/php-%3E=8.1-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/inhere/php-validate)
55
[![Latest Stable Version](http://img.shields.io/packagist/v/inhere/php-validate.svg)](https://packagist.org/packages/inhere/php-validate)
66
[![Coverage Status](https://coveralls.io/repos/github/inhere/php-validate/badge.svg?branch=master)](https://coveralls.io/github/inhere/php-validate?branch=master)
77
[![Github Actions Status](https://github.com/inhere/php-validate/workflows/Unit-tests/badge.svg)](https://github.com/inhere/php-validate/actions)
@@ -59,15 +59,20 @@ validate 同时支持两种规则配置方式,对应了两种规则的收集
5959
- **github** <https://github.com/inhere/php-validate.git>
6060
- **gitee** <https://gitee.com/inhere/php-validate.git>
6161

62-
> **注意:** master 分支是要求 `php7.1+` 的(推荐使用)。`1.x` 分支是支持php5的代码分支,但是基本上不再维护。
63-
6462
## 安装
6563

6664
```bash
6765
composer require inhere/php-validate
68-
// composer require inhere/php-validate ^2.2
66+
# or
67+
# composer require inhere/php-validate ^3.0
6968
```
7069

70+
### 注意
71+
72+
- `master` 分支是要求 `php8.1+` 的(推荐使用)
73+
- `2.x` 分支是支持 `php7.1+`,但是基本上不再维护。
74+
- `1.x` 分支是支持php5的代码分支,但是基本上不再维护。
75+
7176
## 立即使用
7277

7378
<a name="how-to-use1"></a>

composer.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "inhere/php-validate",
33
"type": "library",
4-
"description": "an simple validate, filter library of the php",
4+
"description": "generic data validate, filter library of the php",
55
"keywords": [
66
"php-library",
77
"library",
@@ -19,11 +19,11 @@
1919
}
2020
],
2121
"require": {
22-
"php": ">7.1.0",
23-
"toolkit/stdlib": "~1.0"
22+
"php": ">8.1.0",
23+
"toolkit/stdlib": "~2.0"
2424
},
2525
"require-dev": {
26-
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.5"
26+
"phpunit/phpunit": "^9.5"
2727
},
2828
"autoload": {
2929
"psr-4": {

src/Exception/ValidateException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ValidateException extends RuntimeException
1414
/**
1515
* @var string
1616
*/
17-
public $field;
17+
public string $field;
1818

1919
/**
2020
* @param string $field

src/Filter/FilteringTrait.php

+28-29
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,36 @@
2525
trait FilteringTrait
2626
{
2727
/** @var array user custom filters */
28-
private $_filters = [];
28+
private array $_filters = [];
2929

3030
/**
3131
* value sanitize 直接对给的值进行过滤
3232
*
33-
* @param mixed $value
34-
* @param string|array $filters
35-
* string:
33+
* filters:
34+
* string:
3635
* 'string|trim|upper'
37-
* array:
38-
* [
36+
* array:
37+
* [
3938
* 'string',
4039
* 'trim',
4140
* ['Class', 'method'],
4241
* // 追加额外参数. 传入时,第一个参数总是要过滤的字段值,其余的依次追加
4342
* 'myFilter' => ['arg1', 'arg2'],
4443
* function($val) {
45-
* return str_replace(' ', '', $val);
44+
* return str_replace(' ', '', $val);
4645
* },
47-
* ]
46+
* ]
47+
*
48+
* @param mixed $value
49+
* @param array|string|callable $filters
4850
*
4951
* @return mixed
50-
* @throws InvalidArgumentException
5152
*/
52-
protected function valueFiltering($value, $filters)
53+
protected function valueFiltering(mixed $value, array|string|callable $filters): mixed
5354
{
54-
$filters = is_string($filters) ? Filters::explode($filters, '|') : $filters;
55-
56-
// fix: must ensure is array
57-
if (!is_array($filters)) {
55+
if (is_string($filters)) {
56+
$filters = Filters::explode($filters, '|');
57+
} elseif (!is_array($filters)) {
5858
$filters = [$filters];
5959
}
6060

@@ -64,14 +64,14 @@ protected function valueFiltering($value, $filters)
6464
$args = (array)$filter;
6565
$value = $this->callStringCallback($key, $value, ...$args);
6666

67-
// closure
67+
// closure
6868
} elseif (is_object($filter) && method_exists($filter, '__invoke')) {
6969
$value = $filter($value);
70-
// string, trim, ....
70+
// string, trim, ....
7171
} elseif (is_string($filter)) {
7272
$value = $this->callStringCallback($filter, $value);
7373

74-
// e.g ['Class', 'method'],
74+
// e.g ['Class', 'method'],
7575
} else {
7676
$value = Helper::call($filter, $value);
7777
}
@@ -87,29 +87,29 @@ protected function valueFiltering($value, $filters)
8787
* @return mixed
8888
* @throws InvalidArgumentException
8989
*/
90-
protected function callStringCallback(string $filter, ...$args)
90+
protected function callStringCallback(string $filter, ...$args): mixed
9191
{
9292
// if is alias name
9393
$filterName = Filters::realName($filter);
9494

9595
// if $filter is a custom by addFiler()
9696
if ($callback = $this->getFilter($filter)) {
9797
$value = $callback(...$args);
98-
// if $filter is a custom method of the subclass.
98+
// if $filter is a custom method of the subclass.
9999
} elseif (method_exists($this, $filter . 'Filter')) {
100100
$filter .= 'Filter';
101101
$value = $this->$filter(...$args);
102102

103-
// if $filter is a custom add callback in the property {@see $_filters}.
103+
// if $filter is a custom add callback in the property {@see $_filters}.
104104
} elseif ($callback = UserFilters::get($filter)) {
105105
$value = $callback(...$args);
106106

107-
// if $filter is a custom add callback in the property {@see $_filters}.
107+
// if $filter is a custom add callback in the property {@see $_filters}.
108108
// $filter is a method of the class 'FilterList'
109109
} elseif (method_exists(Filters::class, $filterName)) {
110110
$value = Filters::$filterName(...$args);
111111

112-
// it is function name
112+
// it is function name
113113
} elseif (function_exists($filter)) {
114114
$value = $filter(...$args);
115115
} else {
@@ -134,28 +134,27 @@ public function getFilter(string $name): ?callable
134134
}
135135

136136
/**
137-
* @param string $name
137+
* @param string $name
138138
* @param callable $filter
139139
*
140-
* @return $this
140+
* @return static
141141
*/
142-
public function addFilter(string $name, callable $filter): self
142+
public function addFilter(string $name, callable $filter): static
143143
{
144144
return $this->setFilter($name, $filter);
145145
}
146146

147147
/**
148-
* @param string $name
148+
* @param string $name
149149
* @param callable $filter
150150
*
151-
* @return $this
151+
* @return static
152152
*/
153-
public function setFilter(string $name, callable $filter): self
153+
public function setFilter(string $name, callable $filter): static
154154
{
155155
if ($name = trim($name)) {
156156
$this->_filters[$name] = $filter;
157157
}
158-
159158
return $this;
160159
}
161160

0 commit comments

Comments
 (0)