Skip to content

Commit 5d2bf5b

Browse files
committed
Validator: Add starts_with, ends_with, and contains rule setting to string validator
1 parent 9928ef3 commit 5d2bf5b

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Validator.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,14 +626,29 @@ protected function validateString($value, array $rule = []): bool
626626
{
627627
$valid = is_string($value);
628628

629+
// case-sensitive by default
630+
$caseSensitive = isset($rule['case_sensitive']) ? (bool) $rule['case_sensitive'] : true;
631+
629632
if (isset($rule['equals']) && is_string($rule['equals'])) {
630-
$valid = $valid && $value == $rule['equals'];
633+
$valid = $valid && Str::equals($value, $rule['equals'], $caseSensitive);
631634
} elseif (isset($rule['in']) && is_array($rule['in'])) {
632635
$valid = $valid && in_array($value, $rule['in']);
633636
} elseif (isset($rule['regex']) && is_string($rule['regex'])) {
634637
$valid = $valid && preg_match($rule['regex'], $value);
635638
}
636639

640+
if (isset($rule['starts_with']) && is_string($rule['starts_with'])) {
641+
$valid = $valid && Str::startsWith($value, $rule['starts_with'], $caseSensitive);
642+
}
643+
644+
if (isset($rule['ends_with']) && is_string($rule['ends_with'])) {
645+
$valid = $valid && Str::endsWith($value, $rule['ends_with'], $caseSensitive);
646+
}
647+
648+
if (isset($rule['contains']) && is_string($rule['contains'])) {
649+
$valid = $valid && Str::contains($value, $rule['contains'], $caseSensitive);
650+
}
651+
637652
if (isset($rule['allow_empty']) && $rule['allow_empty'] === false) {
638653
$valid = $valid && !empty($value);
639654
}

tests/ValidatorTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,10 @@ public function testValidateString()
343343
$str_equals = ['equals' => 'Hello, World!'];
344344
$str_in = ['in' => ['hello', 'world', 'foo', 'bar']];
345345
$str_regex = ['regex' => "/php/i"];
346+
$str_starts_with = ['starts_with' => 'Hello'];
347+
$str_ends_with = ['ends_with' => 'World!'];
348+
$str_contains = ['contains' => 'llo, W'];
349+
$str_case_insensitive = ['case_sensitive' => false];
346350

347351
$this->assertTrue(Validator::isString('abc', array_merge($str_min_length, $str_max_length)));
348352
$this->assertFalse(Validator::isString('', $str_min_length));
@@ -352,11 +356,21 @@ public function testValidateString()
352356
$this->assertFalse(Validator::isString(null));
353357
$this->assertTrue(Validator::isString(null, $str_allow_null));
354358
$this->assertTrue(Validator::isString('Hello, World!', $str_equals));
359+
$this->assertTrue(Validator::isString('hello, world!', array_merge($str_case_insensitive, $str_equals)));
355360
$this->assertFalse(Validator::isString('Hello World!', $str_equals));
356361
$this->assertTrue(Validator::isString('foo', $str_in));
357362
$this->assertFalse(Validator::isString('cat', $str_in));
358363
$this->assertTrue(Validator::isString('PHP is my favorite!', $str_regex));
359364
$this->assertFalse(Validator::isString('Java is my favorite!', $str_regex));
365+
$this->assertTrue(Validator::isString('Hello, World!', $str_starts_with));
366+
$this->assertTrue(Validator::isString('hello, world!', array_merge($str_case_insensitive, $str_starts_with)));
367+
$this->assertFalse(Validator::isString('Jello, World!', $str_starts_with));
368+
$this->assertTrue(Validator::isString('Hello, World!', $str_ends_with));
369+
$this->assertTrue(Validator::isString('hello, world!', array_merge($str_case_insensitive, $str_ends_with)));
370+
$this->assertFalse(Validator::isString('Hello, whorld!', $str_ends_with));
371+
$this->assertTrue(Validator::isString('Hello, World!', $str_contains));
372+
$this->assertTrue(Validator::isString('hello, world!', array_merge($str_case_insensitive, $str_contains)));
373+
$this->assertFalse(Validator::isString('Hell0, World!', $str_contains));
360374
}
361375

362376
/**

0 commit comments

Comments
 (0)