Skip to content

Commit

Permalink
refactor: migrate to php8.1+, update some tests and logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 4, 2022
1 parent 654765b commit 41783c5
Show file tree
Hide file tree
Showing 42 changed files with 533 additions and 614 deletions.
9 changes: 1 addition & 8 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,8 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.3, 7.4, 8.0, 8.1] # 7.1, 7.2,
php: [ 8.1] # 7.1, 7.2,
os: [ubuntu-latest] # windows-latest, macOS-latest
include:
- os: 'ubuntu-latest'
php: '7.2'
phpunit: '8.5.13'
- os: 'ubuntu-latest'
php: '7.1'
phpunit: '7.5.20'

steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion README.en.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# PHP Validate

[![License](https://img.shields.io/packagist/l/inhere/php-validate.svg?style=flat-square)](LICENSE)
[![Php Version](https://img.shields.io/badge/php-%3E=7.1-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/inhere/php-validate)
[![Php Version](https://img.shields.io/badge/php-%3E=8.1-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/inhere/php-validate)
[![Latest Stable Version](http://img.shields.io/packagist/v/inhere/php-validate.svg)](https://packagist.org/packages/inhere/php-validate)
[![Coverage Status](https://coveralls.io/repos/github/inhere/php-validate/badge.svg?branch=master)](https://coveralls.io/github/inhere/php-validate?branch=master)
[![Github Actions Status](https://github.com/inhere/php-validate/workflows/Unit-tests/badge.svg)](https://github.com/inhere/php-validate/actions)
Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# PHP Validate

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

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

```bash
composer require inhere/php-validate
// composer require inhere/php-validate ^2.2
# or
# composer require inhere/php-validate ^3.0
```

### 注意

- `master` 分支是要求 `php8.1+` 的(推荐使用)
- `2.x` 分支是支持 `php7.1+`,但是基本上不再维护。
- `1.x` 分支是支持php5的代码分支,但是基本上不再维护。

## 立即使用

<a name="how-to-use1"></a>
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "inhere/php-validate",
"type": "library",
"description": "an simple validate, filter library of the php",
"description": "generic data validate, filter library of the php",
"keywords": [
"php-library",
"library",
Expand All @@ -19,11 +19,11 @@
}
],
"require": {
"php": ">7.1.0",
"toolkit/stdlib": "~1.0"
"php": ">8.1.0",
"toolkit/stdlib": "~2.0"
},
"require-dev": {
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.5"
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/ValidateException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ValidateException extends RuntimeException
/**
* @var string
*/
public $field;
public string $field;

/**
* @param string $field
Expand Down
57 changes: 28 additions & 29 deletions src/Filter/FilteringTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,36 @@
trait FilteringTrait
{
/** @var array user custom filters */
private $_filters = [];
private array $_filters = [];

/**
* value sanitize 直接对给的值进行过滤
*
* @param mixed $value
* @param string|array $filters
* string:
* filters:
* string:
* 'string|trim|upper'
* array:
* [
* array:
* [
* 'string',
* 'trim',
* ['Class', 'method'],
* // 追加额外参数. 传入时,第一个参数总是要过滤的字段值,其余的依次追加
* 'myFilter' => ['arg1', 'arg2'],
* function($val) {
* return str_replace(' ', '', $val);
* return str_replace(' ', '', $val);
* },
* ]
* ]
*
* @param mixed $value
* @param array|string|callable $filters
*
* @return mixed
* @throws InvalidArgumentException
*/
protected function valueFiltering($value, $filters)
protected function valueFiltering(mixed $value, array|string|callable $filters): mixed
{
$filters = is_string($filters) ? Filters::explode($filters, '|') : $filters;

// fix: must ensure is array
if (!is_array($filters)) {
if (is_string($filters)) {
$filters = Filters::explode($filters, '|');
} elseif (!is_array($filters)) {
$filters = [$filters];
}

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

// closure
// closure
} elseif (is_object($filter) && method_exists($filter, '__invoke')) {
$value = $filter($value);
// string, trim, ....
// string, trim, ....
} elseif (is_string($filter)) {
$value = $this->callStringCallback($filter, $value);

// e.g ['Class', 'method'],
// e.g ['Class', 'method'],
} else {
$value = Helper::call($filter, $value);
}
Expand All @@ -87,29 +87,29 @@ protected function valueFiltering($value, $filters)
* @return mixed
* @throws InvalidArgumentException
*/
protected function callStringCallback(string $filter, ...$args)
protected function callStringCallback(string $filter, ...$args): mixed
{
// if is alias name
$filterName = Filters::realName($filter);

// if $filter is a custom by addFiler()
if ($callback = $this->getFilter($filter)) {
$value = $callback(...$args);
// if $filter is a custom method of the subclass.
// if $filter is a custom method of the subclass.
} elseif (method_exists($this, $filter . 'Filter')) {
$filter .= 'Filter';
$value = $this->$filter(...$args);

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

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

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

/**
* @param string $name
* @param string $name
* @param callable $filter
*
* @return $this
* @return static
*/
public function addFilter(string $name, callable $filter): self
public function addFilter(string $name, callable $filter): static
{
return $this->setFilter($name, $filter);
}

/**
* @param string $name
* @param string $name
* @param callable $filter
*
* @return $this
* @return static
*/
public function setFilter(string $name, callable $filter): self
public function setFilter(string $name, callable $filter): static
{
if ($name = trim($name)) {
$this->_filters[$name] = $filter;
}

return $this;
}

Expand Down

0 comments on commit 41783c5

Please sign in to comment.