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