prolab-api/vendor/egulias/email-validator/src/MessageIDParser.php

92 lines
2.3 KiB
PHP
Raw Normal View History

2025-09-24 06:24:52 +00:00
<?php
namespace Egulias\EmailValidator;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Parser\IDLeftPart;
use Egulias\EmailValidator\Parser\IDRightPart;
use Egulias\EmailValidator\Result\ValidEmail;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Warning\EmailTooLong;
use Egulias\EmailValidator\Result\Reason\NoLocalPart;
class MessageIDParser extends Parser
{
public const EMAILID_MAX_LENGTH = 254;
/**
* @var string
*/
protected $idLeft = '';
/**
* @var string
*/
protected $idRight = '';
2025-10-03 11:00:05 +00:00
public function parse(string $str) : Result
2025-09-24 06:24:52 +00:00
{
$result = parent::parse($str);
$this->addLongEmailWarning($this->idLeft, $this->idRight);
return $result;
}
2025-10-03 11:00:05 +00:00
2025-09-24 06:24:52 +00:00
protected function preLeftParsing(): Result
{
if (!$this->hasAtToken()) {
2025-10-03 11:00:05 +00:00
return new InvalidEmail(new NoLocalPart(), $this->lexer->token["value"]);
2025-09-24 06:24:52 +00:00
}
return new ValidEmail();
}
protected function parseLeftFromAt(): Result
{
return $this->processIDLeft();
}
protected function parseRightFromAt(): Result
{
return $this->processIDRight();
}
2025-10-03 11:00:05 +00:00
private function processIDLeft() : Result
2025-09-24 06:24:52 +00:00
{
$localPartParser = new IDLeftPart($this->lexer);
$localPartResult = $localPartParser->parse();
$this->idLeft = $localPartParser->localPart();
2025-10-03 11:00:05 +00:00
$this->warnings = array_merge($localPartParser->getWarnings(), $this->warnings);
2025-09-24 06:24:52 +00:00
return $localPartResult;
}
2025-10-03 11:00:05 +00:00
private function processIDRight() : Result
2025-09-24 06:24:52 +00:00
{
$domainPartParser = new IDRightPart($this->lexer);
$domainPartResult = $domainPartParser->parse();
$this->idRight = $domainPartParser->domainPart();
2025-10-03 11:00:05 +00:00
$this->warnings = array_merge($domainPartParser->getWarnings(), $this->warnings);
2025-09-24 06:24:52 +00:00
return $domainPartResult;
}
2025-10-03 11:00:05 +00:00
public function getLeftPart() : string
2025-09-24 06:24:52 +00:00
{
return $this->idLeft;
}
2025-10-03 11:00:05 +00:00
public function getRightPart() : string
2025-09-24 06:24:52 +00:00
{
return $this->idRight;
}
2025-10-03 11:00:05 +00:00
private function addLongEmailWarning(string $localPart, string $parsedDomainPart) : void
2025-09-24 06:24:52 +00:00
{
if (strlen($localPart . '@' . $parsedDomainPart) > self::EMAILID_MAX_LENGTH) {
$this->warnings[EmailTooLong::CODE] = new EmailTooLong();
}
}
}