laravel-mail-templates/src/MailTemplate.php

214 lines
4.8 KiB
PHP

<?php
namespace Bluesquare\MailTemplates;
class MailTemplate implements \JsonSerializable
{
/**
* The template for the message
*
* @var string
*/
public $templateId;
/**
* 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|null
*/
public $replyTo = null;
/**
* The parameters for the message
*
* @var array
*/
public $parameters = [];
public static function make(string $templateId)
{
return new self($templateId);
}
public function __construct(string $templateId)
{
$this->templateId = $templateId;
$this->sender(
config('mail_templates.sender.email'),
config('mail_templates.sender.name')
);
}
/**
* @param string $address
* @param string|null $name
* @return MailTemplate
*/
public function sender(string $address, string $name = null): MailTemplate
{
$this->sender['address'] = $address;
if (! empty($name))
$this->sender['name'] = $name;
return $this;
}
/**
* @param string $address
* @param string|null $name
* @return MailTemplate
*/
public function replyTo(string $address, string $name = null): MailTemplate
{
$this->replyTo['address'] = $address;
if (! empty($name))
$this->replyTo['name'] = $name;
return $this;
}
/**
* @param array $recipients
* @return MailTemplate
*/
public function recipients(array $recipients): MailTemplate
{
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 MailTemplate
*/
public function addRecipient(string $address, array $informations = []): MailTemplate
{
$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 MailTemplate
*/
public function parameters(array $parameters): MailTemplate
{
$this->parameters = $parameters;
return $this;
}
/**
* @param array $attachments
* @return MailTemplate
*/
public function attachments(array $attachments): MailTemplate
{
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)
];
return $this;
}
/**
* @param string $key
* @param string $value
* @return MailTemplate
*/
public function addParameter(string $key, string $value): MailTemplate
{
$this->parameters[$key] = $value;
return $this;
}
/**
* Get an array representation of the message.
*
* @return array
*/
public function toArray(): array
{
return [
'templateId' => $this->templateId,
'sender' => $this->sender,
'replyTo' => $this->replyTo,
'recipients' => $this->recipients,
'attachments' => $this->attachments,
'parameters' => $this->parameters,
];
}
public function jsonSerialize(): mixed
{
return $this->toArray();
}
}