laravel-mail/BMailTemplate.php

173 lines
3.7 KiB
PHP
Raw Normal View History

2020-04-09 10:50:49 +02:00
<?php
namespace BMail;
class BMailTemplate implements \JsonSerializable
{
/**
* The template for the message
*
* @var integer
*/
public $template;
/**
* The sender for the message
*
* @var array
*/
public $sender = [];
/**
* The recipients for the message
*
* @var array
*/
public $recipients = [];
/**
* The reply address for the message
*
* @var array
*/
public $replyTo = [];
/**
* The parameters for the message
*
* @var array
*/
public $parameters = [];
public function __construct(int $template)
{
$this->template = $template;
}
/**
* @param string $address
* @param string|null $name
* @return BMailTemplate
*/
public function sender(string $address, string $name = null): BMailTemplate
{
$this->sender['address'] = $address;
if ($name)
$this->sender['name'] = $name;
return $this;
}
/**
* @param string $address
* @param string|null $name
* @return BMailTemplate
*/
public function replyTo(string $address, string $name = null): BMailTemplate
{
$this->replyTo['address'] = $address;
if ($name)
$this->replyTo['name'] = $name;
return $this;
}
/**
* @param array $recipients
* @return BMailTemplate
*/
public function recipients(array $recipients): BMailTemplate
{
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 BMailTemplate
*/
public function addRecipient(string $address, array $informations = []): BMailTemplate
{
$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 BMailTemplate
*/
public function parameters(array $parameters): BMailTemplate
{
$this->parameters = $parameters;
return $this;
}
/**
* @param string $key
* @param string $value
* @return BMailTemplate
*/
public function addParameter(string $key, string $value): BMailTemplate
{
$this->parameters[$key] = $value;
return $this;
}
/**
* Get an array representation of the message.
*
* @return array
*/
public function toArray()
{
return [
'template' => $this->template,
'sender' => $this->sender,
'replyTo' => $this->replyTo,
'recipients' => $this->recipients,
'parameters' => $this->parameters,
];
}
public function jsonSerialize()
{
return $this->toArray();
}
}