Files
laravel-push/src/PushTemplate.php
T
2026-09-25 12:09:33 +02:00

305 lines
6.4 KiB
PHP

<?php
namespace Bluesquare\Push;
class PushTemplate 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 = [];
/**
* @var bool Whether the message should not be sent
*/
public $test_mode = false;
/**
* @var bool Mass mailing
*/
public $bulk_mode = false;
/**
* @var string|null Template's locale
*/
public $locale = null;
/**
* @var string|null Group unsubscribe URL
*/
public $unsubscribe_url = null;
/**
* @param string $template_id
* @return PushTemplate
*/
public static function make(string $template_id)
{
return new self($template_id);
}
public function __construct(string $template_id)
{
$this->template_id = $template_id;
}
/**
* @param string $email
* @param string|null $name
* @return $this
*/
public function sender(string $email, ?string $name = null): PushTemplate
{
$this->sender['email'] = $email;
if ($name)
$this->sender['name'] = $name;
return $this;
}
/**
* @param string $email
* @param string|null $name
* @return $this
*/
public function replyTo(string $email, ?string $name = null): PushTemplate
{
$this->replyTo['email'] = $email;
if ($name)
$this->replyTo['name'] = $name;
return $this;
}
/**
* @param array $recipients
* @return $this
*/
public function recipients(array $recipients): PushTemplate
{
if (! empty($recipients)) {
foreach ($recipients as $recipient) {
if (empty($recipient['email'])) {
continue;
}
$data = [
'email' => $recipient['email']
];
if (! empty($recipient['name'])) {
$data['name'] = $recipient['name'];
}
if (! empty($recipient['parameters']) && is_array($recipient['parameters'])) {
$data['parameters'] = $recipient['parameters'];
}
if (! empty($recipient['unsubscribe_url'])) {
$data['unsubscribe_url'] = $recipient['unsubscribe_url'];
}
if (! empty($recipient['locale'])) {
$data['locale'] = $recipient['locale'];
}
$this->recipients[] = $data;
}
}
return $this;
}
/**
* @param string $email
* @param string|null $name
* @param array $parameters
* @param string|null $unsubscribe_url
* @return $this
*/
public function addRecipient(string $email, ?string $name = null, array $parameters = [], ?string $unsubscribe_url = null, ?string $locale = null): PushTemplate
{
$data = [
'email' => $email
];
if (! is_null($name)) {
$data['name'] = $name;
}
if (! empty($parameters)) {
$data['parameters'] = $parameters;
}
if (! is_null($unsubscribe_url)) {
$data['unsubscribe_url'] = $unsubscribe_url;
}
if (! is_null($locale)) {
$data['locale'] = $locale;
}
$this->recipients[] = $data;
return $this;
}
/**
* @param array $parameters
* @return $this
*/
public function parameters(array $parameters): PushTemplate
{
$this->parameters = $parameters;
return $this;
}
/**
* @param array $attachments
* @return $this
*/
public function attachments(array $attachments): PushTemplate
{
foreach ($attachments as $attachment) {
$this->addAttachment($attachment['path'] ?? $attachment['file'], $attachment['filename'] ?? null);
}
return $this;
}
/**
* @param $file
* @param $filename
* @return $this
*/
public function addAttachment($file, ?string $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 $this
*/
public function addParameter(string $key, string $value): PushTemplate
{
$this->parameters[$key] = $value;
return $this;
}
/**
* @param $bulk_mode
* @return $this
*/
public function bulkMode($bulk_mode = true)
{
$this->bulk_mode = $bulk_mode;
return $this;
}
/**
* @param $test_mode
* @return $this
*/
public function testMode($test_mode = true)
{
$this->test_mode = $test_mode;
return $this;
}
/**
* @param string|null $locale
* @return $this
*/
public function locale(?string $locale = null)
{
$this->locale = $locale;
return $this;
}
public function unsubscribeUrl(?string $url = null)
{
$this->unsubscribe_url = $url;
return $this;
}
/**
* Get an array representation of the message.
*
* @return array
*/
public function toArray(): array
{
return [
'template' => $this->template_id,
'locale' => $this->locale,
'recipients' => $this->recipients,
'parameters' => $this->parameters,
'reply_to' => $this->replyTo,
'sender' => $this->sender,
'bulk' => $this->bulk,
'test' => $this->test_mode,
'unsubscribe_url' => $this->unsubscribe_url,
'attachments' => $this->attachments,
];
}
public function jsonSerialize(): mixed
{
return $this->toArray();
}
}