Files
provider_imap/lib/Providers/EntityResource.php
T
2026-08-25 21:45:32 -04:00

76 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderImap\Providers;
use KTXM\ProviderImap\Client\Message;
use KTXF\Mail\Entity\EntityMutableAbstract;
use KTXF\Mail\Object\MessagePropertiesMutableInterface;
/**
* 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;
}
/**
* Populate an entity returned by a create or replacement operation.
*/
public function fromMutation(
string $mailbox,
string|int $identifier,
MessagePropertiesMutableInterface $properties,
): static {
$this->data['collection'] = $mailbox;
$this->data['identifier'] = $identifier;
if ($properties instanceof MessageProperties) {
$this->setProperties($properties);
} else {
$this->getProperties()->jsonDeserialize($properties->jsonSerialize());
}
return $this;
}
/**
* @inheritDoc
*/
public function getProperties(): MessageProperties {
if (!isset($this->properties)) {
$this->properties = new MessageProperties([]);
}
return $this->properties;
}
}