Skip to content

Commit

Permalink
Added EndsWith rule
Browse files Browse the repository at this point in the history
  • Loading branch information
rinodrummer committed Feb 21, 2024
1 parent 3a491da commit a670e13
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Validation/Rules/EndsWith.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Tempest\Validation\Rules;

use Attribute;
use Tempest\Interface\Rule;

#[Attribute]
final readonly class EndsWith implements Rule
{
public function __construct(
private string $needle,
) {
}

public function isValid(mixed $value): bool
{
return str_ends_with($value, $this->needle);
}

public function message(): string
{
return "Value should end with {$this->needle}";
}
}
22 changes: 22 additions & 0 deletions tests/Validation/Rules/EndsWithTest.php
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\EndsWith;

class EndsWithTest extends TestCase
{
/** @test */
public function test_ends_with()
{
$rule = new EndsWith(needle: 'ab');

$this->assertTrue($rule->isValid('ab'));
$this->assertTrue($rule->isValid('cab'));
$this->assertFalse($rule->isValid('b'));
$this->assertFalse($rule->isValid('3434'));
}
}

0 comments on commit a670e13

Please sign in to comment.