Skip to content

Commit

Permalink
Merge pull request tempestphp#84 from vsergiu93/feat-add-not-starts-w…
Browse files Browse the repository at this point in the history
…ith-and-not-ends-with

Adds  DoesNotStartWith and DoesNotEndWith validation rules
  • Loading branch information
aidan-casey committed Feb 21, 2024
2 parents e482372 + c6a1f52 commit e01de3f
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Validation/Rules/DoesNotEndWith.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]
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}";
}
}
27 changes: 27 additions & 0 deletions src/Validation/Rules/DoesNotStartWith.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 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}";
}
}
40 changes: 40 additions & 0 deletions tests/Validation/Rules/DoesNotEndWithTest.php
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],
];
}
}
37 changes: 37 additions & 0 deletions tests/Validation/Rules/DoesNotStartWithTest.php
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],
];
}
}

0 comments on commit e01de3f

Please sign in to comment.