Skip to content

Commit 0db353e

Browse files
add new test and modified rules
1 parent db1f330 commit 0db353e

File tree

8 files changed

+257
-8
lines changed

8 files changed

+257
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/.idea
44
/.vscode
55
composer.lock
6+
.phpunit.cache

CHANGELOG.md

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

33
All notable changes to `shergela/validation-rule` will be documented in this file.
44

5+
## 1.0.2 | 23 August - 2024
6+
7+
## Modified Rule::in() and Rule::notIn() rules.
8+
### Add tests.
9+
10+
511
## 1.0.2 | 19 August - 2024
612

713
## These methods work with timezones

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
"Shergela\\Validations\\": "src"
99
}
1010
},
11+
"autoload-dev": {
12+
"psr-4": {
13+
"Shergela\\Validations\\": "tests"
14+
}
15+
},
1116
"authors": [
1217
{
1318
"name": "Avto Shergelashvili",

phpunit.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
cacheDirectory=".phpunit.cache"
6+
executionOrder="depends,defects"
7+
beStrictAboutCoverageMetadata="true"
8+
beStrictAboutOutputDuringTests="true"
9+
failOnRisky="true"
10+
failOnWarning="true"
11+
colors="true">
12+
<testsuites>
13+
<testsuite name="default">
14+
<directory suffix=".php">tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
</phpunit>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Shergela\Validations\DataManipulation;
4+
5+
use BackedEnum;
6+
use Illuminate\Contracts\Support\Arrayable;
7+
use UnitEnum;
8+
9+
class InNotInRuleManipulation
10+
{
11+
/**
12+
* @param Arrayable<int, string>|array<string>|string $values
13+
*/
14+
public function __construct(protected readonly Arrayable|array|string $values)
15+
{
16+
}
17+
18+
/**
19+
* @return string
20+
*/
21+
private function manipulation(): string
22+
{
23+
$values = $this->values;
24+
25+
$values = array_map(function ($value) {
26+
$value = match (true) {
27+
$value instanceof BackedEnum => $value->value,
28+
$value instanceof UnitEnum => $value->name,
29+
default => $value,
30+
};
31+
32+
/** @var string $v */
33+
$v = $value;
34+
35+
return '"' . str_replace('"', '""', $v) . '"';
36+
}, (array) $values);
37+
38+
return implode(',', $values);
39+
}
40+
41+
/**
42+
* @return string
43+
*/
44+
public function handle(): string
45+
{
46+
return $this->manipulation();
47+
}
48+
}

src/Validation/Rule.php

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
namespace Shergela\Validations\Validation;
44

5+
use BackedEnum;
56
use Closure;
67
use DateTimeZone;
8+
use Illuminate\Contracts\Support\Arrayable;
79
use Illuminate\Contracts\Validation\DataAwareRule;
810
use Illuminate\Contracts\Validation\ValidationRule;
911
use Illuminate\Contracts\Validation\ValidatorAwareRule;
1012
use Illuminate\Support\Arr;
1113
use Illuminate\Support\Facades\Validator as ValidatorFacade;
1214
use Illuminate\Validation\Validator;
15+
use Shergela\Validations\DataManipulation\InNotInRuleManipulation;
1316
use Shergela\Validations\Enums\DatetimeZoneAbbreviationEnum;
1417
use Shergela\Validations\Enums\ValidationDateEnum;
1518
use Shergela\Validations\Enums\ValidationIntegerEnum as IntegerRule;
@@ -22,6 +25,7 @@
2225
use Shergela\Validations\Rules\TimezoneRegionValidation as TimezoneRegion;
2326
use Shergela\Validations\Rules\TimezoneValidation as Timezone;
2427
use Shergela\Validations\Rules\UppercaseFirstLetter as UpperFL;
28+
use UnitEnum;
2529

2630
class Rule extends BuildValidationRule implements ValidationRule, ValidatorAwareRule, DataAwareRule
2731
{
@@ -909,27 +913,37 @@ public function size(int $size): static
909913
}
910914

911915
/**
912-
* @param array<string> $values
916+
* @param Arrayable<int, string>|array<string>|string $values
913917
* @return Rule
914918
*/
915-
public static function in(array $values): Rule
919+
public static function in(Arrayable|array|string $values): Rule
916920
{
917-
$implode = implode(',', $values);
921+
if ($values instanceof Arrayable) {
922+
$values = $values->toArray();
923+
}
924+
925+
/** @var Arrayable<int, string>|array<string>|string $values */
926+
$values = is_array($values) ? $values : func_get_args();
918927

919-
static::$in = RuleEnum::IN . $implode;
928+
static::$in = RuleEnum::IN . (new InNotInRuleManipulation(values: $values))->handle();
920929

921930
return new self();
922931
}
923932

924933
/**
925-
* @param array<string> $values
934+
* @param Arrayable<int, string>|array<string>|string $values
926935
* @return Rule
927936
*/
928-
public static function notIn(array $values): Rule
937+
public static function notIn(Arrayable|array|string $values): Rule
929938
{
930-
$implode = implode(',', $values);
939+
if ($values instanceof Arrayable) {
940+
$values = $values->toArray();
941+
}
931942

932-
static::$notIn = RuleEnum::NOT_IN . $implode;
943+
/** @var Arrayable<int, string>|array<string>|string $values */
944+
$values = is_array($values) ? $values : func_get_args();
945+
946+
static::$notIn = RuleEnum::NOT_IN . (new InNotInRuleManipulation(values: $values))->handle();
933947

934948
return new self();
935949
}
@@ -970,6 +984,32 @@ public function passes(string $attribute, mixed $value): bool
970984
return true;
971985
}
972986

987+
/**
988+
* @return array<string>
989+
*/
990+
public function getRules(): array
991+
{
992+
return $this->getValidationRules();
993+
}
994+
995+
/**
996+
* @param int $key
997+
* @return string|null
998+
*/
999+
public function getRule(int $key = 0): string|null
1000+
{
1001+
$rules = $this->getRules();
1002+
1003+
/**
1004+
* Get only one element of the array
1005+
*/
1006+
if (isset($rules[$key])) {
1007+
return $rules[$key];
1008+
}
1009+
1010+
return null;
1011+
}
1012+
9731013
/**
9741014
* Adds the given failures, and return false.
9751015
*

tests/RuleInTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Shergela\Validations;
4+
5+
6+
use App\Enums\TestEnum;
7+
use Shergela\Validations\Validation\Rule;
8+
use Tests\TestCase;
9+
10+
class RuleInTest extends TestCase
11+
{
12+
public function test_in_validation()
13+
{
14+
$rule = Rule::in(['Laravel', 'Framework', 'PHP'])->getRule();
15+
16+
$this->assertSame('in:"Laravel","Framework","PHP"', (string) $rule);
17+
18+
$rule = Rule::in(collect(['Taylor', 'Michael', 'Tim']))->getRule();
19+
20+
$this->assertSame('in:"Taylor","Michael","Tim"', (string) $rule);
21+
22+
$rule = Rule::in(['Life, the Universe and Everything', 'this is a "quote"'])->getRule();
23+
24+
$this->assertSame('in:"Life, the Universe and Everything","this is a ""quote"""', (string) $rule);
25+
26+
$rule = Rule::in(collect([1, 2, 3, 4]))->getRule();
27+
28+
$this->assertSame('in:"1","2","3","4"', (string) $rule);
29+
30+
$rule = Rule::in(collect([1, 2, 3, 4]))->getRule();
31+
32+
$this->assertSame('in:"1","2","3","4"', (string) $rule);
33+
34+
$rule = Rule::in(["a,b\nc,d"])->getRule();
35+
36+
$this->assertSame("in:\"a,b\nc,d\"", (string) $rule);
37+
38+
$rule = Rule::in([1, 2, 3, 4])->getRule();
39+
40+
$this->assertSame('in:"1","2","3","4"', (string) $rule);
41+
42+
$rule = Rule::in(collect([1, 2, 3, 4]))->getRule();
43+
44+
$this->assertSame('in:"1","2","3","4"', (string) $rule);
45+
46+
$rule = Rule::in('1', '2', '3', '4')->getRule();
47+
48+
$this->assertSame('in:"1","2","3","4"', (string) $rule);
49+
50+
$rule = Rule::in('1', '2', '3', '4')->getRule();
51+
52+
$this->assertSame('in:"1","2","3","4"', (string) $rule);
53+
54+
$rule = Rule::in([TestEnum::ONE])->getRule();
55+
56+
$this->assertSame('in:"1"', (string) $rule);
57+
58+
$rule = Rule::in([TestEnum::TWO])->getRule();
59+
60+
$this->assertSame('in:"2"', (string) $rule);
61+
62+
$rule = Rule::in([TestEnum::STRING_ONE])->getRule();
63+
64+
$this->assertSame('in:"one"', (string) $rule);
65+
}
66+
}

tests/RuleNotInTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Shergela\Validations;
4+
5+
6+
use App\Enums\TestEnum;
7+
use Shergela\Validations\Validation\Rule;
8+
use Tests\TestCase;
9+
10+
class RuleNotInTest extends TestCase
11+
{
12+
public function test_not_in_validation()
13+
{
14+
$rule = Rule::notIn(['Caldera', 'Aramis', 'Athos'])->getRule(1);
15+
16+
$this->assertSame('not_in:"Caldera","Aramis","Athos"', (string) $rule);
17+
18+
$rule = Rule::notIn(collect(['Taylor', 'Michael', 'Tim']))->getRule(1);
19+
20+
$this->assertSame('not_in:"Taylor","Michael","Tim"', (string) $rule);
21+
22+
$rule = Rule::notIn(['Life, the Universe and Everything', 'this is a "quote"'])->getRule(1);
23+
24+
$this->assertSame('not_in:"Life, the Universe and Everything","this is a ""quote"""', (string) $rule);
25+
26+
$rule = Rule::notIn(collect([1, 2, 3, 4]))->getRule(1);
27+
28+
$this->assertSame('not_in:"1","2","3","4"', (string) $rule);
29+
30+
$rule = Rule::notIn(collect([1, 2, 3, 4]))->getRule(1);
31+
32+
$this->assertSame('not_in:"1","2","3","4"', (string) $rule);
33+
34+
$rule = Rule::notIn(["a,b\nc,d"])->getRule(1);
35+
36+
$this->assertSame("not_in:\"a,b\nc,d\"", (string) $rule);
37+
38+
$rule = Rule::notIn([1, 2, 3, 4])->getRule(1);
39+
40+
$this->assertSame('not_in:"1","2","3","4"', (string) $rule);
41+
42+
$rule = Rule::notIn(collect([1, 2, 3, 4]))->getRule(1);
43+
44+
$this->assertSame('not_in:"1","2","3","4"', (string) $rule);
45+
46+
$rule = Rule::notIn('1', '2', '3', '4')->getRule(1);
47+
48+
$this->assertSame('not_in:"1","2","3","4"', (string) $rule);
49+
50+
$rule = Rule::notIn('1', '2', '3', '4')->getRule(1);
51+
52+
$this->assertSame('not_in:"1","2","3","4"', (string) $rule);
53+
54+
$rule = Rule::notIn([TestEnum::ONE])->getRule(1);
55+
56+
$this->assertSame('not_in:"1"', (string) $rule);
57+
58+
$rule = Rule::notIn([TestEnum::TWO])->getRule(1);
59+
60+
$this->assertSame('not_in:"2"', (string) $rule);
61+
62+
$rule = Rule::notIn([TestEnum::STRING_ONE])->getRule(1);
63+
64+
$this->assertSame('not_in:"one"', (string) $rule);
65+
}
66+
}

0 commit comments

Comments
 (0)