You are here

class EmailParser in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/egulias/email-validator/src/Egulias/EmailValidator/EmailParser.php \Egulias\EmailValidator\EmailParser

EmailParser

@author Eduardo Gulias Davis <me@egulias.com>

Hierarchy

Expanded class hierarchy of EmailParser

File

vendor/egulias/email-validator/src/Egulias/EmailValidator/EmailParser.php, line 13

Namespace

Egulias\EmailValidator
View source
class EmailParser {
  const EMAIL_MAX_LENGTH = 254;
  protected $warnings = array();
  protected $domainPart = '';
  protected $localPart = '';
  protected $lexer;
  protected $localPartParser;
  protected $domainPartParser;
  public function __construct(EmailLexer $lexer) {
    $this->lexer = $lexer;
    $this->localPartParser = new LocalPart($this->lexer);
    $this->domainPartParser = new DomainPart($this->lexer);
  }

  /**
   * @param $str
   * @return array
   */
  public function parse($str) {
    $this->lexer
      ->setInput($str);
    if (!$this
      ->hasAtToken()) {
      throw new \InvalidArgumentException('ERR_NOLOCALPART');
    }
    $this->localPartParser
      ->parse($str);
    $this->domainPartParser
      ->parse($str);
    $this
      ->setParts($str);
    if ($this->lexer
      ->hasInvalidTokens()) {
      throw new \InvalidArgumentException('ERR_INVALID_ATEXT');
    }
    return array(
      'local' => $this->localPart,
      'domain' => $this->domainPart,
    );
  }
  public function getWarnings() {
    $localPartWarnings = $this->localPartParser
      ->getWarnings();
    $domainPartWarnings = $this->domainPartParser
      ->getWarnings();
    $this->warnings = array_merge($localPartWarnings, $domainPartWarnings);
    $this
      ->addLongEmailWarning($this->localPart, $this->domainPart);
    return $this->warnings;
  }
  public function getParsedDomainPart() {
    return $this->domainPart;
  }
  protected function setParts($email) {
    $parts = explode('@', $email);
    $this->domainPart = $this->domainPartParser
      ->getDomainPart();
    $this->localPart = $parts[0];
  }
  protected function hasAtToken() {
    $this->lexer
      ->moveNext();
    $this->lexer
      ->moveNext();
    if ($this->lexer->token['type'] === EmailLexer::S_AT) {
      return false;
    }
    return true;
  }

  /**
   * @param string $localPart
   * @param string $parsedDomainPart
   */
  protected function addLongEmailWarning($localPart, $parsedDomainPart) {
    if (strlen($localPart . '@' . $parsedDomainPart) > self::EMAIL_MAX_LENGTH) {
      $this->warnings[] = EmailValidator::RFC5322_TOOLONG;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EmailParser::$domainPart protected property
EmailParser::$domainPartParser protected property
EmailParser::$lexer protected property
EmailParser::$localPart protected property
EmailParser::$localPartParser protected property
EmailParser::$warnings protected property
EmailParser::addLongEmailWarning protected function
EmailParser::EMAIL_MAX_LENGTH constant
EmailParser::getParsedDomainPart public function
EmailParser::getWarnings public function
EmailParser::hasAtToken protected function
EmailParser::parse public function
EmailParser::setParts protected function
EmailParser::__construct public function