From 6ac5ca1dd5840234071cce14c036d9d888eb6760 Mon Sep 17 00:00:00 2001 From: Muhammad Syifa Date: Mon, 6 Aug 2018 14:17:02 +0700 Subject: [PATCH 1/2] Add method to make In and NotIn to be strict --- src/Rules/In.php | 9 ++++++++- src/Rules/NotIn.php | 9 ++++++++- tests/Rules/InTest.php | 12 ++++++++++++ tests/Rules/NotInTest.php | 12 ++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/Rules/In.php b/src/Rules/In.php index 92b571f..3845edb 100644 --- a/src/Rules/In.php +++ b/src/Rules/In.php @@ -9,6 +9,8 @@ class In extends Rule protected $message = "The :attribute is not allowing :value"; + protected $strict = false; + public function fillParameters(array $params) { if (count($params) == 1 AND is_array($params[0])) { @@ -18,12 +20,17 @@ public function fillParameters(array $params) return $this; } + public function strict($strict = true) + { + $this->strict = $strict; + } + public function check($value) { $this->requireParameters(['allowed_values']); $allowed_values = $this->parameter('allowed_values'); - return in_array($value, $allowed_values); + return in_array($value, $allowed_values, $this->strict); } } diff --git a/src/Rules/NotIn.php b/src/Rules/NotIn.php index 99d8136..8abbb24 100644 --- a/src/Rules/NotIn.php +++ b/src/Rules/NotIn.php @@ -9,6 +9,8 @@ class NotIn extends Rule protected $message = "The :attribute is not allowing :value"; + protected $strict = false; + public function fillParameters(array $params) { if (count($params) == 1 AND is_array($params[0])) { @@ -18,11 +20,16 @@ public function fillParameters(array $params) return $this; } + public function strict($strict = true) + { + $this->strict = $strict; + } + public function check($value) { $this->requireParameters(['disallowed_values']); $disallowed_values = (array) $this->parameter('disallowed_values'); - return !in_array($value, $disallowed_values); + return !in_array($value, $disallowed_values, $this->strict); } } diff --git a/tests/Rules/InTest.php b/tests/Rules/InTest.php index 78e1242..f0d1169 100644 --- a/tests/Rules/InTest.php +++ b/tests/Rules/InTest.php @@ -21,4 +21,16 @@ public function testInvalids() $this->assertFalse($this->rule->fillParameters([1,2,3])->check(4)); } + public function testStricts() + { + // Not strict + $this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check(1)); + $this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check(true)); + + // Strict + $this->rule->strict(); + $this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check(1)); + $this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check(1)); + } + } diff --git a/tests/Rules/NotInTest.php b/tests/Rules/NotInTest.php index bb02c99..403c860 100644 --- a/tests/Rules/NotInTest.php +++ b/tests/Rules/NotInTest.php @@ -21,4 +21,16 @@ public function testInvalids() $this->assertFalse($this->rule->fillParameters(['bar', 'baz', 'qux'])->check('bar')); } + public function testStricts() + { + // Not strict + $this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check(1)); + $this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check(true)); + + // Strict + $this->rule->strict(); + $this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check(1)); + $this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check(1)); + } + } From 7dd06aa741c9d40e2a9b2e600a559116d92ce76c Mon Sep 17 00:00:00 2001 From: Muhammad Syifa Date: Mon, 6 Aug 2018 14:26:45 +0700 Subject: [PATCH 2/2] Add readme to enable strict checking for rules 'in' and 'not_in' --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 78d17f2..65f2121 100644 --- a/README.md +++ b/README.md @@ -417,11 +417,29 @@ The field under this rule may have alpha-numeric characters, as well as dashes a The field under this rule must be included in the given list of values. +This rule is using `in_array` to check the value. +By default `in_array` disable strict checking. +So it doesn't check data type. +If you want enable strict checking, you can invoke validator like this: + +```php +$validation = $validator->validate($data, [ + 'enabled' => [ + 'required', + $validator('in', [true, 1])->strict() + ] +]); +``` + +Then 'enabled' value should be boolean `true`, or int `1`. + #### not_in:value_1,value_2,... The field under this rule must not be included in the given list of values. +This rule also using `in_array`. You can enable strict checking by invoking validator and call `strict()` like example in rule `in` above. + #### min:number