Completed
Push — master ( 114b66...8790f5 )
by Eduardo Gulias
07:46
created

Validation/MultipleValidationWithAnd.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Egulias\EmailValidator\Validation;
4
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\Validation\Exception\EmptyValidationList;
7
8
class MultipleValidationWithAnd implements EmailValidation
9
{
10
    /**
11
     * If one of validations gets failure skips all succeeding validation.
12
     * This means MultipleErrors will only contain a single error which first found.
13
     */
14
    const STOP_ON_ERROR = 0;
15
16
    /**
17
     * All of validations will be invoked even if one of them got failure.
18
     * So MultipleErrors will contain all causes.
19
     */
20
    const ALLOW_ALL_ERRORS = 1;
21
22
    /**
23
     * @var EmailValidation[]
24
     */
25
    private $validations = [];
26
27
    /**
28
     * @var array
29
     */
30
    private $warnings = [];
31
32
    /**
33
     * @var MultipleErrors
34
     */
35
    private $error;
36
37
    /**
38
     * @var bool
39
     */
40
    private $mode;
41
42
    /**
43
     * @param EmailValidation[] $validations The validations.
44
     * @param int               $mode        The validation mode (one of the constants).
45
     */
46 7
    public function __construct(array $validations, $mode = self::ALLOW_ALL_ERRORS)
47
    {
48 7
        if (count($validations) == 0) {
49 1
            throw new EmptyValidationList();
50
        }
51
52 6
        $this->validations = $validations;
53 6
        $this->mode = $mode;
0 ignored issues
show
Documentation Bug introduced by
The property $mode was declared of type boolean, but $mode is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
54 6
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 6
    public function isValid($email, EmailLexer $emailLexer)
60
    {
61 6
        $result = true;
62 6
        $errors = [];
63 6
        foreach ($this->validations as $validation) {
64 6
            $emailLexer->reset();
65 6
            $result = $result && $validation->isValid($email, $emailLexer);
66 6
            $this->warnings = array_merge($this->warnings, $validation->getWarnings());
67 6
            $errors = $this->addNewError($validation->getError(), $errors);
68
69 6
            if ($this->shouldStop($result)) {
70 1
                break;
71
            }
72 6
        }
73
74 6
        if (!empty($errors)) {
75 2
            $this->error = new MultipleErrors($errors);
76 2
        }
77
78 6
        return $result;
79
    }
80
81 6
    private function addNewError($possibleError, array $errors)
82
    {
83 6
        if (null !== $possibleError) {
84 2
            $errors[] = $possibleError;
85 2
        }
86
87 6
        return $errors;
88
    }
89
90 6
    private function shouldStop($result)
91
    {
92 6
        return !$result && $this->mode === self::STOP_ON_ERROR;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 4
    public function getError()
99
    {
100 4
        return $this->error;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 2
    public function getWarnings()
107
    {
108 2
        return $this->warnings;
109
    }
110
}
111