Skip to content
This repository was archived by the owner on Sep 13, 2024. It is now read-only.

Commit 165447d

Browse files
committed
Add more rules and tests
1 parent cbab31e commit 165447d

File tree

6 files changed

+67
-7
lines changed

6 files changed

+67
-7
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ $ composer require saritasa/laravel-fluid-validation
1313
**Example**:
1414
```php
1515
$rules = [
16-
'id' => Rule::int()->required(),
17-
'name' => Rule::string()->required()->minLength(3),
18-
'email' => Rule::string()->required()->email()
16+
'id' => Rule::int()->required()->toString(),
17+
'name' => Rule::string()->required()->minLength(3)->toString(),
18+
'email' => Rule::string()->required()->email()->toArray()
1919
]
2020
```
2121

src/RuleSet.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ public function required()
2020
return $this->appendIfNotExists('required');
2121
}
2222

23+
public function requiredWith(string $otherField)
24+
{
25+
return $this->appendIfNotExists("required_with:$otherField");
26+
}
27+
28+
public function requiredWithout(string $otherField)
29+
{
30+
return $this->appendIfNotExists("required_without:$otherField");
31+
}
32+
2333
protected function appendIfNotExists(string $rule)
2434
{
2535
if (in_array($rule, $this->rules)) {

src/StringRuleSet.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ public function email(): StringRuleSet
1414
return $this->appendIfNotExists('email');
1515
}
1616

17-
public function regex(string $pattern){
17+
public function regex(string $pattern, bool $ignoreCase = false): StringRuleSet
18+
{
19+
if (!starts_with($pattern, '/')) {
20+
$pattern = "/$pattern/";
21+
}
22+
if ($ignoreCase && !ends_with('i', $pattern)) {
23+
$pattern .= 'i';
24+
}
1825
return $this->appendIfNotExists("regex:$pattern");
1926
}
2027
}

tests/IntRulesTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Saritasa\Laravel\Validation\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Saritasa\Laravel\Validation\Rule;
7+
8+
class IntRulesTest extends TestCase
9+
{
10+
function testMinMax()
11+
{
12+
$rules = Rule::int()->min(1)->max(10);
13+
$this->assertEquals('int|min:1|max:10', $rules->toString());
14+
}
15+
}

tests/RuleTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ public function testInt()
2525
$this->assertEquals('int', $rules->toString());
2626
}
2727

28-
public function testFluidString()
28+
public function testDoNotRepeat()
2929
{
30-
$rules = Rule::required()->string()->email();
31-
$this->assertEquals('required|string|email', $rules->toString());
30+
$this->assertEquals('required', Rule::required()->required()->required()->toString());
31+
$this->assertEquals('required|string', Rule::required()->string()->required()->required()->toString());
32+
$this->assertEquals('string|required', Rule::string()->required()->required()->required()->toString());
3233
}
3334
}

tests/StringRulesTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Saritasa\Laravel\Validation\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Saritasa\Laravel\Validation\Rule;
7+
8+
class StringRulesTest extends TestCase
9+
{
10+
public function testFluidString()
11+
{
12+
$rules = Rule::required()->string()->email()->requiredWithout('facebook_id');
13+
$this->assertEquals('required|string|email|required_without:facebook_id', $rules->toString());
14+
}
15+
16+
public function testRegex()
17+
{
18+
$rules = Rule::string()->regex('/^\w+[\w-\.]*\w+$/');
19+
$this->assertEquals('string|regex:/^\w+[\w-\.]*\w+$/', $rules->toString());
20+
}
21+
22+
public function testDoNotRepeat()
23+
{
24+
$rules = Rule::string()->email()->email()->regex('@mail.ru$')->regex('@mail.ru$')->email();
25+
$this->assertEquals('string|email|regex:/@mail.ru$/', $rules->toString());
26+
}
27+
}

0 commit comments

Comments
 (0)