* SPDX-License-Identifier: AGPL-3.0-or-later */ namespace KTXM\ProviderImapMail\Providers; use DateTimeInterface; use Gricob\IMAP\Mime\Part\Part; use Gricob\IMAP\Protocol\Response\Line\Data\FetchData; use KTXF\Mail\Entity\EntityMutableAbstract; /** * Mail Entity Resource Implementation */ class EntityResource extends EntityMutableAbstract { public function __construct( string $provider = 'imap', string|int|null $service = null, ) { parent::__construct($provider, $service); } /** * Convert gricob FetchData to mail entity object * * @param FetchData $fetchData result from IMAP FETCH command * @param string $mailbox IMAP mailbox name (used as collection) * @param Part|null $bodyPart MIME Part tree for body content (optional) */ public function fromImap(FetchData $fetchData, string $mailbox, ?Part $bodyPart = null): static { // Collection = the IMAP mailbox name $this->data['collection'] = $mailbox; // Identifier = UID (preferred) or sequence number as fallback $this->data['identifier'] = $fetchData->uid ?? $fetchData->id; // Created = INTERNALDATE (server arrival time) if ($fetchData->internalDate !== null) { $this->data['created'] = $fetchData->internalDate->format(DateTimeInterface::ATOM); } $this->getProperties()->fromImap( flags: $fetchData->flags ?? [], envelope: $fetchData->envelope, bodyStructure: $fetchData->bodyStructure, size: $fetchData->rfc822Size ?? 0, bodyPart: $bodyPart, ); return $this; } /** * Convert mail entity object to store array */ public function toStore(): array { return array_merge( $this->data, ['properties' => $this->getProperties()->toStore()] ); } /** * Hydrate mail entity object from store array */ public function fromStore(array $data): static { $properties = $data['properties'] ?? []; unset($data['properties']); $this->data = $data; $this->getProperties()->fromStore($properties); return $this; } /** * @inheritDoc */ public function getProperties(): MessageProperties { if (!isset($this->properties)) { $this->properties = new MessageProperties([]); } return $this->properties; } }