generated from Nodarx/template
feat: initial version
Signed-off-by: Sebastian Krupinski <root@LAPTOP-7DVOR6NC>
This commit was merged in pull request #1.
This commit is contained in:
93
lib/Providers/CollectionProperties.php
Normal file
93
lib/Providers/CollectionProperties.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\ProviderImapMail\Providers;
|
||||
|
||||
use Gricob\IMAP\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 gricob Mailbox object.
|
||||
*
|
||||
* Total / unread counts are NOT available from a LIST response alone.
|
||||
* They must be set separately (after SELECT + SEARCH UNSEEN).
|
||||
*/
|
||||
public function fromImap(Mailbox $mailbox): static
|
||||
{
|
||||
$this->data['label'] = $mailbox->name;
|
||||
$this->data['delimiter'] = $mailbox->hierarchyDelimiter;
|
||||
$this->data['attributes'] = $mailbox->nameAttributes;
|
||||
$this->data['subscribed'] = in_array('\Subscribed', $mailbox->nameAttributes, true);
|
||||
$this->data['total'] = 0;
|
||||
$this->data['unread'] = 0;
|
||||
$this->data['rank'] = 0;
|
||||
|
||||
// Map standard IMAP role attributes
|
||||
$this->data['role'] = $this->roleFromAttributes($mailbox->nameAttributes)->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::Custom;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user