Skip to content

Commit 01b58f0

Browse files
authored
Merge pull request #23 from sourcetoad/concat-defined
Implement concatDefined
2 parents dc7d14e + 6b42efd commit 01b58f0

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ The defined set can then be used in rules using `RuleSet::useDefined`.
9595
}
9696
```
9797

98+
To concatenate a defined rule set you can call concatDefined with the rule set's name.
99+
100+
```php
101+
RuleSet::create()->required()->concatDefined('existing_email');
102+
```
103+
98104
#### requiredIfAll
99105

100106
Accepts multiple `RequiredIf` rules and only marks as required if all return true.

src/RuleSet.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ public function concat(...$rule): self
6363
return static::create([...$this->rules, ...$rule]);
6464
}
6565

66+
/**
67+
* Append all rules from a defined rule set.
68+
*
69+
* @param string $name
70+
*/
71+
public function concatDefined(string $name): self
72+
{
73+
return $this->concat(...static::useDefined($name)->toArray());
74+
}
75+
6676
/**
6777
* Append a rule to the end of the rule set.
6878
*

tests/Unit/DefinedRuleSetsTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,16 @@ public function testThrowInvalidArgumentExceptionOnUnknownDefinition(): void
7676
// Assert
7777
// No assertions, only expectations.
7878
}
79+
80+
public function testConcatDefinedRuleSet(): void
81+
{
82+
// Arrange
83+
RuleSet::define('user.email', RuleSet::create()->email());
84+
85+
// Act
86+
$ruleSet = RuleSet::create()->required()->concatDefined('user.email');
87+
88+
// Assert
89+
$this->assertSame(['required', 'email'], $ruleSet->toArray());
90+
}
7991
}

0 commit comments

Comments
 (0)