generated from Nodarx/template
refactor: message properties
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -17,6 +17,24 @@ use KTXM\ProviderImap\Client\MessagePart as ImapMessagePart;
|
||||
*/
|
||||
class MessagePart extends MessagePartMutableAbstract {
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $data
|
||||
*/
|
||||
private function hydrateArray(array $data): static {
|
||||
$this->data = $data;
|
||||
|
||||
if (isset($this->data['subParts']) && is_array($this->data['subParts'])) {
|
||||
foreach ($this->data['subParts'] as $entry) {
|
||||
if (is_array($entry)) {
|
||||
$this->parts[] = (new self())->hydrateArray($entry);
|
||||
}
|
||||
}
|
||||
unset($this->data['subParts']);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert gricob BodyStructure part to message part object
|
||||
*
|
||||
@@ -24,82 +42,11 @@ class MessagePart extends MessagePartMutableAbstract {
|
||||
* @param string $partId numeric part identifier (e.g. "1", "1.1", "2")
|
||||
*/
|
||||
public function fromImap(ImapMessagePart $part, string $partId = '1'): static {
|
||||
$data = $part->toArray();
|
||||
$data['partId'] = $partId;
|
||||
$data['blobId'] = $data['blobId'] ?? $partId;
|
||||
|
||||
$this->data['partId'] = $partId;
|
||||
|
||||
if ($part instanceof SinglePart) {
|
||||
$mimeType = strtolower($part->type) . '/' . strtolower($part->subtype);
|
||||
$this->data['type'] = $mimeType;
|
||||
|
||||
if ($part->id !== null) {
|
||||
$this->data['blobId'] = trim($part->id, '<>');
|
||||
}
|
||||
|
||||
// Content-Type parameters (name, charset, etc.)
|
||||
if (!empty($part->attributes)) {
|
||||
foreach ($part->attributes as $key => $value) {
|
||||
$keyLower = strtolower($key);
|
||||
if ($keyLower === 'name') {
|
||||
$this->data['name'] = $value;
|
||||
} elseif ($keyLower === 'charset') {
|
||||
$this->data['charset'] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($part->encoding !== null) {
|
||||
$this->data['encoding'] = strtolower($part->encoding);
|
||||
}
|
||||
|
||||
if ($part->size !== null) {
|
||||
$this->data['size'] = $part->size;
|
||||
}
|
||||
|
||||
if ($part->disposition !== null) {
|
||||
$this->data['disposition'] = strtolower($part->disposition->type);
|
||||
// disposition filename attribute
|
||||
if (!empty($part->disposition->attributes)) {
|
||||
foreach ($part->disposition->attributes as $key => $value) {
|
||||
if (strtolower($key) === 'filename') {
|
||||
$this->data['name'] = $this->data['name'] ?? $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($part->language)) {
|
||||
$this->data['language'] = implode(',', $part->language);
|
||||
}
|
||||
|
||||
if ($part->location !== null) {
|
||||
$this->data['location'] = $part->location;
|
||||
}
|
||||
|
||||
} elseif ($part instanceof MultiPart) {
|
||||
$this->data['type'] = 'multipart/' . strtolower($part->subtype);
|
||||
|
||||
if ($part->disposition !== null) {
|
||||
$this->data['disposition'] = strtolower($part->disposition->type);
|
||||
}
|
||||
|
||||
if (!empty($part->language)) {
|
||||
$this->data['language'] = implode(',', $part->language);
|
||||
}
|
||||
|
||||
if ($part->location !== null) {
|
||||
$this->data['location'] = $part->location;
|
||||
}
|
||||
|
||||
// Recursively process sub-parts
|
||||
// When this part has no section ID (root multipart) children are
|
||||
// numbered "1", "2", … to match IMAP section numbering.
|
||||
foreach ($part->parts as $index => $subPart) {
|
||||
$subPartId = ($partId === '') ? (string)($index + 1) : $partId . '.' . ($index + 1);
|
||||
$this->parts[] = (new MessagePart())->fromImap($subPart, $subPartId);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
return $this->hydrateArray($data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user