Skip to content

Commit 2d431c7

Browse files
authored
Merge pull request rakit#49 from rakit/fix-min-max-rules
Fixing min and max rule for string type. This should fixes rakit#48 .
2 parents 402afc8 + cbc8e6b commit 2d431c7

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

src/Rules/Max.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function check($value)
1919
if (is_int($value)) {
2020
return $value <= $max;
2121
} elseif(is_string($value)) {
22-
return strlen($value) <= $max;
22+
return mb_strlen($value, 'UTF-8') <= $max;
2323
} elseif(is_array($value)) {
2424
return count($value) <= $max;
2525
} else {

src/Rules/Min.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function check($value)
1919
if (is_int($value)) {
2020
return $value >= $min;
2121
} elseif(is_string($value)) {
22-
return strlen($value) >= $min;
22+
return mb_strlen($value, 'UTF-8') >= $min;
2323
} elseif(is_array($value)) {
2424
return count($value) >= $min;
2525
} else {

tests/Rules/MaxTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public function testValids()
1515
$this->assertTrue($this->rule->fillParameters([200])->check(123));
1616
$this->assertTrue($this->rule->fillParameters([6])->check('foobar'));
1717
$this->assertTrue($this->rule->fillParameters([3])->check([1,2,3]));
18+
19+
$this->assertTrue($this->rule->fillParameters([3])->check('мин'));
20+
$this->assertTrue($this->rule->fillParameters([4])->check('كلمة'));
21+
$this->assertTrue($this->rule->fillParameters([3])->check('ワード'));
22+
$this->assertTrue($this->rule->fillParameters([1])->check(''));
1823
}
1924

2025
public function testInvalids()

tests/Rules/MinTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ public function testInvalids()
2121
{
2222
$this->assertFalse($this->rule->fillParameters([7])->check('foobar'));
2323
$this->assertFalse($this->rule->fillParameters([4])->check([1,2,3]));
24-
$this->assertFalse($this->rule->fillParameters([200])->check(123));
24+
$this->assertFalse($this->rule->fillParameters([200])->check(123));
25+
26+
$this->assertFalse($this->rule->fillParameters([4])->check('мин'));
27+
$this->assertFalse($this->rule->fillParameters([5])->check('كلمة'));
28+
$this->assertFalse($this->rule->fillParameters([4])->check('ワード'));
29+
$this->assertFalse($this->rule->fillParameters([2])->check(''));
2530
}
2631

2732
}

tests/ValidatorTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,10 +909,16 @@ public function testGetValidData()
909909
'qty' => 'invalid'
910910
]
911911
],
912+
'emails' => [
913+
914+
'something',
915+
916+
],
912917
'thing' => 'exists',
913918
], [
914919
'thing' => 'required',
915920
'items.*.product_id' => 'required|numeric',
921+
'emails.*' => 'required|email',
916922
'items.*.qty' => 'required|numeric',
917923
'something' => 'default:on|required|in:on,off'
918924
]);
@@ -925,6 +931,10 @@ public function testGetValidData()
925931
'product_id' => 1
926932
]
927933
],
934+
'emails' => [
935+
936+
937+
],
928938
'thing' => 'exists',
929939
'something' => 'on'
930940
], $validData);
@@ -939,10 +949,16 @@ public function testGetInvalidData()
939949
'qty' => 'invalid'
940950
]
941951
],
952+
'emails' => [
953+
954+
'something',
955+
956+
],
942957
'thing' => 'exists',
943958
], [
944959
'thing' => 'required',
945960
'items.*.product_id' => 'required|numeric',
961+
'emails.*' => 'required|email',
946962
'items.*.qty' => 'required|numeric',
947963
'something' => 'required|in:on,off'
948964
]);
@@ -955,6 +971,9 @@ public function testGetInvalidData()
955971
'qty' => 'invalid'
956972
]
957973
],
974+
'emails' => [
975+
1 => 'something'
976+
],
958977
'something' => null
959978
], $invalidData);
960979
}

0 commit comments

Comments
 (0)