Files
provider_imap/lib/Client/Command/Result/MessageTransferResult.php
2026-05-08 00:16:43 -04:00

152 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
namespace KTXM\ProviderImap\Client\Command\Result;
final class MessageTransferResult
{
/**
* @param list<array{source:string, name:string, arguments:list<string>, text:string}> $responseCodes
* @param ?array{uidValidity:string, sourceUids:string, destinationUids:string} $copyUid
* @param list<int> $expunged
* @param list<array{earlier:bool, knownUids:string}> $vanished
*/
public function __construct(
private readonly string $status,
private readonly string $text,
private readonly array $responseCodes = [],
private readonly ?array $copyUid = null,
private readonly bool $tryCreate = false,
private readonly ?string $highestModSeq = null,
private readonly array $expunged = [],
private readonly array $vanished = [],
) {}
public function status(): string
{
return $this->status;
}
public function text(): string
{
return $this->text;
}
public function isOk(): bool
{
return $this->status === 'OK';
}
/**
* @return list<array{source:string, name:string, arguments:list<string>, text:string}>
*/
public function responseCodes(): array
{
return $this->responseCodes;
}
/**
* @return ?array{uidValidity:string, sourceUids:string, destinationUids:string}
*/
public function copyUid(): ?array
{
return $this->copyUid;
}
/**
* @return array<string,string>
*/
public function copyUidMap(): array
{
if ($this->copyUid === null) {
return [];
}
$sourceUids = $this->expandUidSet($this->copyUid['sourceUids']);
$destinationUids = $this->expandUidSet($this->copyUid['destinationUids']);
if (count($sourceUids) !== count($destinationUids)) {
return [];
}
$mapping = [];
foreach ($sourceUids as $index => $sourceUid) {
$mapping[$sourceUid] = $destinationUids[$index];
}
return $mapping;
}
public function tryCreate(): bool
{
return $this->tryCreate;
}
public function highestModSeq(): ?string
{
return $this->highestModSeq;
}
/**
* @return list<int>
*/
public function expunged(): array
{
return $this->expunged;
}
/**
* @return list<array{earlier:bool, knownUids:string}>
*/
public function vanished(): array
{
return $this->vanished;
}
public function hasResponseCode(string $name): bool
{
$name = strtoupper(trim($name));
foreach ($this->responseCodes as $responseCode) {
if ($responseCode['name'] === $name) {
return true;
}
}
return false;
}
/**
* @return list<string>
*/
private function expandUidSet(string $value): array
{
$expanded = [];
foreach (explode(',', $value) as $segment) {
$segment = trim($segment);
if ($segment === '') {
continue;
}
if (!str_contains($segment, ':')) {
$expanded[] = $segment;
continue;
}
[$start, $end] = array_map('trim', explode(':', $segment, 2));
if (!ctype_digit($start) || !ctype_digit($end)) {
return [];
}
$range = range((int) $start, (int) $end, (int) $start <= (int) $end ? 1 : -1);
foreach ($range as $uid) {
$expanded[] = (string) $uid;
}
}
return $expanded;
}
}