1<?php 2 3namespace Egulias\EmailValidator\Validation; 4 5use Egulias\EmailValidator\EmailLexer; 6use Egulias\EmailValidator\Validation\Exception\EmptyValidationList; 7 8class 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 $result = $result && $validation->isValid($email, $emailLexer); 66 $this->warnings = array_merge($this->warnings, $validation->getWarnings()); 67 $errors = $this->addNewError($validation->getError(), $errors); 68 69 if ($this->shouldStop($result)) { 70 break; 71 } 72 } 73 74 if (!empty($errors)) { 75 $this->error = new MultipleErrors($errors); 76 } 77 78 return $result; 79 } 80 81 private function addNewError($possibleError, array $errors) 82 { 83 if (null !== $possibleError) { 84 $errors[] = $possibleError; 85 } 86 87 return $errors; 88 } 89 90 private function shouldStop($result) 91 { 92 return !$result && $this->mode === self::STOP_ON_ERROR; 93 } 94 95 /** 96 * {@inheritdoc} 97 */ 98 public function getError() 99 { 100 return $this->error; 101 } 102 103 /** 104 * {@inheritdoc} 105 */ 106 public function getWarnings() 107 { 108 return $this->warnings; 109 } 110} 111