Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
24 changes: 23 additions & 1 deletion src/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace DirectoryTree\ImapEngine;

class Address
use Illuminate\Contracts\Support\Arrayable;
use JsonSerializable;

class Address implements Arrayable, JsonSerializable
{
/**
* Constructor.
Expand All @@ -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();
}
}
24 changes: 23 additions & 1 deletion src/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}
}
24 changes: 23 additions & 1 deletion src/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}
}
40 changes: 24 additions & 16 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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');
}

Expand All @@ -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,
];
}

Expand All @@ -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),
]));
}

Expand Down
4 changes: 2 additions & 2 deletions src/Pagination/LengthAwarePaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -164,7 +164,7 @@ protected function calculateTo(): ?int
}

/**
* Convert the instance to JSON.
* Get the JSON representation of the paginator.
*/
public function jsonSerialize(): array
{
Expand Down
Loading