EachPassed::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Kontrolio\Rules\Core;
4
5
use Kontrolio\Rules\AbstractRule;
6
use Kontrolio\Rules\RuleInterface;
7
8
/**
9
 * Validation rule passing when all the given rules are passing
10
 *
11
 * @package Kontrolio\Rules\Core
12
 */
13
class EachPassed extends AbstractRule
14
{
15
    /**
16
     * @var RuleInterface[]
17
     */
18
    private $rules;
19
20
    /**
21
     * Validation rule constructor
22
     *
23
     * @param RuleInterface[] ...$rules
24
     */
25
    public function __construct(RuleInterface ...$rules)
26
    {
27
        $this->rules = $rules;
28
    }
29
30
    /**
31
     * Validates input.
32
     *
33
     * @param mixed $input
34
     *
35
     * @return bool
36
     */
37
    public function isValid($input = null)
38
    {
39
        return count($this->getPassedRules($input)) === count($this->rules);
40
    }
41
42
    /**
43
     * Filters rules and returns only passed ones
44
     *
45
     * @param mixed $input
46
     *
47
     * @return array|RuleInterface[]
48
     */
49
    private function getPassedRules($input)
50
    {
51
        return array_filter($this->rules, function (RuleInterface $rule) use ($input) {
52
            return $rule->isValid($input) === true;
53
        });
54
    }
55
}
56