Skip to content

Commit c6ca786

Browse files
committed
fix: size not work on float value
1 parent d592c40 commit c6ca786

File tree

9 files changed

+94
-34
lines changed

9 files changed

+94
-34
lines changed

README.en.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Lightweight and feature-rich PHP validation and filtering library.
1212
- Simple and convenient, support to add custom validator
1313
- Support pre-verification check, customize how to judge non-empty
1414
- Support grouping rules by scene. Or partial verification
15-
- Supports the use of filters to purify and filter values ​​before verification [built-in filter](#built-in-filters)
15+
- Supports the use of filters to purify and filter values before verification [built-in filter](#built-in-filters)
1616
- Support pre-processing and post-processing of verification [independent verification processing](#on-in-Validation)
1717
- Support to customize the error message, field translation, message translation of each verification, and support the default value
1818
- Supports basic array checking, checking of children (`'goods.apple'`) values ​​of arrays, checking of children of wildcards (`'users.*.id' 'goods.*'`)

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ class PageRequest extends Validation
191191
public function messages(): array
192192
{
193193
return [
194+
// 使用验证器名字指定消息
194195
'required' => '{attr} 是必填项。',
195196
// 可以直接针对字段的某个规则进行消息定义
196197
'title.required' => 'O, 标题是必填项。are you known?',
@@ -278,7 +279,7 @@ class UserModel extends DataModel
278279
{
279280
return [
280281
['username, passwd', 'required'],
281-
['passwd', 'compare', 'repasswd', 'on' => 'create']
282+
['passwd', 'compare', 'repasswd', 'on' => 'create'],
282283
['username', 'string', 'min' => 2, 'max' => 20, 'on' => 'create' ],
283284
['id', 'number', 'on' => 'update' ], // 仅作用于场景 update
284285
['createdAt, updatedAt', 'safe'],

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<phpunit backupGlobals="false"
44
backupStaticAttributes="false"
5-
bootstrap="./test/boot.php"
5+
bootstrap="test/bootstrap.php"
66
colors="false"
77
convertErrorsToExceptions="true"
88
convertNoticesToExceptions="true"

src/Filter/Filters.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public static function float($val, $decimal = null, $flags = FILTER_FLAG_ALLOW_F
165165
$options = (int)$flags !== 0 ? ['flags' => (int)$flags] : [];
166166

167167
$ret = filter_var($val, FILTER_SANITIZE_NUMBER_FLOAT, $options);
168-
$new = strpos($ret, '.') ? (float)$ret : $ret;
168+
$new = strpos($ret, '.') ? (float)$ret : (int)$ret;
169169

170170
if (is_int($decimal)) {
171171
return round($new, $decimal);
@@ -565,10 +565,11 @@ public static function encoded(string $val, int $flags = 0): string
565565
*/
566566
public static function quotes(string $val): string
567567
{
568-
$flag = FILTER_SANITIZE_MAGIC_QUOTES;
569568
if (PHP_VERSION_ID > 70300) {
570569
/** @noinspection PhpElementIsNotAvailableInCurrentPhpVersionInspection */
571570
$flag = FILTER_SANITIZE_ADD_SLASHES;
571+
} else {
572+
$flag = FILTER_SANITIZE_MAGIC_QUOTES;
572573
}
573574

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

src/RuleValidation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* Class RuleValidation
1313
* - alias of the Validation
1414
* - one rule to many fields. like Yii 1/2 framework
15+
*
1516
* ```php
1617
* [
1718
* ['field1, field2, ... ', 'validator', ...],

src/Validators.php

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -175,21 +175,28 @@ public static function bool($val): bool
175175
}
176176

177177
/**
178+
* check value is float
179+
*
178180
* @param mixed $val 要验证的变量
179181
* @param null|integer|float $min 最小值
180182
* @param null|int|float $max 最大值
181183
* $options = [
182-
* 'default' => 'default value',
183-
* 'decimal' => 2
184+
* 'default' => 'default value',
185+
* 'decimal' => 2
184186
* ]
185-
* @param int $flags FILTER_FLAG_ALLOW_THOUSAND
187+
* @param int|mixed $flags FILTER_FLAG_ALLOW_THOUSAND
186188
*
187-
* @return mixed
189+
* @return bool
188190
*/
189-
public static function float($val, $min = null, $max = null, $flags = 0)
191+
public static function float($val, $min = null, $max = null, $flags = 0): bool
190192
{
193+
if (!is_numeric($val)) {
194+
return false;
195+
}
196+
191197
$options = (int)$flags !== 0 ? ['flags' => (int)$flags] : [];
192198

199+
// NOTICE: FILTER_VALIDATE_FLOAT not support the 'min_range', 'max_range options.
193200
if (filter_var($val, FILTER_VALIDATE_FLOAT, $options) === false) {
194201
return false;
195202
}
@@ -223,12 +230,12 @@ public static function float($val, $min = null, $max = null, $flags = 0)
223230
/**
224231
* int 验证 (所有的最小、最大都是包含边界值的)
225232
*
226-
* @param mixed $val 要验证的变量
227-
* @param null|integer $min 最小值
228-
* @param null|int $max 最大值
229-
* @param int $flags 标志
230-
* FILTER_FLAG_ALLOW_OCTAL - 允许八进制数值
231-
* FILTER_FLAG_ALLOW_HEX - 允许十六进制数值
233+
* @param int|string $val 要验证的变量
234+
* @param null|int|string $min 最小值
235+
* @param null|int|string $max 最大值
236+
* @param int|string $flags 标志
237+
* FILTER_FLAG_ALLOW_OCTAL - 允许八进制数值
238+
* FILTER_FLAG_ALLOW_HEX - 允许十六进制数值
232239
*
233240
* @return bool false
234241
* @example
@@ -275,9 +282,9 @@ public static function integer($val, $min = null, $max = null, $flags = 0): bool
275282

276283
/**
277284
* @param int|mixed $val
278-
* @param int|null $min
279-
* @param int|null $max
280-
* @param int $flags
285+
* @param int|null $min
286+
* @param int|null $max
287+
* @param int $flags
281288
*
282289
* @return bool
283290
* @see integer()
@@ -312,9 +319,9 @@ public static function number($val, $min = null, $max = null, $flags = 0): bool
312319

313320
/**
314321
* @param int|mixed $val
315-
* @param int|null $min
316-
* @param int|null $max
317-
* @param int $flags
322+
* @param int|null $min
323+
* @param int|null $max
324+
* @param int $flags
318325
*
319326
* @return bool
320327
* @see number()
@@ -540,15 +547,15 @@ public static function max($val, $maxRange): bool
540547
* 范围检查
541548
* $min $max 即使传错位置也会自动调整
542549
*
543-
* @param int|string|array $val 待检测的值。 数字检查数字范围; 字符串、数组则检查长度
544-
* @param null|integer $min 最小值
545-
* @param null|int $max 最大值
550+
* @param int|float|string|array $val 待检测的值。 数字检查数字范围; 字符串、数组则检查长度
551+
* @param null|integer|string $min 最小值
552+
* @param null|int|string $max 最大值
546553
*
547554
* @return bool
548555
*/
549556
public static function size($val, $min = null, $max = null): bool
550557
{
551-
if (!is_int($val)) {
558+
if (!is_numeric($val)) {
552559
if (is_string($val)) {
553560
$val = Helper::strlen(trim($val));
554561
} elseif (is_array($val)) {
@@ -558,13 +565,15 @@ public static function size($val, $min = null, $max = null): bool
558565
}
559566
}
560567

561-
return self::integer($val, $min, $max);
568+
// fix: $val maybe an float.
569+
// return self::integer($val, $min, $max);
570+
return self::float($val, $min, $max);
562571
}
563572

564573
/**
565574
* @param int|string|array|mixed $val
566-
* @param int|null $min
567-
* @param int|null $max
575+
* @param int|string|null $min
576+
* @param int|string|null $max
568577
*
569578
* @return bool
570579
* @see Validators::size()
@@ -845,18 +854,24 @@ public static function english($val): bool
845854
/**
846855
* 验证字段值是否是一个有效的 JSON 字符串。
847856
*
848-
* @param mixed $val
849-
* @param bool $strict
857+
* @param mixed $val
858+
* @param bool|mixed $strict
850859
*
851860
* @return bool
852861
*/
853862
public static function json($val, $strict = true): bool
854863
{
855-
if (!$val || (!is_string($val) && !method_exists($val, '__toString'))) {
864+
if (!$val) {
856865
return false;
857866
}
858867

859-
$val = (string)$val;
868+
if (is_object($val) && method_exists($val, '__toString')) {
869+
$val = (string)$val;
870+
}
871+
872+
if (!is_string($val)) {
873+
return false;
874+
}
860875

861876
// must start with: { OR [
862877
if ($strict && '[' !== $val[0] && '{' !== $val[0]) {

test/FieldValidationTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ public function testValidateField(): void
7777

7878
$errors = $v->getErrors();
7979
$this->assertNotEmpty($errors);
80-
$this->assertCount(3, $errors);
80+
81+
$this->assertCount(4, $errors);
8182
$this->assertSame('freeTime is required!!!!', $v->getErrors('freeTime')[0]);
8283

8384
$v = FieldValidation::check($this->data, [
@@ -207,4 +208,26 @@ public function testIssues22(): void
207208
$this->assertFalse($v->isOk());
208209
$this->assertSame('age must be an integer!', $v->firstError());
209210
}
211+
212+
/**
213+
* @link https://github.com/inhere/php-validate/issues/36
214+
*/
215+
public function testIssues36(): void
216+
{
217+
$params = [];
218+
219+
$v = FieldValidation::check($params, [
220+
['owner', 'required', 'msg' => ['owner' => 'owner 缺失']],
221+
]);
222+
223+
$this->assertTrue($v->isFail());
224+
$this->assertSame('parameter owner is required!', $v->firstError());
225+
226+
$v = FieldValidation::check($params, [
227+
['owner', 'required', 'msg' => ['required' => 'owner 缺失']],
228+
]);
229+
230+
$this->assertTrue($v->isFail());
231+
$this->assertSame('owner 缺失', $v->firstError());
232+
}
210233
}

test/ValidatorsTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
use PHPUnit\Framework\TestCase;
77
use stdClass;
88

9+
/**
10+
* Class ValidatorsTest
11+
* @package Inhere\ValidateTest
12+
*/
913
class ValidatorsTest extends TestCase
1014
{
1115
public function testAliases(): void
@@ -51,6 +55,8 @@ public function testFloat(): void
5155
$this->assertTrue(Validators::float(3.4, 3.1, 5.4));
5256
$this->assertTrue(Validators::float(3.4, 4.1, 2.4));
5357
$this->assertTrue(Validators::float(3.4, null, 5.4));
58+
$this->assertTrue(Validators::float(0.8, 0, 4));
59+
$this->assertTrue(Validators::float(0.5, 0.3, 0.7));
5460
}
5561

5662
public function testInteger(): void
@@ -243,6 +249,13 @@ public function testSize(): void
243249
$this->assertTrue(Validators::size(56, 20, 100));
244250
$this->assertTrue(Validators::size('test', 2, 4));
245251
$this->assertTrue(Validators::size([3, 'test', 'hi'], 1, 4));
252+
$this->assertTrue(Validators::size(0.8, 0, 4));
253+
}
254+
255+
public function testSize_float(): void
256+
{
257+
$this->assertTrue(Validators::size(0.8, 0, 4));
258+
$this->assertTrue(Validators::size(0.5, 0.3, 0.7));
246259
}
247260

248261
public function testFixedSize(): void

test/boot.php renamed to test/bootstrap.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@
2323
}
2424
}
2525
});
26+
27+
if (is_file(dirname(__DIR__, 3) . '/autoload.php')) {
28+
require dirname(__DIR__, 3) . '/autoload.php';
29+
} elseif (is_file(dirname(__DIR__) . '/vendor/autoload.php')) {
30+
require dirname(__DIR__) . '/vendor/autoload.php';
31+
}

0 commit comments

Comments
 (0)