Skip to content

Commit

Permalink
update some. add before and after on rule settings
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 26, 2020
1 parent 3994917 commit e900a44
Show file tree
Hide file tree
Showing 35 changed files with 362 additions and 238 deletions.
44 changes: 18 additions & 26 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,21 @@ $header = <<<'EOF'
@license https://github.com/inhere/php-validate/blob/master/LICENSE
EOF;

return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
// 'header_comment' => [
// 'comment_type' => 'PHPDoc',
// 'header' => $header,
// 'separate' => 'none'
// ],
'array_syntax' => [
'syntax' => 'short'
],
'single_quote' => true,
'class_attributes_separation' => true,
'no_unused_imports' => true,
'standardize_not_equals' => true,
'declare_strict_types' => true,
])
->setFinder(
PhpCsFixer\Finder::create()
// ->exclude('test')
->exclude('docs')
->exclude('vendor')
->in(__DIR__)
)
->setUsingCache(false);
return PhpCsFixer\Config::create()->setRiskyAllowed(true)->setRules([
'@PSR2' => true,
// 'header_comment' => [
// 'comment_type' => 'PHPDoc',
// 'header' => $header,
// 'separate' => 'none'
// ],
'array_syntax' => [
'syntax' => 'short'
],
'single_quote' => true,
'class_attributes_separation' => true,
'no_unused_imports' => true,
'standardize_not_equals' => true,
'declare_strict_types' => true,
])->setFinder(PhpCsFixer\Finder::create()
// ->exclude('test')
->exclude('docs')->exclude('vendor')->in(__DIR__))->setUsingCache(false);
20 changes: 10 additions & 10 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
convertWarningsToExceptions="true"
stopOnFailure="false"
>
<testsuites>
<testsuite name="Php Validate-Filter Test Suite">
<directory>test</directory>
</testsuite>
</testsuites>
<testsuites>
<testsuite name="Php Validate-Filter Test Suite">
<directory>test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
6 changes: 2 additions & 4 deletions src/AbstractValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/**
* Class AbstractValidation
*
* @package Inhere\Validate
*/
abstract class AbstractValidation implements ValidationInterface
Expand Down Expand Up @@ -44,10 +45,7 @@ public function __construct(
bool $startValidate = false
) {
$this->data = $data;
$this
->atScene($scene)
->setRules($rules)
->setTranslates($translates);
$this->atScene($scene)->setRules($rules)->setTranslates($translates);

if ($startValidate) {
$this->validate();
Expand Down
1 change: 1 addition & 0 deletions src/FV.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/**
* Class FV - the short name of the field validation
*
* @package Inhere\Validate
*/
final class FV extends FieldValidation
Expand Down
25 changes: 13 additions & 12 deletions src/Filter/FilteringTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

/**
* Trait FilteringTrait
*
* @package Inhere\Validate\Filter
*/
trait FilteringTrait
Expand All @@ -31,19 +32,19 @@ trait FilteringTrait
*
* @param mixed $value
* @param string|array $filters
* string:
* 'string|trim|upper'
* array:
* [
* 'string',
* 'trim',
* ['Class', 'method'],
* // 追加额外参数. 传入时,第一个参数总是要过滤的字段值,其余的依次追加
* 'myFilter' => ['arg1', 'arg2'],
* function($val) {
* string:
* 'string|trim|upper'
* array:
* [
* 'string',
* 'trim',
* ['Class', 'method'],
* // 追加额外参数. 传入时,第一个参数总是要过滤的字段值,其余的依次追加
* 'myFilter' => ['arg1', 'arg2'],
* function($val) {
* return str_replace(' ', '', $val);
* },
* ]
* },
* ]
*
* @return mixed
* @throws InvalidArgumentException
Expand Down
62 changes: 37 additions & 25 deletions src/Filter/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@
use const FILTER_UNSAFE_RAW;
use const FILTER_VALIDATE_BOOLEAN;
use const MB_CASE_TITLE;
use const PHP_VERSION_ID;

/**
* Class Filters
*
* @package Inhere\Validate\Filter
*/
final class Filters
Expand Down Expand Up @@ -109,6 +111,7 @@ public static function bool($val, $nullAsFalse = false): bool

/**
* 过滤器删除数字中所有非法的字符。
*
* @note 该过滤器允许所有数字以及 . + -
*
* @param mixed $val 要过滤的变量
Expand Down Expand Up @@ -145,14 +148,15 @@ public static function abs($val): int

/**
* 过滤器删除浮点数中所有非法的字符。
*
* @note 该过滤器默认允许所有数字以及 + -
*
* @param mixed $val 要过滤的变量
* @param mixed $val 要过滤的变量
* @param null|int $decimal
* @param int $flags 标志
* FILTER_FLAG_ALLOW_FRACTION - 允许小数分隔符 (比如 .)
* FILTER_FLAG_ALLOW_THOUSAND - 允许千位分隔符(比如 ,)
* FILTER_FLAG_ALLOW_SCIENTIFIC - 允许科学记数法(比如 e 和 E)
* FILTER_FLAG_ALLOW_FRACTION - 允许小数分隔符 (比如 .)
* FILTER_FLAG_ALLOW_THOUSAND - 允许千位分隔符(比如 ,)
* FILTER_FLAG_ALLOW_SCIENTIFIC - 允许科学记数法(比如 e 和 E)
*
* @return mixed
*/
Expand All @@ -175,12 +179,12 @@ public static function float($val, $decimal = null, $flags = FILTER_FLAG_ALLOW_F
*
* @param string|array $val
* @param int $flags 标志
* FILTER_FLAG_NO_ENCODE_QUOTES - 该标志不编码引号
* FILTER_FLAG_STRIP_LOW - 去除 ASCII 值在 32 以下的字符
* FILTER_FLAG_STRIP_HIGH - 去除 ASCII 值在 127 以上的字符
* FILTER_FLAG_ENCODE_LOW - 编码 ASCII 值在 32 以下的字符
* FILTER_FLAG_ENCODE_HIGH - 编码 ASCII 值在 127 以上的字符
* FILTER_FLAG_ENCODE_AMP - 把 & 字符编码为 &amp;
* FILTER_FLAG_NO_ENCODE_QUOTES - 该标志不编码引号
* FILTER_FLAG_STRIP_LOW - 去除 ASCII 值在 32 以下的字符
* FILTER_FLAG_STRIP_HIGH - 去除 ASCII 值在 127 以上的字符
* FILTER_FLAG_ENCODE_LOW - 编码 ASCII 值在 32 以下的字符
* FILTER_FLAG_ENCODE_HIGH - 编码 ASCII 值在 127 以上的字符
* FILTER_FLAG_ENCODE_AMP - 把 & 字符编码为 &amp;
*
* @return string|array
*/
Expand Down Expand Up @@ -276,7 +280,7 @@ public static function lower($val): string
public static function lowercase($val): string
{
if (!$val || !is_string($val)) {
return is_int($val) ? (string)$val: '';
return is_int($val) ? (string)$val : '';
}

if (function_exists('mb_strtolower')) {
Expand Down Expand Up @@ -533,14 +537,15 @@ public static function stripTags($val, $allowedTags = null): string

/**
* 去除 URL 编码不需要的字符。
*
* @note 与 urlencode() 函数很类似。
*
* @param string $val 要过滤的数据
* @param string $val 要过滤的数据
* @param int $flags 标志
* FILTER_FLAG_STRIP_LOW - 去除 ASCII 值在 32 以下的字符
* FILTER_FLAG_STRIP_HIGH - 去除 ASCII 值在 32 以上的字符
* FILTER_FLAG_ENCODE_LOW - 编码 ASCII 值在 32 以下的字符
* FILTER_FLAG_ENCODE_HIGH - 编码 ASCII 值在 32 以上的字符
* FILTER_FLAG_STRIP_LOW - 去除 ASCII 值在 32 以下的字符
* FILTER_FLAG_STRIP_HIGH - 去除 ASCII 值在 32 以上的字符
* FILTER_FLAG_ENCODE_LOW - 编码 ASCII 值在 32 以下的字符
* FILTER_FLAG_ENCODE_HIGH - 编码 ASCII 值在 32 以上的字符
*
* @return string
*/
Expand All @@ -560,17 +565,23 @@ public static function encoded(string $val, int $flags = 0): string
*/
public static function quotes(string $val): string
{
return (string)filter_var($val, FILTER_SANITIZE_MAGIC_QUOTES);
$flag = FILTER_SANITIZE_MAGIC_QUOTES;
if (PHP_VERSION_ID > 70300) {
/** @noinspection PhpElementIsNotAvailableInCurrentPhpVersionInspection */
$flag = FILTER_SANITIZE_ADD_SLASHES;
}

return (string)filter_var($val, $flag);
}

/**
* like htmlspecialchars(), HTML 转义字符 '"<>& 以及 ASCII 值小于 32 的字符。
*
* @param string $val
* @param int $flags 标志
* FILTER_FLAG_STRIP_LOW - 去除 ASCII 值在 32 以下的字符
* FILTER_FLAG_STRIP_HIGH - 去除 ASCII 值在 32 以上的字符
* FILTER_FLAG_ENCODE_HIGH - 编码 ASCII 值在 32 以上的字符
* FILTER_FLAG_STRIP_LOW - 去除 ASCII 值在 32 以下的字符
* FILTER_FLAG_STRIP_HIGH - 去除 ASCII 值在 32 以上的字符
* FILTER_FLAG_ENCODE_HIGH - 编码 ASCII 值在 32 以上的字符
*
* @return string
*/
Expand Down Expand Up @@ -639,6 +650,7 @@ public static function cut($string, $start = 0, $length = 0): string

/**
* url地址过滤 移除所有不符合 url 的字符
*
* @note 该过滤器允许所有的字母、数字以及 $-_.+!*'(),{}|\^~[]`"><#%;/?:@&=
*
* @param string $val 要过滤的数据
Expand Down Expand Up @@ -677,11 +689,11 @@ public static function email($val): string
*
* @param string $string
* @param int $flags 标志
* FILTER_FLAG_STRIP_LOW - 去除 ASCII 值在 32 以下的字符
* FILTER_FLAG_STRIP_HIGH - 去除 ASCII 值在 32 以上的字符
* FILTER_FLAG_ENCODE_LOW - 编码 ASCII 值在 32 以下的字符
* FILTER_FLAG_ENCODE_HIGH - 编码 ASCII 值在 32 以上的字符
* FILTER_FLAG_ENCODE_AMP - 把 & 字符编码为 &amp;
* FILTER_FLAG_STRIP_LOW - 去除 ASCII 值在 32 以下的字符
* FILTER_FLAG_STRIP_HIGH - 去除 ASCII 值在 32 以上的字符
* FILTER_FLAG_ENCODE_LOW - 编码 ASCII 值在 32 以下的字符
* FILTER_FLAG_ENCODE_HIGH - 编码 ASCII 值在 32 以上的字符
* FILTER_FLAG_ENCODE_AMP - 把 & 字符编码为 &amp;
*
* @return string|mixed
*/
Expand Down
1 change: 1 addition & 0 deletions src/Filter/Filtration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

/**
* Class Filtration
*
* @package Inhere\Validate\Filter
* usage:
*
Expand Down
1 change: 1 addition & 0 deletions src/Filter/UserFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

/**
* Class UserFilters - user custom add global filters
*
* @package Inhere\Validate\Filter
*/
final class UserFilters
Expand Down
8 changes: 5 additions & 3 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,20 @@

/**
* Class Helper
*
* @package Inhere\Validate\Utils
*/
class Helper
{
public const IS_TRUE = '|yes|on|1|true|';
public const IS_TRUE = '|yes|on|1|true|';

public const IS_FALSE = '|no|off|0|false|';

public const IS_BOOL = '|yes|on|1|true|no|off|0|false|';
public const IS_BOOL = '|yes|on|1|true|no|off|0|false|';

/**
* known image mime types
*
* @link https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
*/
public static $imgMimeTypes = [
Expand Down Expand Up @@ -318,7 +320,7 @@ public static function compareSize($val, string $operator, $expected): bool
}

/**
* @param mixed $val
* @param mixed $val
* @param array $list
*
* @return bool
Expand Down
1 change: 1 addition & 0 deletions src/RV.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/**
* Class RV - the short name of the rule validation
*
* @package Inhere\Validate
*/
final class RV extends Validation
Expand Down
1 change: 1 addition & 0 deletions src/RuleValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* ['field1, field3, ... ', 'validator', ...]
* ]
* ```
*
* @package Inhere\Validate
*/
class RuleValidation extends Validation
Expand Down
Loading

0 comments on commit e900a44

Please sign in to comment.