Skip to content

Commit 577a427

Browse files
committed
update readme. new add validators: notBe, notEqual
1 parent 64638a5 commit 577a427

File tree

5 files changed

+45
-10
lines changed

5 files changed

+45
-10
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[![License](https://img.shields.io/packagist/l/inhere/php-validate.svg?style=flat-square)](LICENSE)
44
[![Php Version](https://img.shields.io/badge/php-%3E=7.0-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)
6+
[![git branch](https://img.shields.io/badge/branch-php5-yellow.svg)](https://github.com/inhere/php-validate)
67

78
一个简洁小巧且功能完善的php验证、过滤库。仅有几个文件,无依赖。
89

@@ -13,7 +14,7 @@
1314
- 支持自定义每个验证的错误消息,字段翻译,消息翻译,支持默认值
1415
- 支持基本的数组检查,数组的子级值检查
1516
- 方便的获取错误信息,验证后的安全数据获取
16-
- 已经内置了30多个常用的验证器[内置验证器](#built-in-validators)
17+
- 已经内置了40多个常用的验证器[内置验证器](#built-in-validators)
1718
- 规则设置参考自 yii 的。部分规则参考自 laravel
1819
- `RuleValidation` 规则配置类似于Yii: 每条规则中,允许多个字段,但只能有一个验证器。
1920
- e.g `['tagId,userId,name,email,freeTime', 'required', ...]`(下面的示例都是这种)
@@ -24,7 +25,7 @@
2425
2526
## 项目地址
2627

27-
- **git@osc** https://git.oschina.net/inhere/php-validate.git
28+
- **git@osc** https://gitee.com/inhere/php-validate.git
2829
- **github** https://github.com/inhere/php-validate.git
2930

3031
**注意:**
@@ -596,8 +597,10 @@ public function get(string $key, $default = null)
596597
`length` | 长度验证( 跟 `size`差不多, 但只能验证 `string`, `array` 的长度 | `['username', 'length', 'min' => 5, 'max' => 20]`
597598
`in/enum` | 枚举验证 | `['status', 'in', [1,2,3]`
598599
`notIn` | 枚举验证 | `['status', 'notIn', [4,5,6]]`
599-
`mustBe` | 必须是等于给定值 | `['status', 'mustBe', 0]`
600-
`compare/same/equal` | 字段值比较 | `['passwd', 'compare', 'repasswd']`
600+
`mustBe` | 必须是等于给定值 | `['status', 'mustBe', 1]`
601+
`mustBe` | 不能等于给定值 | `['status', 'notBe', 0]`
602+
`compare/same/equal` | 字段值相同比较 | `['passwd', 'compare', 'repasswd']`
603+
`notEqual` | 字段值不能相同比较 | `['passwd', 'notEqual', 'repasswd']`
601604
`required` | 要求此字段/属性是必须的 | `['tagId, userId', 'required' ]`
602605
`requiredIf` | 指定的其它字段( anotherField )值等于任何一个 value 时,此字段为 **必填** | `['city', 'requiredIf', 'myCity', ['chengdu'] ]`
603606
`requiredUnless` | 指定的其它字段( anotherField )值等于任何一个 value 时,此字段为 **不必填** | `['city', 'requiredUnless', 'myCity', ['chengdu'] ]`

src/Utils/ErrorMessageTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,13 @@ trait ErrorMessageTrait
6767
],
6868
'regexp' => '{attr} does not match the {value0} conditions',
6969

70+
'mustBe' => '{attr} must be equals to {value0}',
71+
'notBe' => '{attr} can not be equals to {value0}',
72+
7073
'compare' => '{attr} must be equals to {value0}',
7174
'same' => '{attr} must be equals to {value0}',
7275
'equal' => '{attr} must be equals to {value0}',
76+
'notEqual' => '{attr} can not be equals to {value0}',
7377

7478
'isArray' => '{attr} must be an array',
7579
'isMap' => '{attr} must be an array and is key-value format',

src/Utils/Helper.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22
/**
3-
*
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-03-17
6+
* Time: 11:26
47
*/
58

69
namespace Inhere\Validate\Utils;
@@ -15,7 +18,7 @@ class Helper
1518
* known image mime types
1619
* @link https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
1720
*/
18-
const IMG_MIME_TYPES = [
21+
public static $imgMimeTypes = [
1922
'bmp' => 'image/bmp',
2023
'gif' => 'image/gif',
2124
'ief' => 'image/ief',
@@ -27,7 +30,10 @@ class Helper
2730
'ico' => 'image/x-icon',
2831
];
2932

30-
const IMG_MIME_CONSTANTS = [
33+
/**
34+
* @var array
35+
*/
36+
public static $imgMimeConstants = [
3137
IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_WBMP, IMAGETYPE_ICO
3238
];
3339

@@ -37,7 +43,7 @@ class Helper
3743
*/
3844
public static function getImageMime($ext)
3945
{
40-
return self::IMG_MIME_TYPES[$ext] ?? null;
46+
return self::$imgMimeTypes[$ext] ?? null;
4147
}
4248

4349
/**
@@ -46,9 +52,9 @@ public static function getImageMime($ext)
4652
*/
4753
public static function getImageExtByMime($mime)
4854
{
49-
$key = array_search($mime, self::IMG_MIME_TYPES, true);
55+
$key = array_search($mime, self::$imgMimeTypes, true);
5056

51-
return false !== $key ? self::IMG_MIME_TYPES[$key] : null;
57+
return false !== $key ? self::$imgMimeTypes[$key] : null;
5258
}
5359

5460
/**

src/Utils/UserAndContextValidatorsTrait.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,17 @@ public function equal($val, $compareField)
411411
return $this->compare($val, $compareField);
412412
}
413413

414+
/**
415+
* 字段值比较:当前字段值是否与给定的字段值不相同
416+
* @param mixed $val
417+
* @param string $compareField
418+
* @return bool
419+
*/
420+
public function notEqual($val, $compareField)
421+
{
422+
return $compareField && ($val !== $this->get($compareField));
423+
}
424+
414425
/*******************************************************************************
415426
* getter/setter
416427
******************************************************************************/

src/ValidatorList.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,17 @@ public static function mustBe($val, $excepted)
289289
return $val === $excepted;
290290
}
291291

292+
/**
293+
* 不能等于给定值
294+
* @param mixed $val
295+
* @param mixed $excepted
296+
* @return bool
297+
*/
298+
public static function notBe($val, $excepted)
299+
{
300+
return $val !== $excepted;
301+
}
302+
292303
/**
293304
* 最小值检查
294305
* @param int $val

0 commit comments

Comments
 (0)