Skip to content

Commit ab43ddb

Browse files
authored
Merge pull request #21 from vdhicts/feature/contains-any
Add contains any validation rule
2 parents 507fe4a + ca0202b commit ab43ddb

File tree

6 files changed

+77
-3
lines changed

6 files changed

+77
-3
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ Validates if the value contains a certain phrase.
4646
'field' => [new Contains($needle)],
4747
```
4848

49+
### ContainsAny
50+
51+
Validates if the value contains any of the provided phrases.
52+
53+
```php
54+
'field' => [new ContainsAny(['foo', 'bar'])],
55+
```
56+
4957
### DateAfterOrEqual
5058

5159
Validates if the value is a date after or equals the provided date (Carbon).

resources/lang/en/validationRules.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
return [
44
'bic_number' => 'The :attribute must be a valid BIC number',
55
'contains' => 'The :attribute must contain `%s`',
6+
'contains_any' => 'The :attribute must contain one of these values: `%s`',
67
'date_after_or_equal' => 'The :attribute must be after or equal to `%s`',
78
'date_before_or_equal' => 'The :attribute must be before or equal to `%s`',
89
'date_has_specific_minutes' => 'The :attribute must have one of :minutes minutes',

resources/lang/nl/validationRules.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
return [
44
'bic_number' => 'Het veld :attribute moet een geldig BIC nummer bevatten',
5-
'contains' => 'Het veld :attribute moet `%s` bevatten',
5+
'contains_any' => 'Het veld :attribute moet een van deze waardes bevatten: `%s`',
66
'date_after_or_equal' => 'Het veld :attribute moet na of op `%s` zijn',
77
'date_before_or_equal' => 'Het veld :attribute moet voor of op `%s` zijn',
88
'date_has_specific_minutes' => 'Het veld :attribute moet ingesteld zijn op een van :minutes minuten',

src/Rules/ContainsAny.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Vdhicts\ValidationRules\Rules;
4+
5+
use Illuminate\Contracts\Validation\Rule;
6+
use Illuminate\Support\Str;
7+
8+
class ContainsAny implements Rule
9+
{
10+
private array $needles;
11+
12+
public function __construct(array $needles)
13+
{
14+
$this->needles = $needles;
15+
}
16+
17+
/**
18+
* Determine if the validation rule passes.
19+
*
20+
* @param string $attribute
21+
* @param mixed $value
22+
* @return bool
23+
*/
24+
public function passes($attribute, $value): bool
25+
{
26+
return Str::contains($value, $this->needles);
27+
}
28+
29+
/**
30+
* Get the validation error message.
31+
*
32+
* @return string
33+
*/
34+
public function message(): string
35+
{
36+
return sprintf(
37+
__('validationRules.contains_any'),
38+
implode(', ', $this->needles)
39+
);
40+
}
41+
}

src/Rules/DateHasSpecificMinutes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Vdhicts\ValidationRules\Rules;
44

5-
use Carbon\Exceptions\InvalidFormatException;
5+
use Exception;
66
use Illuminate\Contracts\Validation\Rule;
77
use Illuminate\Support\Carbon;
88

@@ -28,7 +28,7 @@ public function passes($attribute, $value): bool
2828
{
2929
try {
3030
$date = Carbon::createFromFormat($this->format, $value);
31-
} catch (InvalidFormatException $exception) {
31+
} catch (Exception $exception) {
3232
return false;
3333
}
3434

tests/Rules/ContainsAnyTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Vdhicts\ValidationRules\Tests\Rules;
4+
5+
use Vdhicts\ValidationRules\Tests\TestCase;
6+
use Vdhicts\ValidationRules\Rules\ContainsAny;
7+
8+
class ContainsAnyTest extends TestCase
9+
{
10+
public function testRulePasses()
11+
{
12+
$rule = new ContainsAny(['foo', 'bar']);
13+
$this->assertTrue($rule->passes('', 'this is a test to see if foo or bar is in the text'));
14+
}
15+
16+
public function testRuleFails()
17+
{
18+
$rule = new ContainsAny(['test']);
19+
$this->assertFalse($rule->passes('', 'this is a fail'));
20+
21+
$rule = new ContainsAny(['']);
22+
$this->assertFalse($rule->passes('', 'this is a test'));
23+
}
24+
}

0 commit comments

Comments
 (0)