Skip to content

Commit

Permalink
update some logic for unit tests and add new helper func
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 13, 2019
1 parent beaf869 commit 827d91a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
6 changes: 3 additions & 3 deletions docs/Valid.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Valid
*
* @return Valid
*/
public static function new(array $data): self
public static function create(array $data): self
{
return new static($data);
}
Expand All @@ -33,9 +33,9 @@ public function __construct(array $data)
$this->data = $data;
}

public function getInt(string $field, $min = null, $max = null, $default = null): int
public function getInt(string $field, $min = null, $max = null, $default = 0): int
{

return 0;
}

/**
Expand Down
41 changes: 41 additions & 0 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use function is_array;
use function is_int;
use function is_object;
use function is_scalar;
use function is_string;
use function mb_strlen;
use function mb_strpos;
Expand Down Expand Up @@ -313,4 +314,44 @@ public static function compareSize($val, string $operator, $expected): bool

return $ok;
}

/**
* @param mixed $val
* @param array $list
*
* @return bool
*/
public static function inArray($val, array $list): bool
{
if (!is_scalar($val)) {
return false;
}

$valType = gettype($val);
foreach ($list as $item) {
if (!is_scalar($item)) {
continue;
}

// compare value
switch ($valType) {
case 'integer':
$exist = $val === (int)$item;
break;
case 'float':
case 'double':
case 'string':
$exist = (string)$val === (string)$item;
break;
default:
return false;
}

if ($exist) {
return true;
}
}

return false;
}
}
1 change: 1 addition & 0 deletions src/Validator/GlobalMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ final class GlobalMessage
'ipv4' => '{attr} is not a IPv4 address!',
'ipv6' => '{attr} is not a IPv6 address!',
'required' => 'parameter {attr} is required!',
'requiredIf' => 'parameter {attr} is required!',
'length' => [
'{attr} length validation is not through!',
'{attr} must be an string/array and minimum length is {min}',
Expand Down
47 changes: 46 additions & 1 deletion test/RuleValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Inhere\ValidateTest;

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

/**
* @link https://github.com/inhere/php-validate/issues/13
*/
public function testIssue13(): void
{
$rule = [
Expand All @@ -619,7 +623,9 @@ public function testIssue13(): void

$v = Validation::check([
'goods_id' => [
1144181460261978556, 114418146, 1144
// 1144181460261978556,
114418146,
1144
]
], $rule);

Expand Down Expand Up @@ -647,4 +653,43 @@ public function testIssue13(): void
$this->assertFalse($v->isOk());
$this->assertSame('商品分类id必须是一串数字', $v->firstError());
}

/**
* @link https://github.com/inhere/php-validate/issues/21
*/
public function tIssues21(): void
{
$rs = [
['users.*.id', 'required'],
['users.*.id', 'each', 'required'],
// ['users.*.id', 'each', 'string']
];

$v = RV::check([
'users' => [
['name' => 'n1'],
['name' => 'n1'],
],
], $rs);

$this->assertFalse($v->isOk());
$this->assertSame('parameter users.*.id is required!', $v->firstError());

$v = RV::check([
'users' => [
['name' => 'n1'],
['id' => 2, 'name' => 'n1'],
],
], $rs);

$this->assertFalse($v->isOk());
$this->assertSame('', $v->firstError());

$v = RV::check([
'users' => [
['id' => 1, 'name' => 'n1'],
['id' => 2, 'name' => 'n1'],
],
], $rs);
}
}

0 comments on commit 827d91a

Please sign in to comment.