* SPDX-License-Identifier: AGPL-3.0-or-later */ namespace KTXM\ProviderImap\Providers; use DateTimeInterface; use KTXM\ProviderImap\Client\Message; 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 IMAP data to a mail entity object. */ public function fromImap(Message $message, string $mailbox): static { $this->data['collection'] = $mailbox; $this->data['identifier'] = $message->uid() ?: $message->sequence(); if ($message->internalDate() !== null) { $this->data['created'] = $message->internalDate(); } $this->getProperties()->fromImap($message); 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; } }