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#69 from ludoguenet/main
Adds Alpha and AlphaNumeric rules.
- Loading branch information
Showing
10 changed files
with
92 additions
and
7 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
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
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 Tempest\Validation\Rules; | ||
|
||
use Attribute; | ||
use Tempest\Interface\Rule; | ||
|
||
#[Attribute] | ||
final readonly class Alpha implements Rule | ||
{ | ||
public function isValid(mixed $value): bool | ||
{ | ||
return boolval(preg_match('/^[A-Za-z]+$/', $value)); | ||
} | ||
|
||
public function message(): string | ||
{ | ||
return 'Value should only contain alphabetic characters'; | ||
} | ||
} |
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 Tempest\Validation\Rules; | ||
|
||
use Attribute; | ||
use Tempest\Interface\Rule; | ||
|
||
#[Attribute] | ||
final readonly class AlphaNumeric implements Rule | ||
{ | ||
public function isValid(mixed $value): bool | ||
{ | ||
return boolval(preg_match('/^[A-Za-z0-9]+$/', $value)); | ||
} | ||
|
||
public function message(): string | ||
{ | ||
return 'Value should only contain alphanumeric characters'; | ||
} | ||
} |
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
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
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Tempest\Validation\Rules; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Tempest\Validation\Rules\AlphaNumeric; | ||
|
||
class AlphaNumericTest extends TestCase | ||
{ | ||
public function test_alphanumeric() | ||
{ | ||
$rule = new AlphaNumeric(); | ||
|
||
$this->assertSame('Value should only contain alphanumeric characters', $rule->message()); | ||
$this->assertFalse($rule->isValid('string_123')); | ||
$this->assertTrue($rule->isValid('string123')); | ||
$this->assertTrue($rule->isValid('STRING123')); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Tempest\Validation\Rules; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Tempest\Validation\Rules\Alpha; | ||
|
||
class AlphaTest extends TestCase | ||
{ | ||
public function test_alpha() | ||
{ | ||
$rule = new Alpha(); | ||
|
||
$this->assertSame('Value should only contain alphabetic characters', $rule->message()); | ||
$this->assertFalse($rule->isValid('string123')); | ||
$this->assertTrue($rule->isValid('string')); | ||
$this->assertTrue($rule->isValid('STRING')); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Tempest\Discovery\DiscoveryDiscovery; | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
@unlink(DiscoveryDiscovery::CACHE_PATH); | ||
@unlink(DiscoveryDiscovery::CACHE_PATH); |