Skip to content

Commit 6ac5ca1

Browse files
committed
Add method to make In and NotIn to be strict
1 parent 16443e3 commit 6ac5ca1

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

src/Rules/In.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class In extends Rule
99

1010
protected $message = "The :attribute is not allowing :value";
1111

12+
protected $strict = false;
13+
1214
public function fillParameters(array $params)
1315
{
1416
if (count($params) == 1 AND is_array($params[0])) {
@@ -18,12 +20,17 @@ public function fillParameters(array $params)
1820
return $this;
1921
}
2022

23+
public function strict($strict = true)
24+
{
25+
$this->strict = $strict;
26+
}
27+
2128
public function check($value)
2229
{
2330
$this->requireParameters(['allowed_values']);
2431

2532
$allowed_values = $this->parameter('allowed_values');
26-
return in_array($value, $allowed_values);
33+
return in_array($value, $allowed_values, $this->strict);
2734
}
2835

2936
}

src/Rules/NotIn.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class NotIn extends Rule
99

1010
protected $message = "The :attribute is not allowing :value";
1111

12+
protected $strict = false;
13+
1214
public function fillParameters(array $params)
1315
{
1416
if (count($params) == 1 AND is_array($params[0])) {
@@ -18,11 +20,16 @@ public function fillParameters(array $params)
1820
return $this;
1921
}
2022

23+
public function strict($strict = true)
24+
{
25+
$this->strict = $strict;
26+
}
27+
2128
public function check($value)
2229
{
2330
$this->requireParameters(['disallowed_values']);
2431
$disallowed_values = (array) $this->parameter('disallowed_values');
25-
return !in_array($value, $disallowed_values);
32+
return !in_array($value, $disallowed_values, $this->strict);
2633
}
2734

2835
}

tests/Rules/InTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,16 @@ public function testInvalids()
2121
$this->assertFalse($this->rule->fillParameters([1,2,3])->check(4));
2222
}
2323

24+
public function testStricts()
25+
{
26+
// Not strict
27+
$this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check(1));
28+
$this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check(true));
29+
30+
// Strict
31+
$this->rule->strict();
32+
$this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check(1));
33+
$this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check(1));
34+
}
35+
2436
}

tests/Rules/NotInTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,16 @@ public function testInvalids()
2121
$this->assertFalse($this->rule->fillParameters(['bar', 'baz', 'qux'])->check('bar'));
2222
}
2323

24+
public function testStricts()
25+
{
26+
// Not strict
27+
$this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check(1));
28+
$this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check(true));
29+
30+
// Strict
31+
$this->rule->strict();
32+
$this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check(1));
33+
$this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check(1));
34+
}
35+
2436
}

0 commit comments

Comments
 (0)