Skip to content

Commit a7c42cd

Browse files
committed
fix: TypeError in less_than and less_than_equal_to
1 parent 381a445 commit a7c42cd

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

system/Validation/StrictRules/Rules.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,28 @@ public function is_unique($str, string $field, array $data): bool
149149
/**
150150
* Less than
151151
*
152-
* @param mixed $str
152+
* @param mixed $str expects int|string
153153
*/
154154
public function less_than($str, string $max): bool
155155
{
156+
if (is_int($str)) {
157+
$str = (string) $str;
158+
}
159+
156160
return $this->nonStrictRules->less_than($str, $max);
157161
}
158162

159163
/**
160164
* Equal to or Less than
161165
*
162-
* @param mixed $str
166+
* @param mixed $str expects int|string
163167
*/
164168
public function less_than_equal_to($str, string $max): bool
165169
{
170+
if (is_int($str)) {
171+
$str = (string) $str;
172+
}
173+
166174
return $this->nonStrictRules->less_than_equal_to($str, $max);
167175
}
168176

tests/system/Validation/StrictRules/RulesTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,50 @@ public function provideGreaterThanStrict(): Generator
129129
[10, 'a', false],
130130
];
131131
}
132+
133+
/**
134+
* @dataProvider provideLessThanStrict
135+
*
136+
* @param int $value
137+
*/
138+
public function testLessThanStrict($value, string $param, bool $expected): void
139+
{
140+
$this->validation->setRules(['foo' => "less_than[{$param}]"]);
141+
142+
$data = ['foo' => $value];
143+
$this->assertSame($expected, $this->validation->run($data));
144+
}
145+
146+
public function provideLessThanStrict(): Generator
147+
{
148+
yield from [
149+
[-10, '-11', false],
150+
[9, '10', true],
151+
[10, '9', false],
152+
[10, '10', false],
153+
[10, 'a', true],
154+
];
155+
}
156+
157+
/**
158+
* @dataProvider provideLessThanEqualStrict
159+
*
160+
* @param int $value
161+
*/
162+
public function testLessEqualThanStrict($value, ?string $param, bool $expected): void
163+
{
164+
$this->validation->setRules(['foo' => "less_than_equal_to[{$param}]"]);
165+
166+
$data = ['foo' => $value];
167+
$this->assertSame($expected, $this->validation->run($data));
168+
}
169+
170+
public function provideLessThanEqualStrict(): Generator
171+
{
172+
yield from [
173+
[0, '0', true],
174+
[1, '0', false],
175+
[-1, '0', true],
176+
];
177+
}
132178
}

0 commit comments

Comments
 (0)