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

Commit cbab31e

Browse files
committed
Basic rules: string, int, required
1 parent 566884b commit cbab31e

File tree

8 files changed

+179
-2
lines changed

8 files changed

+179
-2
lines changed

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@
1313
"require": {
1414
"php": ">=7.0",
1515
"illuminate/database": "^5.4",
16-
"illuminate/support": "^5.4"
16+
"illuminate/support": "^5.4",
17+
"saritasa/php-common": "^1.0"
1718
},
1819
"require-dev": {
1920
"phpunit/phpunit": "^6.0"
2021
},
2122
"minimum-stability": "stable",
2223
"autoload": {
2324
"psr-4" : {
24-
"Saritasa\\Laravel\\Validation": "src/"
25+
"Saritasa\\Laravel\\Validation\\": "src/"
2526
}
2627
},
28+
"autoload-dev": {
29+
"psr-4": { "Saritasa\\Laravel\\Validation\\Tests\\": "tests/" }
30+
},
2731
"config": {
2832
"preferred-install": "dist",
2933
"sort-packages": true

src/GenericRuleSet.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Saritasa\Laravel\Validation;
4+
5+
class GenericRuleSet extends RuleSet
6+
{
7+
function int(): IntRuleSet
8+
{
9+
return new IntRuleSet($this->rules);
10+
}
11+
12+
function string(): StringRuleSet
13+
{
14+
return new StringRuleSet($this->rules);
15+
}
16+
}

src/IRule.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Saritasa\Laravel\Validation;
4+
5+
interface IRule
6+
{
7+
function toString(): string;
8+
}

src/IntRuleSet.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Saritasa\Laravel\Validation;
4+
5+
class IntRuleSet extends RuleSet
6+
{
7+
public function __construct(array $rules = [])
8+
{
9+
parent::__construct(self::mergeIfNotExists('int', $rules));
10+
}
11+
12+
public function min(int $minimalValue): IntRuleSet
13+
{
14+
return $this->appendIfNotExists("min:$minimalValue");
15+
}
16+
17+
public function max(int $maximalValue): IntRuleSet
18+
{
19+
return $this->appendIfNotExists("max:$maximalValue");
20+
}
21+
}

src/Rule.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@
22

33
namespace Saritasa\Laravel\Validation;
44

5+
/**
6+
* Root builder for validation rules
7+
*/
58
class Rule
69
{
10+
static function required(): GenericRuleSet
11+
{
12+
return (new GenericRuleSet())->required();
13+
}
14+
15+
static function int(): IntRuleSet
16+
{
17+
return new IntRuleSet();
18+
}
19+
20+
static function string(): StringRuleSet
21+
{
22+
return new StringRuleSet();
23+
}
724
}

src/RuleSet.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Saritasa\Laravel\Validation;
4+
5+
class RuleSet
6+
{
7+
/** @var array */
8+
protected $rules;
9+
10+
public function __construct(array $rules = [])
11+
{
12+
$this->rules = $rules;
13+
}
14+
15+
/**
16+
* @return static
17+
*/
18+
public function required()
19+
{
20+
return $this->appendIfNotExists('required');
21+
}
22+
23+
protected function appendIfNotExists(string $rule)
24+
{
25+
if (in_array($rule, $this->rules)) {
26+
return $this;
27+
}
28+
else {
29+
return new static(array_merge($this->rules, [$rule]));
30+
}
31+
}
32+
33+
protected static function mergeIfNotExists(string $rule, array $rules = []): array
34+
{
35+
if (in_array($rule, $rules)) {
36+
return $rules;
37+
}
38+
else {
39+
return array_merge($rules, [$rule]);
40+
}
41+
}
42+
43+
public function toArray()
44+
{
45+
return array_map(function($rule) {
46+
if ($rule instanceof IRule) {
47+
return $rule->toString();
48+
} else {
49+
return $rule;
50+
}
51+
}, $this->rules);
52+
}
53+
54+
public function toString()
55+
{
56+
return implode('|', $this->toArray());
57+
}
58+
}

src/StringRuleSet.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Saritasa\Laravel\Validation;
4+
5+
class StringRuleSet extends RuleSet
6+
{
7+
public function __construct(array $rules = [])
8+
{
9+
parent::__construct(self::mergeIfNotExists('string', $rules));
10+
}
11+
12+
public function email(): StringRuleSet
13+
{
14+
return $this->appendIfNotExists('email');
15+
}
16+
17+
public function regex(string $pattern){
18+
return $this->appendIfNotExists("regex:$pattern");
19+
}
20+
}

tests/RuleTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Saritasa\Laravel\Validation\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Saritasa\Laravel\Validation\Rule;
7+
8+
class RuleTest extends TestCase
9+
{
10+
public function testRequired()
11+
{
12+
$rules = Rule::required();
13+
$this->assertEquals('required', $rules->toString());
14+
}
15+
16+
public function testString()
17+
{
18+
$rules = Rule::string();
19+
$this->assertEquals('string', $rules->toString());
20+
}
21+
22+
public function testInt()
23+
{
24+
$rules = Rule::int();
25+
$this->assertEquals('int', $rules->toString());
26+
}
27+
28+
public function testFluidString()
29+
{
30+
$rules = Rule::required()->string()->email();
31+
$this->assertEquals('required|string|email', $rules->toString());
32+
}
33+
}

0 commit comments

Comments
 (0)