generated from Nodarx/template
feat: initial version
Signed-off-by: Sebastian Krupinski <root@LAPTOP-7DVOR6NC>
This commit was merged in pull request #1.
This commit is contained in:
56
lib/Client/Mime/LazyMessage.php
Normal file
56
lib/Client/Mime/LazyMessage.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Mime;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Gricob\IMAP\Client;
|
||||
use Gricob\IMAP\Mime\Part\Part;
|
||||
|
||||
class LazyMessage extends Message
|
||||
{
|
||||
public function __construct(
|
||||
private Client $client,
|
||||
int $id,
|
||||
?array $headers = null,
|
||||
?DateTimeImmutable $internalDate = null,
|
||||
) {
|
||||
$this->id = $id;
|
||||
|
||||
if (null !== $headers) {
|
||||
$this->headers = $headers;
|
||||
}
|
||||
|
||||
if (null !== $internalDate) {
|
||||
$this->internalDate = $internalDate;
|
||||
}
|
||||
}
|
||||
|
||||
public function headers(): array
|
||||
{
|
||||
if (!isset($this->headers)) {
|
||||
$this->headers = $this->client->fetchHeaders($this->id);
|
||||
}
|
||||
|
||||
return parent::headers();
|
||||
}
|
||||
|
||||
public function body(): Part
|
||||
{
|
||||
if (!isset($this->body)) {
|
||||
$this->body = $this->client->fetchBody($this->id);
|
||||
}
|
||||
|
||||
return parent::body();
|
||||
}
|
||||
|
||||
public function internalDate(): DateTimeImmutable
|
||||
{
|
||||
if (!isset($this->internalDate)) {
|
||||
$this->internalDate = $this->client->fetchInternalDate($this->id);
|
||||
}
|
||||
|
||||
return parent::internalDate();
|
||||
}
|
||||
}
|
||||
55
lib/Client/Mime/Message.php
Normal file
55
lib/Client/Mime/Message.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Mime;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Gricob\IMAP\Mime\Part\Part;
|
||||
|
||||
class Message
|
||||
{
|
||||
/**
|
||||
* @param array<string, string> $headers
|
||||
*/
|
||||
public function __construct(
|
||||
protected int $id,
|
||||
protected array $headers,
|
||||
protected Part $body,
|
||||
protected DateTimeImmutable $internalDate,
|
||||
) {
|
||||
}
|
||||
|
||||
public function id(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function headers(): array
|
||||
{
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
public function body(): Part
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
public function internalDate(): DateTimeImmutable
|
||||
{
|
||||
return $this->internalDate;
|
||||
}
|
||||
|
||||
public function textBody(): ?string
|
||||
{
|
||||
return $this->body()->findPartByMimeType('text/plain')?->decodedBody();
|
||||
}
|
||||
|
||||
public function htmlBody(): ?string
|
||||
{
|
||||
return $this->body()->findPartByMimeType('text/html')?->decodedBody();
|
||||
}
|
||||
}
|
||||
20
lib/Client/Mime/Part/Body.php
Normal file
20
lib/Client/Mime/Part/Body.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Mime\Part;
|
||||
|
||||
use Stringable;
|
||||
|
||||
class Body implements Stringable
|
||||
{
|
||||
public function __construct(
|
||||
protected string $value
|
||||
) {
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
14
lib/Client/Mime/Part/Disposition.php
Normal file
14
lib/Client/Mime/Part/Disposition.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Mime\Part;
|
||||
|
||||
final readonly class Disposition
|
||||
{
|
||||
public function __construct(
|
||||
public string $type,
|
||||
public ?string $filename,
|
||||
) {
|
||||
}
|
||||
}
|
||||
26
lib/Client/Mime/Part/LazyBody.php
Normal file
26
lib/Client/Mime/Part/LazyBody.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Mime\Part;
|
||||
|
||||
use Gricob\IMAP\Client;
|
||||
|
||||
class LazyBody extends Body
|
||||
{
|
||||
public function __construct(
|
||||
private Client $client,
|
||||
private int $id,
|
||||
private string $section,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
if (!isset($this->value)) {
|
||||
$this->value = $this->client->fetchSectionBody($this->id, $this->section);
|
||||
}
|
||||
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
31
lib/Client/Mime/Part/MultiPart.php
Normal file
31
lib/Client/Mime/Part/MultiPart.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Mime\Part;
|
||||
|
||||
final readonly class MultiPart extends Part
|
||||
{
|
||||
/**
|
||||
* @param array<string,string> $attributes
|
||||
* @param list<Part> $parts
|
||||
*/
|
||||
public function __construct(
|
||||
string $subtype,
|
||||
array $attributes,
|
||||
public array $parts,
|
||||
) {
|
||||
parent::__construct('multipart', $subtype, $attributes);
|
||||
}
|
||||
|
||||
public function findPartByMimeType(string $mimeType): ?SinglePart
|
||||
{
|
||||
foreach ($this->parts as $part) {
|
||||
if ($matchedPart = $part->findPartByMimeType(strtolower($mimeType))) {
|
||||
return $matchedPart;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
36
lib/Client/Mime/Part/Part.php
Normal file
36
lib/Client/Mime/Part/Part.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Mime\Part;
|
||||
|
||||
abstract readonly class Part
|
||||
{
|
||||
public string $type;
|
||||
public string $subtype;
|
||||
|
||||
/**
|
||||
* @var array<string, string>
|
||||
*/
|
||||
public array $attributes;
|
||||
|
||||
/**
|
||||
* @param array<string,string> $attributes
|
||||
*/
|
||||
public function __construct(
|
||||
string $type,
|
||||
string $subtype,
|
||||
array $attributes,
|
||||
) {
|
||||
$this->subtype = strtolower($subtype);
|
||||
$this->type = strtolower($type);
|
||||
$this->attributes = $attributes;
|
||||
}
|
||||
|
||||
abstract public function findPartByMimeType(string $mimeType): ?SinglePart;
|
||||
|
||||
public function mimeType(): string
|
||||
{
|
||||
return $this->type.'/'.$this->subtype;
|
||||
}
|
||||
}
|
||||
62
lib/Client/Mime/Part/SinglePart.php
Normal file
62
lib/Client/Mime/Part/SinglePart.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Mime\Part;
|
||||
|
||||
final readonly class SinglePart extends Part
|
||||
{
|
||||
private string $encoding;
|
||||
|
||||
public function __construct(
|
||||
string $type,
|
||||
string $subtype,
|
||||
array $attributes,
|
||||
private Body $body,
|
||||
private string $charset,
|
||||
string $encoding,
|
||||
private ?Disposition $disposition,
|
||||
) {
|
||||
|
||||
$this->encoding = strtolower($encoding);
|
||||
parent::__construct($type, $subtype, $attributes);
|
||||
}
|
||||
|
||||
public function body(): string
|
||||
{
|
||||
return (string) $this->body;
|
||||
}
|
||||
|
||||
public function decodedBody(): string
|
||||
{
|
||||
return match ($this->encoding) {
|
||||
'quoted-printable' => quoted_printable_decode($this->body()),
|
||||
'base64' => base64_decode($this->body()),
|
||||
default => $this->body(),
|
||||
};
|
||||
}
|
||||
|
||||
public function charset(): string
|
||||
{
|
||||
return $this->charset;
|
||||
}
|
||||
|
||||
public function encoding(): string
|
||||
{
|
||||
return $this->encoding;
|
||||
}
|
||||
|
||||
public function disposition(): ?Disposition
|
||||
{
|
||||
return $this->disposition;
|
||||
}
|
||||
|
||||
public function findPartByMimeType(string $mimeType): ?SinglePart
|
||||
{
|
||||
if ($this->mimeType() === strtolower($mimeType)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user