Passed
Pull Request — master (#465)
by
unknown
05:56 queued 02:23
created

PropagateOptionsHelper::propagateForSingleRule()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 7
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 15
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Helper;
6
7
use Yiisoft\Validator\RuleInterface;
8
use Yiisoft\Validator\SkipOnEmptyInterface;
9
use Yiisoft\Validator\SkipOnErrorInterface;
10
use Yiisoft\Validator\WhenInterface;
11
12
/**
13
 * A helper class used to propagate options' values from a single parent rule to its single child rule.
14
 */
15
final class PropagateOptionsHelper
16
{
17
    /**
18
     * Propagates options' values from a single parent rule to its single child rule. The following options' values are
19
     * propagated:
20
     *
21
     * - `$skipOnEmpty` (both rules must implement {@see SkipOnEmptyInterface}).
22
     * - `$skipOnError` (both rules must implement {@see SkipOnErrorInterface}).
23
     * - `$when` (both rules must implement {@see WhenInterface}).
24
     *
25
     * @param RuleInterface $parentRule A parent rule which options' values need to be propagated.
26
     * @param RuleInterface $childRule A child rule which options' values must be changed to be the same as in parent.
27
     *
28
     * @return RuleInterface A child rule with changed options' values or unchanged if none of the required interfaces
29
     * were implemented.
30
     */
31
    public static function propagateForSingleRule(RuleInterface $parentRule, RuleInterface $childRule): RuleInterface
32
    {
33
        if ($parentRule instanceof SkipOnEmptyInterface && $childRule instanceof SkipOnEmptyInterface) {
34
            $childRule = $childRule->skipOnEmpty($parentRule->getSkipOnEmpty());
35
        }
36
37
        if ($parentRule instanceof SkipOnErrorInterface && $childRule instanceof SkipOnErrorInterface) {
38
            $childRule = $childRule->skipOnError($parentRule->shouldSkipOnError());
39
        }
40
41
        if ($parentRule instanceof WhenInterface && $childRule instanceof WhenInterface) {
42
            $childRule = $childRule->when($parentRule->getWhen());
43
        }
44
45
        return $childRule;
46
    }
47
}
48