Passed
Push — master ( 9e8075...242c87 )
by Dedipyaman
01:41
created

ForAll   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validate() 0 24 5
1
<?php
2
/*
3
 * This file is part of Phypes <https://github.com/2DSharp/Phypes>.
4
 *
5
 * (c) Dedipyaman Das <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
12
namespace Phypes\Rule\Aggregate;
13
14
15
use Phypes\Error\Error;
16
use Phypes\Result\Failure;
17
use Phypes\Result\Result;
18
use Phypes\Result\Success;
19
use Phypes\Rule\Rule;
20
21
class ForAll implements Rule
22
{
23
    /**
24
     * @var array $rules
25
     */
26
    private $rules = [];
27
28
    public function __construct(Rule... $rules)
29
    {
30
        $this->rules = $rules;
31
    }
32
33
    /**
34
     * Validate for each rule. Works with an AND logic.
35
     * @param $data
36
     * @return Result
37
     */
38
    public function validate($data): Result
39
    {
40
        $errors = [];
41
        foreach ($this->rules as $rule) {
42
43
            $result = $rule->validate($data);
44
45
            if (!$result->isValid()) {
46
                /**
47
                 * @var Failure $result
48
                 * @var Error $errors[]
49
                 * @var Error $error
50
                 */
51
                foreach ($result->getErrors() as $error) {
52
                    $errors[] = $error;
53
                }
54
            }
55
56
        }
57
58
        if (empty($errors))
59
            return new Success();
60
        else {
61
            return new Failure(...$errors); // Use the splat operator
62
63
        }
64
    }
65
}