Skip to content

Commit

Permalink
Fix #153: Rename Rules to RuleSet (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Feb 8, 2022
1 parent 72c1ffe commit 1d076d6
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 99 deletions.
12 changes: 6 additions & 6 deletions README.md
Expand Up @@ -46,12 +46,12 @@ Library could be used in two ways: validating a single value and validating a se
### Validating a single value

```php
use Yiisoft\Validator\Rules;
use Yiisoft\Validator\RuleSet;
use Yiisoft\Validator\Rule\Required;
use Yiisoft\Validator\Rule\Number;
use Yiisoft\Validator\Result;

$rules = new Rules([
$ruleSet = new RuleSet([
Required::rule(),
Number::rule()->min(10),
static function ($value): Result {
Expand All @@ -63,7 +63,7 @@ $rules = new Rules([
}
]);

$result = $rules->validate(41);
$result = $ruleSet->validate(41);
if ($result->isValid() === false) {
foreach ($result->getErrors() as $error) {
// ...
Expand Down Expand Up @@ -332,16 +332,16 @@ final class NoLessThanExistingBidRule extends Rule
To reuse multiple validation rules it is advised to group rules like the following:

```php
use Yiisoft\Validator\Rules;
use Yiisoft\Validator\RuleSet;
use Yiisoft\Validator\Rule\HasLength;
use Yiisoft\Validator\Rule\MatchRegularExpression;
use \Yiisoft\Validator\Rule\GroupRule;

final class UsernameRule extends GroupRule
{
public function getRules(): Rules
public function getRules(): RuleSet
{
return new Rules([
return new RuleSet([
HasLength::rule()->min(2)->max(20),
MatchRegularExpression::rule('~[a-z_\-]~i')
]);
Expand Down
2 changes: 1 addition & 1 deletion src/Rule.php
Expand Up @@ -64,7 +64,7 @@ final public function validate($value, ValidationContext $context = null): Resul
}

if (
($this->skipOnError && $context && $context->getParameter(Rules::PARAMETER_PREVIOUS_RULES_ERRORED) === true) ||
($this->skipOnError && $context && $context->getParameter(RuleSet::PARAMETER_PREVIOUS_RULES_ERRORED) === true) ||
(is_callable($this->when) && !($this->when)($value, $context))
) {
return new Result();
Expand Down
12 changes: 6 additions & 6 deletions src/Rule/Each.php
Expand Up @@ -7,7 +7,7 @@
use Yiisoft\Validator\HasValidationErrorMessage;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\Rule;
use Yiisoft\Validator\Rules;
use Yiisoft\Validator\RuleSet;
use Yiisoft\Validator\ValidationContext;

/**
Expand All @@ -17,15 +17,15 @@ final class Each extends Rule
{
use HasValidationErrorMessage;

private Rules $rules;
private RuleSet $ruleSet;

private string $incorrectInputMessage = 'Value should be array or iterable.';
private string $message = '{error} {value} given.';

public static function rule(Rules $rules): self
public static function rule(RuleSet $ruleSet): self
{
$rule = new self();
$rule->rules = $rules;
$rule->ruleSet = $ruleSet;
return $rule;
}

Expand All @@ -38,7 +38,7 @@ protected function validateValue($value, ValidationContext $context = null): Res
}

foreach ($value as $index => $item) {
$itemResult = $this->rules->validate($item, $context);
$itemResult = $this->ruleSet->validate($item, $context);
if ($itemResult->isValid()) {
continue;
}
Expand Down Expand Up @@ -73,6 +73,6 @@ public function incorrectInputMessage(string $message): self

public function getOptions(): array
{
return $this->rules->asArray();
return $this->ruleSet->asArray();
}
}
10 changes: 5 additions & 5 deletions src/Rule/GroupRule.php
Expand Up @@ -7,7 +7,7 @@
use Yiisoft\Validator\HasValidationErrorMessage;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\Rule;
use Yiisoft\Validator\Rules;
use Yiisoft\Validator\RuleSet;
use Yiisoft\Validator\ValidationContext;

/**
Expand All @@ -22,7 +22,7 @@ abstract class GroupRule extends Rule
protected function validateValue($value, ValidationContext $context = null): Result
{
$result = new Result();
if (!$this->getRules()->validate($value, $context)->isValid()) {
if (!$this->getRuleSet()->validate($value, $context)->isValid()) {
$result->addError($this->formatMessage($this->message));
}

Expand All @@ -32,12 +32,12 @@ protected function validateValue($value, ValidationContext $context = null): Res
/**
* Return custom rules set
*
* @return Rules
* @return RuleSet
*/
abstract protected function getRules(): Rules;
abstract protected function getRuleSet(): RuleSet;

public function getOptions(): array
{
return $this->getRules()->asArray();
return $this->getRuleSet()->asArray();
}
}
8 changes: 4 additions & 4 deletions src/Rule/Nested.php
Expand Up @@ -11,7 +11,7 @@
use Yiisoft\Validator\Result;
use Yiisoft\Validator\Rule;
use Yiisoft\Validator\RuleInterface;
use Yiisoft\Validator\Rules;
use Yiisoft\Validator\RuleSet;
use Yiisoft\Validator\ValidationContext;
use function is_array;
use function is_object;
Expand Down Expand Up @@ -93,10 +93,10 @@ protected function validateValue($value, ValidationContext $context = null): Res
continue;
}

$ruleSet = is_array($rules) ? $rules : [$rules];
$aggregatedRule = new Rules($ruleSet);
$rules = is_array($rules) ? $rules : [$rules];
$ruleSet = new RuleSet($rules);
$validatedValue = ArrayHelper::getValueByPath($value, $valuePath);
$itemResult = $aggregatedRule->validate($validatedValue);
$itemResult = $ruleSet->validate($validatedValue);
if ($itemResult->isValid()) {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Rules.php → src/RuleSet.php
Expand Up @@ -9,9 +9,9 @@
use function is_callable;

/**
* Rules represents multiple rules for a single value.
* Rule set represents multiple rules for a single value.
*/
final class Rules
final class RuleSet
{
public const PARAMETER_PREVIOUS_RULES_ERRORED = 'previousRulesErrored';

Expand Down
10 changes: 5 additions & 5 deletions src/RulesDumper.php
Expand Up @@ -60,16 +60,16 @@ public function asArray(iterable $rules): array
$rulesOfArray = [];
foreach ($rules as $attribute => $rulesSet) {
if (is_array($rulesSet)) {
$rulesSet = new Rules($rulesSet);
$ruleSet = new RuleSet($rulesSet);
}
if (!$rulesSet instanceof Rules) {
if (!$ruleSet instanceof RuleSet) {
throw new InvalidArgumentException(sprintf(
'Value should be an instance of %s or an array of rules, %s given.',
Rules::class,
is_object($rulesSet) ? get_class($rulesSet) : gettype($rulesSet)
RuleSet::class,
is_object($ruleSet) ? get_class($ruleSet) : gettype($ruleSet)
));
}
$rulesOfArray[$attribute] = $rulesSet->withFormatter($this->formatter)->asArray();
$rulesOfArray[$attribute] = $ruleSet->withFormatter($this->formatter)->asArray();
}
return $rulesOfArray;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Validator.php
Expand Up @@ -39,13 +39,13 @@ public function validate($data, iterable $rules = []): ResultSet
$results = new ResultSet();

foreach ($rules as $attribute => $attributeRules) {
$aggregateRule = new Rules($attributeRules);
$ruleSet = new RuleSet($attributeRules);
if ($this->formatter !== null) {
$aggregateRule = $aggregateRule->withFormatter($this->formatter);
$ruleSet = $ruleSet->withFormatter($this->formatter);
}
$results->addResult(
$attribute,
$aggregateRule->validate($data->getAttributeValue($attribute), $context->withAttribute($attribute))
$ruleSet->validate($data->getAttributeValue($attribute), $context->withAttribute($attribute))
);
}
if ($data instanceof PostValidationHookInterface) {
Expand Down
12 changes: 6 additions & 6 deletions tests/Rule/EachTest.php
Expand Up @@ -7,7 +7,7 @@
use PHPUnit\Framework\TestCase;
use Yiisoft\Validator\Rule\Each;
use Yiisoft\Validator\Rule\Number;
use Yiisoft\Validator\Rules;
use Yiisoft\Validator\RuleSet;

/**
* @group validators
Expand All @@ -23,11 +23,11 @@ public function validateValues(): void
10, 20, 30,
];

$rules = new Rules([
$ruleSet = new RuleSet([
Number::rule()->max(13),
]);

$result = Each::rule($rules)->validate($values);
$result = Each::rule($ruleSet)->validate($values);
$errors = $result->getErrors();

$this->assertFalse($result->isValid());
Expand All @@ -38,12 +38,12 @@ public function validateValues(): void

public function testName(): void
{
$this->assertEquals('each', Each::rule(new Rules([Number::rule()->max(13)]))->getName());
$this->assertEquals('each', Each::rule(new RuleSet([Number::rule()->max(13)]))->getName());
}

public function testOptions(): void
{
$rules = new Rules([
$ruleSet = new RuleSet([
Number::rule()->max(13),
Number::rule()->max(14),
]);
Expand Down Expand Up @@ -71,6 +71,6 @@ public function testOptions(): void
'skipOnEmpty' => false,
'skipOnError' => true,
],
], Each::rule($rules)->getOptions());
], Each::rule($ruleSet)->getOptions());
}
}
10 changes: 5 additions & 5 deletions tests/Rule/NestedTest.php
Expand Up @@ -15,7 +15,7 @@
use Yiisoft\Validator\Rule\Nested;
use Yiisoft\Validator\Rule\Number;
use Yiisoft\Validator\Rule\Required;
use Yiisoft\Validator\Rules;
use Yiisoft\Validator\RuleSet;
use Yiisoft\Validator\Tests\Stub\ParametrizedRule;

/**
Expand Down Expand Up @@ -207,9 +207,9 @@ public function testWithOtherNestedAndEach(): void
],
];
$rule = Nested::rule([
'charts' => Each::rule(new Rules([
'charts' => Each::rule(new RuleSet([
Nested::rule([
'points' => Each::rule(new Rules([
'points' => Each::rule(new RuleSet([
Nested::rule([
'coordinates' => Nested::rule([
'x' => [
Expand All @@ -223,7 +223,7 @@ public function testWithOtherNestedAndEach(): void
],
'y' => [Number::rule()->min(-10)->max(10)],
]),
'rgb' => Each::rule(new Rules([
'rgb' => Each::rule(new RuleSet([
Number::rule()->min(0)->max(255)->skipOnError(false),
])),
])->skipOnError(false),
Expand Down Expand Up @@ -325,7 +325,7 @@ public function testIntValuePath(): void
public function testSeparateErrorGroups(): void
{
$rule = Nested::rule([
'key' => Each::rule(new Rules([
'key' => Each::rule(new RuleSet([
HasLength::rule()->min(5)->skipOnError(false),
InRange::rule(['aaa', 'bbb', 'ccc'])->skipOnError(false),
])),
Expand Down

0 comments on commit 1d076d6

Please sign in to comment.