Initial commit
This commit is contained in:
79
lib/Providers/Mail/MessagePart.php
Normal file
79
lib/Providers/Mail/MessagePart.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\ProviderJmapc\Providers\Mail;
|
||||
|
||||
use KTXF\Mail\Object\MessagePartMutableAbstract;
|
||||
|
||||
class MessagePart extends MessagePartMutableAbstract {
|
||||
|
||||
/**
|
||||
* convert jmap parameters collection to message object
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array $parameters jmap parameters collection
|
||||
* @param bool $amend flag merged or replaced parameters
|
||||
*/
|
||||
public function fromJmap(array $parameters, bool $amend = false): self {
|
||||
|
||||
if ($amend) {
|
||||
// merge parameters with existing ones
|
||||
$this->data = array_merge($this->data, $parameters);
|
||||
} else {
|
||||
// replace parameters store
|
||||
$this->data = $parameters;
|
||||
}
|
||||
|
||||
// determine if parameters contains subparts
|
||||
// if subParts exist convert them to a MessagePart object
|
||||
// and remove subParts parameter
|
||||
if (is_array($this->data['subParts'])) {
|
||||
foreach ($this->data['subParts'] as $key => $entry) {
|
||||
if (is_object($entry)) {
|
||||
$entry = get_object_vars($entry);
|
||||
}
|
||||
$this->parts[$key] = (new MessagePart($parameters))->fromJmap($entry);
|
||||
}
|
||||
unset($this->data['subParts']);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* convert message object to jmap parameters array
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return array collection of all message parameters
|
||||
*/
|
||||
public function toJmap(): array {
|
||||
|
||||
// copy parameter value
|
||||
$parameters = $this->data;
|
||||
// determine if this MessagePart has any sub MessageParts
|
||||
// if sub MessageParts exist retrieve sub MessagePart parameters
|
||||
// and add them to the subParts parameters, otherwise set the subParts parameter to nothing
|
||||
if (count($this->parts) > 0) {
|
||||
$parameters['subParts'] = [];
|
||||
foreach ($this->parts as $entry) {
|
||||
if ($entry instanceof MessagePart) {
|
||||
$parameters['subParts'][] = $entry->toJmap();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$parameters['subParts'] = null;
|
||||
}
|
||||
|
||||
return $parameters;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user