Skip to content

Commit 99bd184

Browse files
authored
Rename InRange rule to In (#395)
1 parent cc3e50a commit 99bd184

6 files changed

Lines changed: 166 additions & 167 deletions

File tree

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,29 @@
1616
use Yiisoft\Validator\WhenInterface;
1717

1818
/**
19-
* Validates that the value is among a list of values.
20-
*
21-
* The range can be specified via constructor.
22-
* If the {@see InRange::$not} is called, the rule will ensure the value is NOT among the specified range.
19+
* Validates that the value is one of the values provided in {@see $values}.
20+
* If the {@see In::$not} is set, the validation logic is inverted and the rule will ensure that the value is NOT one of
21+
* them.
2322
*/
2423
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
25-
final class InRange implements SerializableRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
24+
final class In implements SerializableRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
2625
{
2726
use SkipOnEmptyTrait;
2827
use SkipOnErrorTrait;
2928
use WhenTrait;
3029

3130
public function __construct(
32-
private iterable $range,
3331
/**
34-
* @var bool whether the comparison is strict (both type and value must be the same)
32+
* @var iterable<scalar>
33+
*/
34+
private iterable $values,
35+
/**
36+
* @var bool Whether the comparison is strict (both type and value must be the same)
3537
*/
3638
private bool $strict = false,
3739
/**
38-
* @var bool whether to invert the validation logic. Defaults to false. If set to `true`, the value should NOT
39-
* be among the list of values passed via constructor.
40+
* @var bool Whether to invert the validation logic. Defaults to `false`. If set to `true`, the value must NOT
41+
* be among the list of {@see $values}.
4042
*/
4143
private bool $not = false,
4244
private string $message = 'This value is invalid.',
@@ -58,9 +60,9 @@ public function getName(): string
5860
return 'inRange';
5961
}
6062

61-
public function getRange(): iterable
63+
public function getValues(): iterable
6264
{
63-
return $this->range;
65+
return $this->values;
6466
}
6567

6668
public function isStrict(): bool
@@ -81,7 +83,7 @@ public function getMessage(): string
8183
public function getOptions(): array
8284
{
8385
return [
84-
'range' => $this->range,
86+
'values' => $this->values,
8587
'strict' => $this->strict,
8688
'not' => $this->not,
8789
'message' => [
@@ -94,6 +96,6 @@ public function getOptions(): array
9496

9597
public function getHandlerClassName(): string
9698
{
97-
return InRangeHandler::class;
99+
return InHandler::class;
98100
}
99101
}
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,18 @@
1111
use Yiisoft\Validator\ValidationContext;
1212

1313
/**
14-
* Validates that the value is among a list of values.
15-
*
16-
* The range can be specified via constructor.
17-
* If the {@see InRange::$not} is called, the rule will ensure the value is NOT among the specified range.
14+
* A handler for {@see In} rule.
1815
*/
19-
final class InRangeHandler implements RuleHandlerInterface
16+
final class InHandler implements RuleHandlerInterface
2017
{
2118
public function validate(mixed $value, object $rule, ValidationContext $context): Result
2219
{
23-
if (!$rule instanceof InRange) {
24-
throw new UnexpectedRuleException(InRange::class, $rule);
20+
if (!$rule instanceof In) {
21+
throw new UnexpectedRuleException(In::class, $rule);
2522
}
2623

2724
$result = new Result();
28-
if ($rule->isNot() === ArrayHelper::isIn($value, $rule->getRange(), $rule->isStrict())) {
25+
if ($rule->isNot() === ArrayHelper::isIn($value, $rule->getValues(), $rule->isStrict())) {
2926
$result->addError($rule->getMessage(), ['attribute' => $context->getAttribute()]);
3027
}
3128

tests/Rule/InRangeTest.php

Lines changed: 0 additions & 139 deletions
This file was deleted.

tests/Rule/InTest.php

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Validator\Tests\Rule;
6+
7+
use ArrayObject;
8+
use Yiisoft\Validator\Rule\In;
9+
use Yiisoft\Validator\Rule\InHandler;
10+
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait;
11+
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
12+
use Yiisoft\Validator\Tests\Rule\Base\SerializableRuleTestTrait;
13+
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait;
14+
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
15+
16+
final class InTest extends RuleTestCase
17+
{
18+
use DifferentRuleInHandlerTestTrait;
19+
use SerializableRuleTestTrait;
20+
use SkipOnErrorTestTrait;
21+
use WhenTestTrait;
22+
23+
public function testGetName(): void
24+
{
25+
$rule = new In(range(1, 10));
26+
$this->assertSame('inRange', $rule->getName());
27+
}
28+
29+
public function dataOptions(): array
30+
{
31+
return [
32+
[
33+
new In(range(1, 10)),
34+
[
35+
'values' => range(1, 10),
36+
'strict' => false,
37+
'not' => false,
38+
'message' => [
39+
'message' => 'This value is invalid.',
40+
],
41+
'skipOnEmpty' => false,
42+
'skipOnError' => false,
43+
],
44+
],
45+
[
46+
new In(range(1, 2), strict: true),
47+
[
48+
'values' => [1, 2],
49+
'strict' => true,
50+
'not' => false,
51+
'message' => [
52+
'message' => 'This value is invalid.',
53+
],
54+
'skipOnEmpty' => false,
55+
'skipOnError' => false,
56+
],
57+
],
58+
[
59+
new In(range(1, 2), not: true),
60+
[
61+
'values' => [1, 2],
62+
'strict' => false,
63+
'not' => true,
64+
'message' => [
65+
'message' => 'This value is invalid.',
66+
],
67+
'skipOnEmpty' => false,
68+
'skipOnError' => false,
69+
],
70+
],
71+
];
72+
}
73+
74+
public function dataValidationPassed(): array
75+
{
76+
return [
77+
[1, [new In(range(1, 10))]],
78+
[10, [new In(range(1, 10))]],
79+
['10', [new In(range(1, 10))]],
80+
['5', [new In(range(1, 10))]],
81+
82+
[['a'], [new In([['a'], ['b']])]],
83+
['a', [new In(new ArrayObject(['a', 'b']))]],
84+
85+
[1, [new In(range(1, 10), strict: true)]],
86+
[5, [new In(range(1, 10), strict: true)]],
87+
[10, [new In(range(1, 10), strict: true)]],
88+
89+
[0, [new In(range(1, 10), not: true)]],
90+
[11, [new In(range(1, 10), not: true)]],
91+
[5.5, [new In(range(1, 10), not: true)]],
92+
];
93+
}
94+
95+
public function dataValidationFailed(): array
96+
{
97+
$errors = ['' => ['This value is invalid.']];
98+
99+
return [
100+
[0, [new In(range(1, 10))], $errors],
101+
[11, [new In(range(1, 10))], $errors],
102+
[5.5, [new In(range(1, 10))], $errors],
103+
104+
[null, [new In(range(1, 10))], $errors],
105+
['0', [new In(range(1, 10))], $errors],
106+
[0, [new In(range(1, 10))], $errors],
107+
['', [new In(range(1, 10))], $errors],
108+
109+
['1', [new In(range(1, 10), strict: true)], $errors],
110+
['10', [new In(range(1, 10), strict: true)], $errors],
111+
['5.5', [new In(range(1, 10), strict: true)], $errors],
112+
[['1', '2', '3', '4', '5', '6'], [new In(range(1, 10), strict: true)], $errors],
113+
[['1', '2', '3', 4, 5, 6], [new In(range(1, 10), strict: true)], $errors],
114+
115+
[1, [new In(range(1, 10), not: true)], $errors],
116+
[10, [new In(range(1, 10), not: true)], $errors],
117+
['10', [new In(range(1, 10), not: true)], $errors],
118+
['5', [new In(range(1, 10), not: true)], $errors],
119+
120+
'custom error' => [15, [new In(range(1, 10), message: 'Custom error')], ['' => ['Custom error']]],
121+
];
122+
}
123+
124+
public function testSkipOnError(): void
125+
{
126+
$this->testSkipOnErrorInternal(new In(range(1, 10)), new In(range(1, 10), skipOnError: true));
127+
}
128+
129+
public function testWhen(): void
130+
{
131+
$when = static fn (mixed $value): bool => $value !== null;
132+
$this->testWhenInternal(new In(range(1, 10)), new In(range(1, 10), when: $when));
133+
}
134+
135+
protected function getDifferentRuleInHandlerItems(): array
136+
{
137+
return [In::class, InHandler::class];
138+
}
139+
}

0 commit comments

Comments
 (0)