Files
provider_imap/lib/Providers/CollectionProperties.php
2026-05-08 00:16:43 -04:00

99 lines
3.4 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\Mailbox;
use KTXF\Mail\Collection\CollectionPropertiesMutableAbstract;
use KTXF\Mail\Collection\CollectionRoles;
/**
* IMAP Mail Collection Properties
*
* Backed by the same internal $data shape as the JMAP provider so that cache
* documents are interchangeable with fromStore() / toStore().
*/
class CollectionProperties extends CollectionPropertiesMutableAbstract
{
// ── IMAP hydration ───────────────────────────────────────────────────────
/**
* Populate from a standalone IMAP Mailbox value object.
*
* Total / unread counts are available when the caller uses LIST-STATUS.
*/
public function fromImap(Mailbox $mailbox, array $options = []): static
{
$delimiter = $mailbox->delimiter() ?? '';
$name = $mailbox->name();
$attributes = $mailbox->attributes();
$this->data['label'] = ($delimiter !== '' && str_contains($name, $delimiter))
? substr($name, strrpos($name, $delimiter) + strlen($delimiter))
: $name;
$this->data['delimiter'] = $delimiter;
$this->data['attributes'] = $attributes;
$this->data['subscribed'] = in_array('\\SUBSCRIBED', $attributes, true) || in_array('\\Subscribed', $attributes, true);
$this->data['total'] = $mailbox->messages();
$this->data['unread'] = $mailbox->unread();
$this->data['rank'] = 0;
// Map standard IMAP role attributes
$this->data['role'] = $this->roleFromAttributes($attributes)->value;
return $this;
}
// ── Store (MongoDB cache) ────────────────────────────────────────────────
public function toStore(): array
{
return $this->data;
}
public function fromStore(array $data): static
{
$this->data = $data;
return $this;
}
// ── JSON helpers ─────────────────────────────────────────────────────────
public function jsonSerialize(): array
{
return $this->data;
}
public function getDelimiter(): ?string
{
return $this->data['delimiter'] ?? null;
}
// ── Helpers ───────────────────────────────────────────────────────────────
private function roleFromAttributes(array $attributes): CollectionRoles
{
foreach ($attributes as $attr) {
$lower = strtolower($attr);
$role = match ($lower) {
'\sent' => CollectionRoles::Sent,
'\trash' => CollectionRoles::Trash,
'\drafts' => CollectionRoles::Drafts,
'\junk' => CollectionRoles::Junk,
'\archive' => CollectionRoles::Archive,
default => null,
};
if ($role !== null) {
return $role;
}
}
return CollectionRoles::None;
}
}