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.
Merge pull request tempestphp#84 from vsergiu93/feat-add-not-starts-w…
…ith-and-not-ends-with Adds DoesNotStartWith and DoesNotEndWith validation rules
- Loading branch information
Showing
4 changed files
with
131 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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Validation\Rules; | ||
|
||
use Attribute; | ||
use Tempest\Interface\Rule; | ||
|
||
#[Attribute] | ||
class DoesNotEndWith 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 not end 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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Validation\Rules; | ||
|
||
use Attribute; | ||
use Tempest\Interface\Rule; | ||
|
||
#[Attribute] | ||
final readonly class DoesNotStartWith implements Rule | ||
{ | ||
public function __construct( | ||
private string $needle | ||
) { | ||
} | ||
|
||
public function isValid(mixed $value): bool | ||
{ | ||
return ! str_starts_with($value, $this->needle); | ||
} | ||
|
||
public function message(): string | ||
{ | ||
return "Value should not 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,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Tempest\Validation\Rules; | ||
|
||
use Tempest\Validation\Rules\DoesNotEndWith; | ||
use Tests\Tempest\TestCase; | ||
|
||
class DoesNotEndWithTest extends TestCase | ||
{ | ||
/** | ||
* | ||
* @dataProvider dataSets | ||
*/ | ||
public function test_rule(string $needle, string $stringToTest, bool $expected): void | ||
{ | ||
$rule = new DoesNotEndWith($needle); | ||
|
||
$this->assertEquals($expected, $rule->isValid($stringToTest)); | ||
} | ||
|
||
public static function dataSets(): array | ||
{ | ||
return [ | ||
'should return false if it ends with the given string' => ['test', 'this is a test', false], | ||
'should return true if it does not end with the given string' => ['test', 'test this is a', true], | ||
'should return true for an empty string' => ['test', '', true], | ||
'should return true for a single non-matching character' => ['test', 'a', true], | ||
'should return false for a matching character string' => ['a', 'banana', false], | ||
'should return true if it ends with a different string' => [ | ||
'test', | ||
'this is a test best', | ||
true, | ||
], | ||
'should return true if the string is the reverse of the given string' => ['test', 'tset', true], | ||
'should return false if the string and given string are the same' => ['test', 'test', false], | ||
]; | ||
} | ||
} |
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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Tempest\Validation\Rules; | ||
|
||
use Tempest\Validation\Rules\DoesNotStartWith; | ||
use Tests\Tempest\TestCase; | ||
|
||
class DoesNotStartWithTest extends TestCase | ||
{ | ||
/** | ||
* | ||
* @dataProvider dataSets | ||
* | ||
*/ | ||
public function test_rule(string $needle, string $stringToTest, bool $expected): void | ||
{ | ||
$rule = new DoesNotStartWith($needle); | ||
|
||
$this->assertEquals($expected, $rule->isValid($stringToTest)); | ||
} | ||
|
||
public static function dataSets(): array | ||
{ | ||
return [ | ||
'should return false if it starts with the given string' => ['test', 'test this is a test', false], | ||
'should return true if it does not start with the given string' => ['test', 'this is a test', true], | ||
'should return true for an empty string' => ['test', '', true], | ||
'should return true for a single non-matching character' => ['test', 'a', true], | ||
'should return false for a matching character string' => ['a', 'apple', false], | ||
'should return true if it starts with a different string' => ['test', 'best this is a test', true], | ||
'should return true if the string is the reverse of the given string' => ['test', 'tset', true], | ||
'should return false if the string and given string are the same' => ['test', 'test', false], | ||
]; | ||
} | ||
} |