Skip to content

Commit 3994917

Browse files
committed
fix some error on strict mode
1 parent 6f7634f commit 3994917

File tree

7 files changed

+42
-7
lines changed

7 files changed

+42
-7
lines changed

src/Filter/Filters.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public static function lower($val): string
276276
public static function lowercase($val): string
277277
{
278278
if (!$val || !is_string($val)) {
279-
return is_int($val) ? $val : '';
279+
return is_int($val) ? (string)$val: '';
280280
}
281281

282282
if (function_exists('mb_strtolower')) {
@@ -308,7 +308,7 @@ public static function upper($val): string
308308
public static function uppercase($str): string
309309
{
310310
if (!$str || !is_string($str)) {
311-
return is_int($str) ? $str : '';
311+
return is_int($str) ? (string)$str : '';
312312
}
313313

314314
if (function_exists('mb_strtoupper')) {

src/Traits/ScopedValidatorsTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ public function fileValidator(string $field, $suffixes = null): bool
352352
return true;
353353
}
354354

355-
if (!$suffix = trim(strrchr($file['name'], '.'), '.')) {
355+
if (!$suffix = trim((string)strrchr($file['name'], '.'), '.')) {
356356
return false;
357357
}
358358

src/ValidationTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ protected function getByWildcard(string $path, $default = null)
573573
$result[] = isset($item[$field]) ? $item[$field] : new ArrayValueNotExists();
574574
}
575575
}
576+
576577
return $result;
577578
}
578579

src/Validators.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public static function accepted($val): bool
351351
*/
352352
public static function alpha($val): bool
353353
{
354-
return is_string($val) && preg_match('/^(?:[a-zA-Z]+)$/', $val);
354+
return is_string($val) && preg_match('/^(?:[a-zA-Z]+)$/', $val) === 1;
355355
}
356356

357357
/**
@@ -363,11 +363,11 @@ public static function alpha($val): bool
363363
*/
364364
public static function alphaNum($val): bool
365365
{
366-
if (!is_string($val) && !is_numeric($val)) {
366+
if (!is_string($val) || !is_numeric($val)) {
367367
return false;
368368
}
369369

370-
return 1 === preg_match('/^(?:[a-zA-Z0-9]+)$/', $val);
370+
return 1 === preg_match('/^(?:[a-zA-Z0-9]+)$/', (string)$val);
371371
}
372372

373373
/**
@@ -792,7 +792,7 @@ public static function macAddress($input): bool
792792
return false;
793793
}
794794

795-
return preg_match('/^(([0-9a-fA-F]{2}-){5}|([0-9a-fA-F]{2}:){5})[0-9a-fA-F]{2}$/', $input);
795+
return 1 === preg_match('/^(([0-9a-fA-F]{2}-){5}|([0-9a-fA-F]{2}:){5})[0-9a-fA-F]{2}$/', $input);
796796
}
797797

798798
/**

test/RuleValidationTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,31 @@ public function testIssue13(): void
691691
$this->assertSame('商品分类id必须是一串数字', $v->firstError());
692692
}
693693

694+
/**
695+
* @link https://github.com/inhere/php-validate/issues/17
696+
*/
697+
public function tIssues17(): void
698+
{
699+
$rs = [
700+
['users.*.id', 'required'],
701+
['users.*.id', 'each', 'required'],
702+
['users.*.id', 'each', 'number', 'min' => 34, 'msg' => [
703+
'min' => 'xxx error',
704+
'number' => 'xxx error',
705+
]],
706+
];
707+
708+
$v = RV::check([
709+
'users' => [
710+
['id' => 12,],
711+
['id' => 23],
712+
],
713+
], $rs);
714+
715+
$this->assertFalse($v->isOk());
716+
$this->assertSame('parameter users.*.id is required!', $v->firstError());
717+
}
718+
694719
/**
695720
* @link https://github.com/inhere/php-validate/issues/21
696721
*/

test/ValidationTraitTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public function testGetByPath(): void
3434
'attr' => [
3535
'wid' => 1
3636
]
37+
],
38+
'users' => [
39+
['id' => 1,],
40+
['id' => 2,]
3741
]
3842
]
3943
]);
@@ -50,6 +54,10 @@ public function testGetByPath(): void
5054
$val = $v->getByPath('prod.*.attr');
5155
$this->assertSame([['wid' => 1]], $val);
5256

57+
// TODO ..
58+
// $val = $v->getByPath('users.*.id');
59+
// $this->assertSame([1, 2], $val);
60+
5361
// $val = $v->getByPath('prod.*.attr.wid');
5462
// $this->assertSame([1], $val);
5563
}

test/ValidatorsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public function testAlphaNum(): void
135135
{
136136
$this->assertFalse(Validators::alphaNum('='));
137137
$this->assertFalse(Validators::alphaNum(null));
138+
$this->assertFalse(Validators::alphaNum(true));
138139
$this->assertFalse(Validators::alphaNum([]));
139140
$this->assertTrue(Validators::alphaNum(5));
140141
$this->assertTrue(Validators::alphaNum('5'));

0 commit comments

Comments
 (0)