comparison vendor/egulias/email-validator/EmailValidator/Validation/MultipleValidationWithAnd.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
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 public function __construct(array $validations, $mode = self::ALLOW_ALL_ERRORS)
47 {
48 if (count($validations) == 0) {
49 throw new EmptyValidationList();
50 }
51
52 $this->validations = $validations;
53 $this->mode = $mode;
54 }
55
56 /**
57 * {@inheritdoc}
58 */
59 public function isValid($email, EmailLexer $emailLexer)
60 {
61 $result = true;
62 $errors = [];
63 foreach ($this->validations as $validation) {
64 $emailLexer->reset();
65 $validationResult = $validation->isValid($email, $emailLexer);
66 $result = $result && $validationResult;
67 $this->warnings = array_merge($this->warnings, $validation->getWarnings());
68 $errors = $this->addNewError($validation->getError(), $errors);
69
70 if ($this->shouldStop($result)) {
71 break;
72 }
73 }
74
75 if (!empty($errors)) {
76 $this->error = new MultipleErrors($errors);
77 }
78
79 return $result;
80 }
81
82 private function addNewError($possibleError, array $errors)
83 {
84 if (null !== $possibleError) {
85 $errors[] = $possibleError;
86 }
87
88 return $errors;
89 }
90
91 private function shouldStop($result)
92 {
93 return !$result && $this->mode === self::STOP_ON_ERROR;
94 }
95
96 /**
97 * {@inheritdoc}
98 */
99 public function getError()
100 {
101 return $this->error;
102 }
103
104 /**
105 * {@inheritdoc}
106 */
107 public function getWarnings()
108 {
109 return $this->warnings;
110 }
111 }