generated from Nodarx/template
refactor: use new mail interface desing
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -15,42 +15,21 @@ final class FetchOptions
|
||||
|
||||
public static function default(): self
|
||||
{
|
||||
return self::message();
|
||||
}
|
||||
|
||||
public static function summary(): self
|
||||
{
|
||||
return new self([
|
||||
'UID',
|
||||
'FLAGS',
|
||||
'INTERNALDATE',
|
||||
'RFC822.SIZE',
|
||||
]);
|
||||
return (new self([]))
|
||||
->withUid()
|
||||
->withFlags()
|
||||
->withInternalDate()
|
||||
->withSize();
|
||||
}
|
||||
|
||||
public static function message(): self
|
||||
{
|
||||
return self::summary()
|
||||
return self::default()
|
||||
->withHeaders()
|
||||
->withEnvelope()
|
||||
->withBodyStructure();
|
||||
}
|
||||
|
||||
public static function fullMessage(): self
|
||||
{
|
||||
return self::message()->withBodyText();
|
||||
}
|
||||
|
||||
public function withBodySection(string $section): self
|
||||
{
|
||||
$section = strtoupper(trim($section));
|
||||
|
||||
if ($section === '') {
|
||||
return $this;
|
||||
}
|
||||
|
||||
return $this->with(sprintf('BODY[%s]', $section));
|
||||
}
|
||||
|
||||
public static function of(string ...$items): self
|
||||
{
|
||||
return new self(self::normalize($items));
|
||||
@@ -76,6 +55,25 @@ final class FetchOptions
|
||||
return $this->with('RFC822.SIZE');
|
||||
}
|
||||
|
||||
public function withHeaders(): self
|
||||
{
|
||||
return $this->with('BODY[HEADER]');
|
||||
}
|
||||
|
||||
public function withHeader(string ...$fields): self
|
||||
{
|
||||
$fields = array_values(array_filter(array_map(
|
||||
static fn (string $field): string => strtoupper(trim($field)),
|
||||
$fields,
|
||||
), static fn (string $field): bool => $field !== ''));
|
||||
|
||||
if ($fields === []) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
return $this->with(sprintf('BODY.PEEK[HEADER.FIELDS (%s)]', implode(' ', $fields)));
|
||||
}
|
||||
|
||||
public function withEnvelope(): self
|
||||
{
|
||||
return $this->with('ENVELOPE');
|
||||
@@ -88,21 +86,18 @@ final class FetchOptions
|
||||
|
||||
public function withBodyText(): self
|
||||
{
|
||||
return $this->withBodySection('TEXT');
|
||||
return $this->with('BODY[TEXT]');
|
||||
}
|
||||
|
||||
public function withHeaderFields(string ...$fields): self
|
||||
public function withBodySection(string $section): self
|
||||
{
|
||||
$fields = array_values(array_filter(array_map(
|
||||
static fn (string $field): string => strtoupper(trim($field)),
|
||||
$fields,
|
||||
), static fn (string $field): bool => $field !== ''));
|
||||
$section = strtoupper(trim($section));
|
||||
|
||||
if ($fields === []) {
|
||||
if ($section === '') {
|
||||
return $this;
|
||||
}
|
||||
|
||||
return $this->with(sprintf('BODY.PEEK[HEADER.FIELDS (%s)]', implode(' ', $fields)));
|
||||
return $this->with(sprintf('BODY[%s]', $section));
|
||||
}
|
||||
|
||||
public function with(string $item): self
|
||||
|
||||
@@ -21,6 +21,7 @@ final class Message
|
||||
private readonly int $uid,
|
||||
private readonly int $size,
|
||||
private readonly ?string $internalDate,
|
||||
private readonly ?string $receivedAt,
|
||||
private readonly array $flags,
|
||||
private readonly ?string $subject,
|
||||
private readonly ?string $sentAt,
|
||||
@@ -56,6 +57,11 @@ final class Message
|
||||
return $this->internalDate;
|
||||
}
|
||||
|
||||
public function receivedAt(): ?string
|
||||
{
|
||||
return $this->receivedAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
@@ -139,7 +145,7 @@ final class Message
|
||||
|
||||
public function bodyText(): ?string
|
||||
{
|
||||
return $this->bodyText;
|
||||
return $this->bodySections['TEXT'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -160,6 +166,7 @@ final class Message
|
||||
$this->uid,
|
||||
$this->size,
|
||||
$this->internalDate,
|
||||
$this->receivedAt,
|
||||
$this->flags,
|
||||
$this->subject,
|
||||
$this->sentAt,
|
||||
|
||||
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace KTXM\ProviderImap\Client;
|
||||
|
||||
use DateTimeInterface;
|
||||
|
||||
final class MessageParser
|
||||
{
|
||||
public static function isFetchMessage(string $payload): bool
|
||||
@@ -23,12 +25,14 @@ final class MessageParser
|
||||
$envelope = is_array($attributes['ENVELOPE'] ?? null) ? $attributes['ENVELOPE'] : null;
|
||||
$bodyStructure = isset($attributes['BODYSTRUCTURE']) ? self::parseBodyPart($attributes['BODYSTRUCTURE'], '') : null;
|
||||
$bodySections = self::parseBodySections($attributes, $bodyStructure);
|
||||
$headers = self::parseFetchedHeaders($attributes);
|
||||
|
||||
return new Message(
|
||||
$sequence,
|
||||
$uid,
|
||||
self::toOptionalInt($attributes['RFC822.SIZE'] ?? null) ?? 0,
|
||||
self::toNullableString($attributes['INTERNALDATE'] ?? null),
|
||||
self::extractReceivedAt($headers),
|
||||
self::parseFlags($attributes['FLAGS'] ?? null),
|
||||
self::decodeMimeHeader(self::envelopeString($envelope, 1)),
|
||||
self::envelopeString($envelope, 0),
|
||||
@@ -258,6 +262,127 @@ final class MessageParser
|
||||
return is_string($value) && $value !== '' ? $value : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $attributes
|
||||
* @return array<string, list<string>>
|
||||
*/
|
||||
private static function parseFetchedHeaders(array $attributes): array
|
||||
{
|
||||
$headers = [];
|
||||
|
||||
foreach ($attributes as $name => $value) {
|
||||
if (!is_string($value)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!preg_match('/^BODY(?:\.PEEK)?\[(.+)\]$/i', $name, $matches)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$section = strtoupper(trim($matches[1]));
|
||||
if (!str_starts_with($section, 'HEADER')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (self::parseHeaderBlock($value) as $headerName => $headerValues) {
|
||||
$normalized = strtolower($headerName);
|
||||
$headers[$normalized] ??= [];
|
||||
array_push($headers[$normalized], ...$headerValues);
|
||||
}
|
||||
}
|
||||
|
||||
return $headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, list<string>>
|
||||
*/
|
||||
private static function parseHeaderBlock(string $headers): array
|
||||
{
|
||||
$parsed = [];
|
||||
$currentName = null;
|
||||
$currentValue = '';
|
||||
|
||||
foreach (preg_split("/\r\n|\n|\r/", $headers) ?: [] as $line) {
|
||||
if ($line === '') {
|
||||
break;
|
||||
}
|
||||
|
||||
if (($line[0] === ' ' || $line[0] === "\t") && $currentName !== null) {
|
||||
$currentValue .= ' ' . trim($line);
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($currentName !== null) {
|
||||
$parsed[$currentName] ??= [];
|
||||
$parsed[$currentName][] = trim($currentValue);
|
||||
}
|
||||
|
||||
$separator = strpos($line, ':');
|
||||
if ($separator === false) {
|
||||
$currentName = null;
|
||||
$currentValue = '';
|
||||
continue;
|
||||
}
|
||||
|
||||
$currentName = substr($line, 0, $separator);
|
||||
$currentValue = substr($line, $separator + 1);
|
||||
}
|
||||
|
||||
if ($currentName !== null) {
|
||||
$parsed[$currentName] ??= [];
|
||||
$parsed[$currentName][] = trim($currentValue);
|
||||
}
|
||||
|
||||
return $parsed;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, list<string>> $headers
|
||||
*/
|
||||
private static function extractReceivedAt(array $headers): ?string
|
||||
{
|
||||
foreach ($headers['delivery-date'] ?? [] as $value) {
|
||||
$date = self::parseHeaderDate($value);
|
||||
if ($date !== null) {
|
||||
return $date;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($headers['received'] ?? [] as $value) {
|
||||
$date = self::parseHeaderDateFromReceived($value);
|
||||
if ($date !== null) {
|
||||
return $date;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static function parseHeaderDate(?string $value): ?string
|
||||
{
|
||||
$value = self::toNullableString($value);
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return (new \DateTimeImmutable($value))->format(DateTimeInterface::ATOM);
|
||||
} catch (\Exception) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static function parseHeaderDateFromReceived(string $value): ?string
|
||||
{
|
||||
$separator = strrpos($value, ';');
|
||||
if ($separator === false) {
|
||||
return self::parseHeaderDate($value);
|
||||
}
|
||||
|
||||
return self::parseHeaderDate(substr($value, $separator + 1));
|
||||
}
|
||||
|
||||
private static function envelopeString(?array $envelope, int $index): ?string
|
||||
{
|
||||
if ($envelope === null) {
|
||||
|
||||
Reference in New Issue
Block a user