Skip to content

Commit 827d91a

Browse files
committed
update some logic for unit tests and add new helper func
1 parent beaf869 commit 827d91a

File tree

4 files changed

+91
-4
lines changed

4 files changed

+91
-4
lines changed

docs/Valid.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Valid
1818
*
1919
* @return Valid
2020
*/
21-
public static function new(array $data): self
21+
public static function create(array $data): self
2222
{
2323
return new static($data);
2424
}
@@ -33,9 +33,9 @@ public function __construct(array $data)
3333
$this->data = $data;
3434
}
3535

36-
public function getInt(string $field, $min = null, $max = null, $default = null): int
36+
public function getInt(string $field, $min = null, $max = null, $default = 0): int
3737
{
38-
38+
return 0;
3939
}
4040

4141
/**

src/Helper.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use function is_array;
2424
use function is_int;
2525
use function is_object;
26+
use function is_scalar;
2627
use function is_string;
2728
use function mb_strlen;
2829
use function mb_strpos;
@@ -313,4 +314,44 @@ public static function compareSize($val, string $operator, $expected): bool
313314

314315
return $ok;
315316
}
317+
318+
/**
319+
* @param mixed $val
320+
* @param array $list
321+
*
322+
* @return bool
323+
*/
324+
public static function inArray($val, array $list): bool
325+
{
326+
if (!is_scalar($val)) {
327+
return false;
328+
}
329+
330+
$valType = gettype($val);
331+
foreach ($list as $item) {
332+
if (!is_scalar($item)) {
333+
continue;
334+
}
335+
336+
// compare value
337+
switch ($valType) {
338+
case 'integer':
339+
$exist = $val === (int)$item;
340+
break;
341+
case 'float':
342+
case 'double':
343+
case 'string':
344+
$exist = (string)$val === (string)$item;
345+
break;
346+
default:
347+
return false;
348+
}
349+
350+
if ($exist) {
351+
return true;
352+
}
353+
}
354+
355+
return false;
356+
}
316357
}

src/Validator/GlobalMessage.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ final class GlobalMessage
3636
'ipv4' => '{attr} is not a IPv4 address!',
3737
'ipv6' => '{attr} is not a IPv6 address!',
3838
'required' => 'parameter {attr} is required!',
39+
'requiredIf' => 'parameter {attr} is required!',
3940
'length' => [
4041
'{attr} length validation is not through!',
4142
'{attr} must be an string/array and minimum length is {min}',

test/RuleValidationTest.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Inhere\ValidateTest;
44

55
use Inhere\Validate\RuleValidation;
6+
use Inhere\Validate\RV;
67
use Inhere\Validate\Validation;
78
use PHPUnit\Framework\TestCase;
89
use PHPUnit\Runner\Version;
@@ -610,6 +611,9 @@ public function testValidatorAlias(): void
610611
$this->assertEquals('list val must be an array', $v->lastError());
611612
}
612613

614+
/**
615+
* @link https://github.com/inhere/php-validate/issues/13
616+
*/
613617
public function testIssue13(): void
614618
{
615619
$rule = [
@@ -619,7 +623,9 @@ public function testIssue13(): void
619623

620624
$v = Validation::check([
621625
'goods_id' => [
622-
1144181460261978556, 114418146, 1144
626+
// 1144181460261978556,
627+
114418146,
628+
1144
623629
]
624630
], $rule);
625631

@@ -647,4 +653,43 @@ public function testIssue13(): void
647653
$this->assertFalse($v->isOk());
648654
$this->assertSame('商品分类id必须是一串数字', $v->firstError());
649655
}
656+
657+
/**
658+
* @link https://github.com/inhere/php-validate/issues/21
659+
*/
660+
public function tIssues21(): void
661+
{
662+
$rs = [
663+
['users.*.id', 'required'],
664+
['users.*.id', 'each', 'required'],
665+
// ['users.*.id', 'each', 'string']
666+
];
667+
668+
$v = RV::check([
669+
'users' => [
670+
['name' => 'n1'],
671+
['name' => 'n1'],
672+
],
673+
], $rs);
674+
675+
$this->assertFalse($v->isOk());
676+
$this->assertSame('parameter users.*.id is required!', $v->firstError());
677+
678+
$v = RV::check([
679+
'users' => [
680+
['name' => 'n1'],
681+
['id' => 2, 'name' => 'n1'],
682+
],
683+
], $rs);
684+
685+
$this->assertFalse($v->isOk());
686+
$this->assertSame('', $v->firstError());
687+
688+
$v = RV::check([
689+
'users' => [
690+
['id' => 1, 'name' => 'n1'],
691+
['id' => 2, 'name' => 'n1'],
692+
],
693+
], $rs);
694+
}
650695
}

0 commit comments

Comments
 (0)