| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Egulias\EmailValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Egulias\EmailValidator\Parser\DomainPart; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Egulias\EmailValidator\Parser\LocalPart; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * EmailParser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @author Eduardo Gulias Davis <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class EmailParser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     const EMAIL_MAX_LENGTH = 254; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $warnings = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $domainPart = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $localPart = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $lexer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected $localPartParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $domainPartParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 151 |  |     public function __construct(EmailLexer $lexer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 151 |  |         $this->lexer = $lexer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 151 |  |         $this->localPartParser = new LocalPart($this->lexer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 151 |  |         $this->domainPartParser = new DomainPart($this->lexer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 151 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param $str | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 151 |  |     public function parse($str) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 151 |  |         $this->lexer->setInput($str); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 151 |  |         if (!$this->hasAtToken()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |             throw new \InvalidArgumentException('ERR_NOLOCALPART'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 150 |  |         $this->localPartParser->parse($str); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 115 |  |         $this->domainPartParser->parse($str); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 75 |  |         $this->setParts($str); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 75 |  |         if ($this->lexer->hasInvalidTokens()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             throw new \InvalidArgumentException('ERR_INVALID_ATEXT'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 75 |  |         return array('local' => $this->localPart, 'domain' => $this->domainPart); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 75 |  |     public function getWarnings() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 75 |  |         $localPartWarnings = $this->localPartParser->getWarnings(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 75 |  |         $domainPartWarnings = $this->domainPartParser->getWarnings(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 75 |  |         $this->warnings = array_merge($localPartWarnings, $domainPartWarnings); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 75 |  |         $this->addLongEmailWarning($this->localPart, $this->domainPart); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 75 |  |         return $this->warnings; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 46 |  |     public function getParsedDomainPart() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 46 |  |         return $this->domainPart; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 75 |  |     protected function setParts($email) | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 75 |  |         $parts = explode('@', $email); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 75 |  |         $this->domainPart = $this->domainPartParser->getDomainPart(); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 75 |  |         $this->localPart = $parts[0]; | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 75 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 151 |  |     protected function hasAtToken() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 151 |  |         $this->lexer->moveNext(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 151 |  |         $this->lexer->moveNext(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 151 |  |         if ($this->lexer->token['type'] === EmailLexer::S_AT) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 150 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param string $localPart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param string $parsedDomainPart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 75 |  |     protected function addLongEmailWarning($localPart, $parsedDomainPart) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 75 |  |         if (strlen($localPart . '@' . $parsedDomainPart) > self::EMAIL_MAX_LENGTH) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 4 |  |             $this->warnings[] = EmailValidator::RFC5322_TOOLONG; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 4 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 75 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |  |