diff --git a/src/Factory.php b/src/Factory.php index b94d48c..c8777a1 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -87,6 +87,10 @@ protected function registerDefaultRules(): void 'prohibited' => new Rules\Prohibited, 'prohibited_if' => new Rules\ProhibitedIf, 'prohibited_unless' => new Rules\ProhibitedUnless, + 'prohibited_with' => new Rules\ProhibitedWith, + 'prohibited_with_all' => new Rules\ProhibitedWithAll, + 'prohibited_without' => new Rules\ProhibitedWithout, + 'prohibited_without_all' => new Rules\ProhibitedWithoutAll, 'regex' => new Rules\Regex, 'rejected' => new Rules\Rejected, 'required' => new Rules\Required, diff --git a/src/Resources/i18n/de.php b/src/Resources/i18n/de.php index ea8d352..cf373c6 100644 --- a/src/Resources/i18n/de.php +++ b/src/Resources/i18n/de.php @@ -46,6 +46,10 @@ 'rule.prohibited' => ':attribute ist nicht erlaubt', 'rule.prohibited_if' => ':attribute ist nicht erlaubt, wenn :field einen der folgenden Werte entspricht: :values', 'rule.prohibited_unless' => ':attribute ist nicht erlaubt, wenn :field nicht einem der folgenden Werte entspricht: :values', +// 'rule.prohibited_with' => ':attribute ist nicht erlaubt mit: :fields', +// 'rule.prohibited_with_all' => ':attribute ist nicht mit allen :fields erlaubt', +// 'rule.prohibited_without' => ':attribute ist verboten, wenn alle der folgenden Felder vorhanden sind: :fields', +// 'rule.prohibited_without_all' => ' :attribute ist verboten, wenn eines der folgenden Felder nicht vorhanden ist: :fields', 'rule.regex' => ':attribute entspricht nicht dem erwarteten Format', 'rule.rejected' => ':attribute muss einen der folgenden Werte enthalten: :rejected', 'rule.required' => ':attribute ist erforderlich', diff --git a/src/Resources/i18n/en.php b/src/Resources/i18n/en.php index 483159c..b90063e 100644 --- a/src/Resources/i18n/en.php +++ b/src/Resources/i18n/en.php @@ -46,6 +46,10 @@ 'rule.prohibited' => ':attribute is not allowed', 'rule.prohibited_if' => ':attribute is not allowed if :field has value(s) :values', 'rule.prohibited_unless' => ':attribute is not allowed if :field does not have value(s) :values', + 'rule.prohibited_with' => ':attribute is not allowed with :fields', + 'rule.prohibited_with_all' => ':attribute is not allowed with all of :fields', + 'rule.prohibited_without' => ':attribute is not allowed when one of the following fields is absent: :fields', + 'rule.prohibited_without_all' => ':attribute is not allowed when all of the following fields are absents: :fields', 'rule.regex' => ':attribute does not meet required format', 'rule.rejected' => ':attribute must be one of: :rejected', 'rule.required' => ':attribute is required', diff --git a/src/Resources/i18n/fr.php b/src/Resources/i18n/fr.php index 655474b..faa2cce 100644 --- a/src/Resources/i18n/fr.php +++ b/src/Resources/i18n/fr.php @@ -46,12 +46,16 @@ 'rule.prohibited' => 'Le champ :attribute est interdit.', 'rule.prohibited_if' => 'Le champ :attribute est interdit quand :field a la/les valeur(s) :values', 'rule.prohibited_unless' => 'Le champ :attribute est interdit à moins que :field est l\'une des valeurs :values', + 'rule.prohibited_with' => 'Le champ :attribute est interdit l\'un des champs suivants est présent : {fields}.', + 'rule.prohibited_with_all' => 'Le champ :attribute est interdit quand tous les champs suivants sont présents : :fields.', + 'rule.prohibited_without' => 'Le champ :attribute est interdit quand l\'un des champs suivants est absent : :fields.', + 'rule.prohibited_without_all' => 'Le champ :attribute est interdit quand tous les champs suivants sont absents : :fields.', 'rule.regex' => 'Le format du champ :attribute est invalide.', 'rule.rejected' => 'Le champ :attribute doit être parmi: :rejected', 'rule.required' => 'Le champ :attribute est obligatoire.', 'rule.required_if' => 'Le champ :attribute est obligatoire quand la valeur de :field est :values.', 'rule.required_unless' => 'Le champ :attribute est obligatoire sauf si :fields est :values.', - 'rule.required_with' => 'Le champ :attribute est obligatoire quand :fields est présent.', + 'rule.required_with' => 'Le champ :attribute est obligatoire quand l\'un des champs suivants est présent: :fields.', 'rule.required_with_all' => 'Le champ :attribute est obligatoire quand :fields sont présents.', 'rule.required_without' => 'Le champ :attribute est obligatoire quand :fields n\'est pas présent.', 'rule.required_without_all' => 'Le champ :attribute est requis quand aucun de :fields n\'est présent.', diff --git a/src/Resources/i18n/tr.php b/src/Resources/i18n/tr.php index eb47095..e961531 100644 --- a/src/Resources/i18n/tr.php +++ b/src/Resources/i18n/tr.php @@ -48,6 +48,10 @@ 'rule.prohibited' => ':attribute izin verilmez', 'rule.prohibited_if' => ':attribute izin verilmez eğer :field değeri :values ise', 'rule.prohibited_unless' => ':attribute izin verilmez eğer :field değeri :values içermiyorsa', +// 'rule.prohibited_with' => ':attribute :fields ile izin verilmez', +// 'rule.prohibited_with_all' => ':attribute :fields\'ın tümü ile izin verilmez', +// 'rule.prohibited_with_all' => 'Aşağıdaki tüm alanlar mevcut olduğunda :attribute alanı yasaktır: :fields', +// 'rule.prohibited_with_all' => 'Aşağıdaki alanların tümü olmadığında :attribute alanı yasaktır: :fields', 'rule.regex' => ':attribute gerekli formata uymaz', 'rule.rejected' => ':attribute şunlardan biri olmalıdır: :rejected', 'rule.required' => ':attribute zorunludur', diff --git a/src/Resources/i18n/zh.php b/src/Resources/i18n/zh.php index b2a84bf..4ed2fa7 100644 --- a/src/Resources/i18n/zh.php +++ b/src/Resources/i18n/zh.php @@ -46,6 +46,10 @@ 'rule.prohibited' => ':attribute 禁止使用', 'rule.prohibited_if' => '如果 :field 字段存在以下值::values,:attribute 禁止使用', 'rule.prohibited_unless' => '如果 :field 字段不存在以下值::values,:attribute 禁止使用', +// 'rule.prohibited_with' => '如果存在下列欄位之一,則禁止使用 :attribute 欄位:{fields}.', +// 'rule.prohibited_with_all' => '當以下所有欄位都存在時,禁止使用 :attribute 欄位 : :fields.', +// 'rule.prohibited_without' => '當下列欄位之一不存在時,禁止使用 :attribute 欄位 : :fields.', +// 'rule.prohibited_without_all' => '當以下所有欄位都不存在時,禁止使用 :attribute 欄位 : :fields.', 'rule.regex' => ':attribute 格式不正确', 'rule.rejected' => ':attribute 必须是以下值: :rejected', 'rule.required' => ':attribute 必须存在', diff --git a/src/Rules/In.php b/src/Rules/In.php index 64e176b..263990f 100644 --- a/src/Rules/In.php +++ b/src/Rules/In.php @@ -44,6 +44,16 @@ public function check(mixed $value): bool { $this->assertHasRequiredParameters(['allowed_values']); + if (is_array($value)) { + foreach ($value as $v) { + if (!in_array($v, $this->parameter('allowed_values'), $this->strict)) { + return false; + } + } + + return true; + } + return in_array($value, $this->parameter('allowed_values'), $this->strict); } } diff --git a/src/Rules/NotIn.php b/src/Rules/NotIn.php index fa552c5..9946d23 100644 --- a/src/Rules/NotIn.php +++ b/src/Rules/NotIn.php @@ -45,6 +45,16 @@ public function check(mixed $value): bool { $this->assertHasRequiredParameters(['disallowed_values']); + if (is_array($value)) { + foreach ($value as $v) { + if (in_array($v, (array)$this->parameter('disallowed_values'), $this->strict)) { + return false; + } + } + + return true; + } + return !in_array($value, (array)$this->parameter('disallowed_values'), $this->strict); } } diff --git a/src/Rules/ProhibitedWith.php b/src/Rules/ProhibitedWith.php new file mode 100644 index 0000000..f9be797 --- /dev/null +++ b/src/Rules/ProhibitedWith.php @@ -0,0 +1,44 @@ +params['fields'] = $params; + + return $this; + } + + public function check(mixed $value): bool + { + $this->assertHasRequiredParameters(['fields']); + + $fields = $this->parameter('fields'); + $requiredValidator = $this->validation->factory()->rule('required'); + + if (!$requiredValidator->check($this->attribute()->value())) { + return true; + } + + foreach ($fields as $field) { + if ($requiredValidator->check($this->attribute->value($field))) { + return false; + } + } + + return $requiredValidator->check($value); + } +} diff --git a/src/Rules/ProhibitedWithAll.php b/src/Rules/ProhibitedWithAll.php new file mode 100644 index 0000000..5e98948 --- /dev/null +++ b/src/Rules/ProhibitedWithAll.php @@ -0,0 +1,47 @@ +params['fields'] = $params; + + return $this; + } + + public function check(mixed $value): bool + { + $this->assertHasRequiredParameters(['fields']); + + $fields = $this->parameter('fields'); + $requiredValidator = $this->validation->factory()->rule('required'); + + if (!$requiredValidator->check($this->attribute->value())) { + return true; + } + + $fieldsHaveValues = true; + foreach ($fields as $field) { + $fieldsHaveValues = $fieldsHaveValues && $requiredValidator->check($this->attribute->value($field)); + } + + if (!$fieldsHaveValues) { + return true; + } + + return false; + } +} \ No newline at end of file diff --git a/src/Rules/ProhibitedWithout.php b/src/Rules/ProhibitedWithout.php new file mode 100644 index 0000000..ba57f93 --- /dev/null +++ b/src/Rules/ProhibitedWithout.php @@ -0,0 +1,43 @@ +params['fields'] = $params; + + return $this; + } + + public function check(mixed $value): bool + { + $this->assertHasRequiredParameters(['fields']); + + $fields = $this->parameter('fields'); + $requiredValidator = $this->validation->factory()->rule('required'); + + $fieldsHaveValues = true; + foreach ($fields as $field) { + $fieldsHaveValues = $fieldsHaveValues && $requiredValidator->check($this->attribute->value($field)); + } + + if (!$fieldsHaveValues) { + return false; + } + + return $requiredValidator->check($value); + } +} diff --git a/src/Rules/ProhibitedWithoutAll.php b/src/Rules/ProhibitedWithoutAll.php new file mode 100644 index 0000000..f51fab1 --- /dev/null +++ b/src/Rules/ProhibitedWithoutAll.php @@ -0,0 +1,51 @@ +params['fields'] = $params; + + return $this; + } + + public function check(mixed $value): bool + { + if (!isset($value)) { + return true; + } + + $this->assertHasRequiredParameters(['fields']); + + $fields = $this->parameter('fields'); + $requiredValidator = $this->validation->factory()->rule('required'); + + if (!$requiredValidator->check($value)) { + return true; + } + + $fieldsHaveValues = false; + foreach ($fields as $field) { + $fieldsHaveValues = $fieldsHaveValues || $requiredValidator->check($this->attribute->value($field)); + } + + if ($fieldsHaveValues) { + return true; + } + + return false; + } +} diff --git a/tests/Rules/InTest.php b/tests/Rules/InTest.php index fcfcd3d..7cecb47 100644 --- a/tests/Rules/InTest.php +++ b/tests/Rules/InTest.php @@ -20,11 +20,13 @@ public function testValids() { $this->assertTrue($this->rule->fillParameters([1,2,3])->check(1)); $this->assertTrue($this->rule->fillParameters(['1', 'bar', '3'])->check('bar')); + $this->assertTrue($this->rule->fillParameters(['1', 'bar', '3'])->check(['bar', '3'])); } public function testInvalids() { $this->assertFalse($this->rule->fillParameters([1,2,3])->check(4)); + $this->assertFalse($this->rule->fillParameters([1,2,3])->check([3,4])); } public function testStricts() @@ -32,11 +34,12 @@ 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)); + $this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check([true, 3])); // Strict $this->rule->strict(); $this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check(1)); - $this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check(1)); + $this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check([1,3])); } public function testWithCommasInStrings() diff --git a/tests/Rules/NotInTest.php b/tests/Rules/NotInTest.php index e38daf5..f094154 100644 --- a/tests/Rules/NotInTest.php +++ b/tests/Rules/NotInTest.php @@ -19,11 +19,13 @@ public function testValids() { $this->assertTrue($this->rule->fillParameters(['2', '3', '4'])->check('1')); $this->assertTrue($this->rule->fillParameters([1, 2, 3])->check(5)); + $this->assertTrue($this->rule->fillParameters([1, 2, 3])->check([5,10])); } public function testInvalids() { $this->assertFalse($this->rule->fillParameters(['bar', 'baz', 'qux'])->check('bar')); + $this->assertFalse($this->rule->fillParameters(['bar', 'baz', 'qux'])->check(['bar','foo'])); } public function testStricts() @@ -31,10 +33,11 @@ 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)); + $this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check([true, 3, 9])); // Strict $this->rule->strict(); $this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check(1)); - $this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check(1)); + $this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check([1,3])); } } diff --git a/tests/Rules/ProhibitedTest.php b/tests/Rules/ProhibitedTest.php index d525650..0a57acf 100644 --- a/tests/Rules/ProhibitedTest.php +++ b/tests/Rules/ProhibitedTest.php @@ -163,4 +163,104 @@ public function testProhibitedUnless() $this->assertFalse($res->passes()); $this->assertEquals('bar is not allowed if foo does not have value(s) "bar", "baz", "foo"', $res->errors()->get('bar')['prohibited_unless']); } + + public function testProhibitedWithRule() + { + $validator = new Factory(); + + $v1 = $validator->validate([ + 'b' => '', + ], [ + 'b' => 'prohibited_with:a' + ]); + + $this->assertTrue($v1->passes()); + + $v2 = $validator->validate([ + 'a' => '1', + 'b' => 'test', + ], [ + 'b' => 'prohibited_with:a' + ]); + + $this->assertFalse($v2->passes()); + } + + public function testProhibitedWithoutRule() + { + $validator = new Factory(); + + $v1 = $validator->validate([ + 'b' => 'test', + ], [ + 'b' => 'prohibited_without:a' + ]); + + $this->assertFalse($v1->passes()); + + $v2 = $validator->validate([ + 'a' => '1', + 'b' => 'test', + ], [ + 'b' => 'prohibited_without:a' + ]); + + $this->assertTrue($v2->passes()); + } + + public function testProhibitedWithAllRule() + { + $validator = new Factory(); + + $v1 = $validator->validate([ + 'b' => '', + 'a' => '1' + ], [ + 'b' => 'prohibited_with_all:a,c' + ]); + + $this->assertTrue($v1->passes()); + + $v2 = $validator->validate([ + 'a' => '1', + 'b' => 'test', + 'c' => '2' + ], [ + 'b' => 'prohibited_with_all:a,c' + ]); + + $this->assertFalse($v2->passes()); + } + + public function testProhibitedWithoutAllRule() + { + $validator = new Factory(); + + $v1 = $validator->validate([ + 'b' => 'foo', + 'a' => null, + 'c' => null, + ], [ + 'b' => 'prohibited_without_all:a,c' + ]); + + $this->assertFalse($v1->passes()); + + $v1 = $validator->validate([ + 'b' => '', + 'a' => '1' + ], [ + 'b' => 'prohibited_without_all:a,c' + ]); + + $this->assertTrue($v1->passes()); + + $v2 = $validator->validate([ + 'b' => 'test', + ], [ + 'b' => 'prohibited_without_all:a,c' + ]); + + $this->assertFalse($v2->passes()); + } }