Skip to content

Commit

Permalink
some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
arukompas committed Aug 25, 2023
1 parent 25acddc commit f9d2239
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
37 changes: 28 additions & 9 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Opcodes\MailParser;

class Message
class Message implements \JsonSerializable
{
protected string $message;

Expand Down Expand Up @@ -130,13 +130,13 @@ protected function parse()

foreach ($lines as $line) {
if ($headerInProgress) {
$this->headers[$headerInProgress] .= "\n" . $line;
$this->headers[$headerInProgress] .= "\n" . rtrim($line);
$headerInProgress = str_ends_with($line, ';');
continue;
}

if ($currentBodyHeaderInProgress) {
$currentBodyHeaders[$currentBodyHeaderInProgress] .= "\n" . $line;
$currentBodyHeaders[$currentBodyHeaderInProgress] .= "\n" . rtrim($line);
$currentBodyHeaderInProgress = str_ends_with($line, ';');
continue;
}
Expand All @@ -160,18 +160,18 @@ protected function parse()
}

if ($collectingBody && preg_match('/^(?<key>[A-Za-z\-0-9]+): (?<value>.*)$/', $line, $matches)) {
$currentBodyHeaders[$matches['key']] = $matches['value'];
$currentBodyHeaders[$matches['key']] = rtrim($matches['value']);

// if the last character is a semicolon, then the header is continued on the next line
if (str_ends_with($matches['value'], ';')) {
if (str_ends_with($currentBodyHeaders[$matches['key']], ';')) {
$currentBodyHeaderInProgress = $matches['key'];
}

continue;
}

if ($collectingBody) {
$currentBody .= $line."\n";
$currentBody .= rtrim($line)."\n";
continue;
}

Expand All @@ -182,11 +182,11 @@ protected function parse()
}

if (preg_match('/^(?<key>[A-Za-z\-0-9]+): (?<value>.*)$/', $line, $matches)) {
$this->headers[$matches['key']] = $matches['value'];
$this->headers[$matches['key']] = rtrim($matches['value']);

// if the last character is a semicolon, then the header is continued on the next line
if (str_ends_with($matches['value'], ';')) {
$headerInProgress = $matches['key'];
if (str_ends_with($this->headers[$matches['key']], ';')) {
$headerInProgress = rtrim($matches['key']);
}

continue;
Expand All @@ -198,4 +198,23 @@ protected function addPart(string $currentBody, array $currentBodyHeaders): void
{
$this->parts[] = new MessagePart(trim($currentBody), $currentBodyHeaders);
}

public function toArray(): array
{
return [
'id' => $this->getId(),
'subject' => $this->getSubject(),
'from' => $this->getFrom(),
'to' => $this->getTo(),
'reply_to' => $this->getReplyTo(),
'date' => $this->getDate() ? $this->getDate()->format('c') : null,
'headers' => $this->getHeaders(),
'parts' => array_map(fn ($part) => $part->toArray(), $this->getParts()),
];
}

public function jsonSerialize(): mixed
{
return $this->toArray();
}
}
11 changes: 10 additions & 1 deletion src/MessagePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function isImage(): bool

public function isAttachment(): bool
{
return str_starts_with($this->getHeader('Content-Disposition'), 'attachment');
return str_starts_with($this->getHeader('Content-Disposition', ''), 'attachment');
}

public function getFilename(): string
Expand All @@ -70,4 +70,13 @@ public function getFilename(): string

return '';
}

public function toArray(): array
{
return [
'headers' => $this->getHeaders(),
'content' => $this->getContent(),
'filename' => $this->getFilename(),
];
}
}
3 changes: 3 additions & 0 deletions tests/Unit/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,7 @@
expect($attachmentPart->getContent())->toBe('This is a test string')
->and($attachmentPart->isAttachment())->toBe(true)
->and($attachmentPart->getFilename())->toBe('test.txt');

$attachments = $message->getAttachments();
expect($attachments)->toHaveCount(1);
});

0 comments on commit f9d2239

Please sign in to comment.