Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Egulias\EmailValidator\Validation;
4
 
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\Result\InvalidEmail;
7
use Egulias\EmailValidator\Validation\Exception\EmptyValidationList;
8
use Egulias\EmailValidator\Result\MultipleErrors;
150 lars 9
use Egulias\EmailValidator\Warning\Warning;
148 lars 10
 
11
class MultipleValidationWithAnd implements EmailValidation
12
{
13
    /**
14
     * If one of validations fails, the remaining validations will be skipped.
15
     * This means MultipleErrors will only contain a single error, the first found.
16
     */
17
    public const STOP_ON_ERROR = 0;
18
 
19
    /**
20
     * All of validations will be invoked even if one of them got failure.
21
     * So MultipleErrors will contain all causes.
22
     */
23
    public const ALLOW_ALL_ERRORS = 1;
24
 
25
    /**
26
     * @var EmailValidation[]
27
     */
28
    private $validations = [];
29
 
30
    /**
150 lars 31
     * @var Warning[]
148 lars 32
     */
33
    private $warnings = [];
34
 
35
    /**
36
     * @var MultipleErrors|null
37
     */
38
    private $error;
39
 
40
    /**
41
     * @var int
42
     */
43
    private $mode;
44
 
45
    /**
46
     * @param EmailValidation[] $validations The validations.
47
     * @param int               $mode        The validation mode (one of the constants).
48
     */
49
    public function __construct(array $validations, $mode = self::ALLOW_ALL_ERRORS)
50
    {
51
        if (count($validations) == 0) {
52
            throw new EmptyValidationList();
53
        }
54
 
55
        $this->validations = $validations;
56
        $this->mode = $mode;
57
    }
58
 
59
    /**
60
     * {@inheritdoc}
61
     */
150 lars 62
    public function isValid(string $email, EmailLexer $emailLexer): bool
148 lars 63
    {
64
        $result = true;
65
        foreach ($this->validations as $validation) {
66
            $emailLexer->reset();
67
            $validationResult = $validation->isValid($email, $emailLexer);
68
            $result = $result && $validationResult;
69
            $this->warnings = array_merge($this->warnings, $validation->getWarnings());
70
            if (!$validationResult) {
71
                $this->processError($validation);
72
            }
73
 
74
            if ($this->shouldStop($result)) {
75
                break;
76
            }
77
        }
78
 
79
        return $result;
80
    }
81
 
150 lars 82
    private function initErrorStorage(): void
148 lars 83
    {
84
        if (null === $this->error) {
85
            $this->error = new MultipleErrors();
86
        }
87
    }
88
 
150 lars 89
    private function processError(EmailValidation $validation): void
148 lars 90
    {
91
        if (null !== $validation->getError()) {
92
            $this->initErrorStorage();
93
            /** @psalm-suppress PossiblyNullReference */
94
            $this->error->addReason($validation->getError()->reason());
95
        }
96
    }
97
 
150 lars 98
    private function shouldStop(bool $result): bool
148 lars 99
    {
100
        return !$result && $this->mode === self::STOP_ON_ERROR;
101
    }
102
 
103
    /**
104
     * Returns the validation errors.
105
     */
150 lars 106
    public function getError(): ?InvalidEmail
148 lars 107
    {
108
        return $this->error;
109
    }
110
 
111
    /**
150 lars 112
     * @return Warning[]
148 lars 113
     */
150 lars 114
    public function getWarnings(): array
148 lars 115
    {
116
        return $this->warnings;
117
    }
118
}