forked from tempestphp/tempest-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Validation\Rules; | ||
|
||
use Attribute; | ||
use Tempest\Interface\Rule; | ||
|
||
#[Attribute] | ||
final readonly class StartsWith implements Rule | ||
{ | ||
public function __construct( | ||
private string $needle, | ||
) { | ||
} | ||
|
||
public function isValid(mixed $value): bool | ||
{ | ||
if (str_starts_with($value, $this->needle)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function message(): string | ||
{ | ||
return "Value should start with {$this->needle}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Tempest\Validation\Rules; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Tempest\Validation\Rules\StartsWith; | ||
|
||
class StartsWithTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function test_starts_with() | ||
{ | ||
$rule = new StartsWith(needle: 'ab'); | ||
|
||
$this->assertTrue($rule->isValid('ab')); | ||
$this->assertTrue($rule->isValid('abc')); | ||
$this->assertFalse($rule->isValid('a')); | ||
$this->assertFalse($rule->isValid('3434')); | ||
} | ||
} |