diff --git a/readme.md b/readme.md index 264e165..905972f 100755 --- a/readme.md +++ b/readme.md @@ -388,9 +388,11 @@ The `Message` class provides several methods to access basic properties: **Headers and Contents** -- `$message->headers()`: Returns the raw headers as a string. -- `$message->contents()`: Returns the raw message content. -- `$message->hasHeaders()` / `hasContents()`: Determine whether the message has headers or contents. +- `$message->head()`: Returns the raw message header. +- `$message->body()`: Returns the raw message body. +- `$message->header($name)`: Returns a specific header. +- `$message->headers()`: Returns an array of all headers. +- `$message->hasHead()` / `hasBody()`: Determine whether the message has headers or body. **Metadata** diff --git a/src/Address.php b/src/Address.php index 3552344..153bcea 100644 --- a/src/Address.php +++ b/src/Address.php @@ -2,7 +2,10 @@ namespace DirectoryTree\ImapEngine; -class Address +use Illuminate\Contracts\Support\Arrayable; +use JsonSerializable; + +class Address implements Arrayable, JsonSerializable { /** * Constructor. @@ -27,4 +30,23 @@ public function name(): string { return $this->name; } + + /** + * Get the array representation of the address. + */ + public function toArray(): array + { + return [ + 'email' => $this->email, + 'name' => $this->name, + ]; + } + + /** + * Get the JSON representation of the address. + */ + public function jsonSerialize(): array + { + return $this->toArray(); + } } diff --git a/src/Attachment.php b/src/Attachment.php index 9967190..41f260e 100644 --- a/src/Attachment.php +++ b/src/Attachment.php @@ -2,10 +2,12 @@ namespace DirectoryTree\ImapEngine; +use Illuminate\Contracts\Support\Arrayable; +use JsonSerializable; use Psr\Http\Message\StreamInterface; use Symfony\Component\Mime\MimeTypes; -class Attachment +class Attachment implements Arrayable, JsonSerializable { /** * Constructor. @@ -71,4 +73,24 @@ public function extension(): ?string return null; } + + /** + * Get the array representation of the attachment. + */ + public function toArray(): array + { + return [ + 'filename' => $this->filename, + 'content_type' => $this->contentType, + 'contents' => $this->contents(), + ]; + } + + /** + * Get the JSON representation of the attachment. + */ + public function jsonSerialize(): array + { + return $this->toArray(); + } } diff --git a/src/Folder.php b/src/Folder.php index a4d25e8..23ed5ff 100644 --- a/src/Folder.php +++ b/src/Folder.php @@ -7,8 +7,10 @@ use DirectoryTree\ImapEngine\Enums\ImapFetchIdentifier; use DirectoryTree\ImapEngine\Exceptions\Exception; use DirectoryTree\ImapEngine\Exceptions\ImapCapabilityException; +use Illuminate\Contracts\Support\Arrayable; +use JsonSerializable; -class Folder +class Folder implements Arrayable, JsonSerializable { /** * The folder's cached capabilities. @@ -206,4 +208,24 @@ protected function capabilities(): array { return $this->capabilities ??= $this->mailbox->capabilities(); } + + /** + * Get the array representation of the folder. + */ + public function toArray(): array + { + return [ + 'path' => $this->path, + 'flags' => $this->flags, + 'delimiter' => $this->delimiter, + ]; + } + + /** + * Get the JSON representation of the folder. + */ + public function jsonSerialize(): array + { + return $this->toArray(); + } } diff --git a/src/Message.php b/src/Message.php index 176ae56..af53876 100644 --- a/src/Message.php +++ b/src/Message.php @@ -30,8 +30,8 @@ public function __construct( protected Folder $folder, protected int $uid, protected array $flags, - protected string $headers, - protected string $contents, + protected string $head, + protected string $body, ) {} /** @@ -62,33 +62,33 @@ public function flags(): array /** * Get the message's raw headers. */ - public function headers(): string + public function head(): string { - return $this->headers; + return $this->head; } /** * Determine if the message has headers. */ - public function hasHeaders(): bool + public function hasHead(): bool { - return ! empty($this->headers); + return ! empty($this->head); } /** - * Get the message's raw contents. + * Get the message's raw body. */ - public function contents(): string + public function body(): string { - return $this->contents; + return $this->body; } /** * Determine if the message has contents. */ - public function hasContents(): bool + public function hasBody(): bool { - return ! empty($this->contents); + return ! empty($this->body); } /** @@ -297,6 +297,14 @@ public function hasFlag(BackedEnum|string $flag): bool return in_array(Str::prefix(Str::enum($flag), '\\'), $this->flags); } + /** + * Get all headers from the message. + */ + public function headers(): array + { + return $this->parse()->getAllHeaders(); + } + /** * Get a header from the message. */ @@ -483,7 +491,7 @@ public function restore(): void */ public function parse(): MailMimeMessage { - if (! $this->hasHeaders() && ! $this->hasContents()) { + if (! $this->hasHead() && ! $this->hasBody()) { throw new RuntimeException('Cannot parse an empty message'); } @@ -498,8 +506,8 @@ public function toArray(): array return [ 'uid' => $this->uid, 'flags' => $this->flags, - 'headers' => $this->headers, - 'contents' => $this->contents, + 'head' => $this->head, + 'body' => $this->body, ]; } @@ -509,8 +517,8 @@ public function toArray(): array public function __toString(): string { return implode("\r\n\r\n", array_filter([ - rtrim($this->headers), - ltrim($this->contents), + rtrim($this->head), + ltrim($this->body), ])); } diff --git a/src/Pagination/LengthAwarePaginator.php b/src/Pagination/LengthAwarePaginator.php index 968944f..421b08a 100644 --- a/src/Pagination/LengthAwarePaginator.php +++ b/src/Pagination/LengthAwarePaginator.php @@ -129,7 +129,7 @@ public function previousPageUrl(): ?string } /** - * Convert the pagination data to an array. + * Get the array representation of the paginator. */ public function toArray(): array { @@ -164,7 +164,7 @@ protected function calculateTo(): ?int } /** - * Convert the instance to JSON. + * Get the JSON representation of the paginator. */ public function jsonSerialize(): array {