> */ final class FetchManyCommand implements CommandInterface { private readonly MessageTarget $target; private readonly FetchOptions $options; public function __construct(MessageTarget|string|SequenceSet|null $target = null, ?FetchOptions $options = null) { $this->target = match (true) { $target instanceof MessageTarget => $target, $target instanceof SequenceSet => MessageTarget::sequence($target), is_string($target) => MessageTarget::sequence($target), default => MessageTarget::all(), }; $this->options = $options ?? FetchOptions::default(); } public function name(): string { return 'FETCH'; } public function allowedStates(): array { return [SessionState::Selected]; } public function encode(string $tag, SessionContext $context): RequestFrame { unset($tag, $context); return new RequestFrame(sprintf( '%s %s (%s)', $this->target->identifierMode() === IdentifierMode::Uid ? 'UID FETCH' : 'FETCH', $this->target->sequenceSet()->toCommand(), $this->options->toCommand(), )); } public function handle(ResponseStream $responses, SessionContext $context): Generator { if ($context->selectedMailbox() === null) { throw new ImapException('FETCH requires a selected mailbox.'); } return (new FetchResponseParser())->parseMany($responses); } }