Subversion-Projekte lars-tiefland.laravel_shop

Revision

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