EachPassed   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 6
c 1
b 0
f 1
dl 0
loc 40
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPassedRules() 0 4 1
A __construct() 0 3 1
A isValid() 0 3 1
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