202 lines
4.6 KiB
PHP
202 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace Bluescale\Mail;
|
|
|
|
class BluescaleMailTemplate implements \JsonSerializable
|
|
{
|
|
/**
|
|
* The template for the message
|
|
*
|
|
* @var string
|
|
*/
|
|
public $template_id;
|
|
|
|
/**
|
|
* The sender for the message
|
|
*
|
|
* @var array
|
|
*/
|
|
public $sender = [];
|
|
|
|
/**
|
|
* The recipients for the message
|
|
*
|
|
* @var array
|
|
*/
|
|
public $recipients = [];
|
|
|
|
/**
|
|
* The attachments for the message
|
|
*
|
|
* @var array
|
|
*/
|
|
public $attachments = [];
|
|
|
|
/**
|
|
* The reply address for the message
|
|
*
|
|
* @var array
|
|
*/
|
|
public $replyTo = [];
|
|
|
|
/**
|
|
* The parameters for the message
|
|
*
|
|
* @var array
|
|
*/
|
|
public $parameters = [];
|
|
|
|
public function __construct(string $template_id)
|
|
{
|
|
$this->template_id = $template_id;
|
|
}
|
|
|
|
/**
|
|
* @param string $address
|
|
* @param string|null $name
|
|
* @return BluescaleMailTemplate
|
|
*/
|
|
public function sender(string $address, string $name = null): BluescaleMailTemplate
|
|
{
|
|
$this->sender['address'] = $address;
|
|
|
|
if ($name)
|
|
$this->sender['name'] = $name;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @param string $address
|
|
* @param string|null $name
|
|
* @return BluescaleMailTemplate
|
|
*/
|
|
public function replyTo(string $address, string $name = null): BluescaleMailTemplate
|
|
{
|
|
$this->replyTo['address'] = $address;
|
|
|
|
if ($name)
|
|
$this->replyTo['name'] = $name;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @param array $recipients
|
|
* @return BluescaleMailTemplate
|
|
*/
|
|
public function recipients(array $recipients): BluescaleMailTemplate
|
|
{
|
|
if (!empty($recipients)) {
|
|
foreach ($recipients as $recipient) {
|
|
if (!empty($recipient[0]) && is_string($recipient[0])) {
|
|
$data = [
|
|
'address' => $recipient[0]
|
|
];
|
|
|
|
if (!empty($recipient[1])) {
|
|
if (!empty($recipient[1]['name']))
|
|
$data['name'] = $recipient[1]['name'];
|
|
|
|
if (!empty($recipient[1]['parameters']))
|
|
$data['parameters'] = $recipient[1]['parameters'];
|
|
}
|
|
|
|
$this->recipients[] = $data;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @param string $address
|
|
* @param array $informations
|
|
* @return BluescaleMailTemplate
|
|
*/
|
|
public function addRecipient(string $address, array $informations = []): BluescaleMailTemplate
|
|
{
|
|
$data = [
|
|
'address' => $address
|
|
];
|
|
|
|
if (!empty($informations)) {
|
|
if (!empty($informations['name']))
|
|
$data['name'] = $informations['name'];
|
|
|
|
if (!empty($informations['parameters']))
|
|
$data['parameters'] = $informations['parameters'];
|
|
}
|
|
|
|
$this->recipients[] = $data;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @param array $parameters
|
|
* @return BluescaleMailTemplate
|
|
*/
|
|
public function parameters(array $parameters): BluescaleMailTemplate
|
|
{
|
|
$this->parameters = $parameters;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @param array $attachments
|
|
* @return BluescaleMailTemplate
|
|
*/
|
|
public function attachments(array $attachments): BluescaleMailTemplate
|
|
{
|
|
foreach ($attachments as $attachment) {
|
|
$this->addAttachment($attachment['path'] ?? $attachment['file'], $attachment['filename'] ?? null);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function addAttachment($file, $filename = null)
|
|
{
|
|
$file = is_object($file) && method_exists($file, 'getRealPath') ? $file->getRealPath() : $file;
|
|
|
|
$this->attachments[] = [
|
|
'path' => $file,
|
|
'filename' => $filename ?? basename($file)
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param string $key
|
|
* @param string $value
|
|
* @return BluescaleMailTemplate
|
|
*/
|
|
public function addParameter(string $key, string $value): BluescaleMailTemplate
|
|
{
|
|
$this->parameters[$key] = $value;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get an array representation of the message.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function toArray(): array
|
|
{
|
|
return [
|
|
'template_id' => $this->template_id,
|
|
'sender' => $this->sender,
|
|
'replyTo' => $this->replyTo,
|
|
'recipients' => $this->recipients,
|
|
'attachments' => $this->attachments,
|
|
'parameters' => $this->parameters,
|
|
];
|
|
}
|
|
|
|
public function jsonSerialize()
|
|
{
|
|
return $this->toArray();
|
|
}
|
|
}
|