Skip to content

Commit

Permalink
Merge pull request tempestphp#69 from ludoguenet/main
Browse files Browse the repository at this point in the history
Adds Alpha and AlphaNumeric rules.
  • Loading branch information
aidan-casey committed Feb 20, 2024
2 parents 98c5f2a + 4c261ed commit 5ebe834
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/Application/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ private function initDiscovery(Container $container): void
if ($this->appConfig->discoveryCache && $discovery->hasCache()) {
$discovery->restoreCache($container);
next($this->appConfig->discoveryClasses);

continue;
}

Expand Down
1 change: 0 additions & 1 deletion src/Discovery/DiscoveryDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public function discover(ReflectionClass $class): void
$this->appConfig->discoveryClasses[] = $class->getName();
}


public function hasCache(): bool
{
return file_exists(self::CACHE_PATH);
Expand Down
22 changes: 22 additions & 0 deletions src/Validation/Rules/Alpha.php
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';
}
}
22 changes: 22 additions & 0 deletions src/Validation/Rules/AlphaNumeric.php
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';
}
}
3 changes: 1 addition & 2 deletions src/Validation/Rules/ShouldBeFalse.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#[Attribute]
final readonly class ShouldBeFalse implements Rule
{

public function isValid(mixed $value): bool
{
return $value === false || $value === 'false' || $value === 0 || $value === '0';
Expand All @@ -20,4 +19,4 @@ public function message(): string
{
return 'Value should represent a boolean false value.';
}
}
}
3 changes: 1 addition & 2 deletions src/Validation/Rules/ShouldBeTrue.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#[Attribute]
final readonly class ShouldBeTrue implements Rule
{

public function isValid(mixed $value): bool
{
return $value === true || $value === 'true' || $value === 1 || $value === '1';
Expand All @@ -20,4 +19,4 @@ public function message(): string
{
return 'Value should represent a boolean true value.';
}
}
}
21 changes: 21 additions & 0 deletions tests/Validation/Rules/AlphaNumericTest.php
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'));
}
}
21 changes: 21 additions & 0 deletions tests/Validation/Rules/AlphaTest.php
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'));
}
}
1 change: 0 additions & 1 deletion tests/Validation/Rules/ShouldBeFalseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PHPUnit\Framework\TestCase;
use Tempest\Validation\Rules\ShouldBeFalse;
use Tempest\Validation\Rules\ShouldBeTrue;

class ShouldBeFalseTest extends TestCase
{
Expand Down
4 changes: 3 additions & 1 deletion tests/bootstrap.php
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);

0 comments on commit 5ebe834

Please sign in to comment.