abather/model-notification

将通知模板添加到模型中

1.0.0 2024-04-25 06:42 UTC

This package is auto-updated.

Last update: 2024-09-25 11:24:41 UTC


README

Latest Version on Packagist Total Downloads

此包帮助您为包含每个模型的模板消息进行组织和保存。每条消息都依赖于keylanguagechannel

安装

您可以通过composer安装此包

composer require abather/model-notification

您可以使用以下命令发布和运行迁移

php artisan vendor:publish --tag="model-notification-migrations"
php artisan migrate

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="model-notification-config"

这是已发布配置文件的内容

return [
    "fallback_lang" => env("MODEL_NOTIFICATION_FALLBACK_LANG", "ar"),

    "variable_starter" => "[",

    "variable_ender" => "]",

    "relationship_variable_symbol" => "->",

    "file_name" => "file",

    "prevent_including_file" => false,

    "file_variables" => [
        "file",
        "file_path",
        "attachment"
    ]
];

覆盖全局配置

如果您希望排除特定模型的文件,可以在模型中添加变量prevent_including_file

public static $prevent_including_file = true;

如果您想为模型使用特定的文件变量,可以添加变量file_variables

public static $file_variables = ["document"];

使用方法

您可以通过实现Notifier接口和使用Notifier特质,使用此包与任何Model一起使用

namespace App\Models;

use Abather\ModelNotification\Contracts\Notifier;
use Illuminate\Database\Eloquent\Model;

class Bill extends Model implements Notifier
{
    use \Abather\ModelNotification\Notifier;
}

现在您可以创建、调用或更新如下所述的消息模板。

为模型创建新模板

要为任何模型创建新模板,您可以使用makeTemplateMessage()方法。您必须指定keylangchanneltemplate

Bill::makeTemplateMessage()
    ->key("new")
    ->channel("sms")
    ->lang("ar")
    ->template("You have a new bill [id], with an amount of: [amount]")
    ->save();

在模板文本中包含变量

template()方法可以包含模型中存在的任何属性。属性值将替换属性名称。例如,[amount]将检索名为amount的属性值。要更改表示变量名称的符号,您可以修改配置文件中的variable_startervariable_ender

包含关系中的属性

template()方法还可以包含支持的关系中的值,例如[user->name]。它类似于包含属性,并支持belongsTohasOne关系。

在模板文本中包含文件路径

您可以通过添加在config("model-notification.file_variables")中定义的任何键或在模型中定义的$file_variables来在模板中包含文件URL。此外,在创建模板消息时,您还需要使用includeFile()

Bill::makeTemplateMessage()
    ->includeFile();

您可以通过在模型中覆盖getFilePath()方法来更改返回的URL。

为模板消息添加额外数据

您可以将任何额外数据传递给prob([$key => $value])。每个值都将与模板相同的处理过程替换属性、关系属性或文件URL。

Bill::makeTemplateMessage()
    ->prob(["title" => "New bill generated with number [id]", "icon" => "bill"]);

获取模板消息

您可以使用getTemplateMessages()方法获取与模型相关的所有消息

Bill::getTemplateMessages();

如果您想获取特定通道、语言或键的消息,您可以使用notificationTemplates()查询构建器的forChannel($channel)forLang($lang)forKey($key)作用域

Bill::notificationTemplates()
    ->forChannel("sms")
    ->forLang("ar")
    ->get();

这将返回一个NotificationTemplate集合。

如果您想获取特定的模板消息,可以使用通过传递keylangchannelgetTemplateMessage方法

Bill::getTemplateMessage("anyKey", "ar", "sms");

此方法将返回一个NotificationTemplate实例。

如果您想获取文本消息,可以使用通过传递keylangchannelgetTemplateMessageText()方法

Bill::getTemplateMessageText("anyKey", "ar", "sms");

这将返回一个字符串,该字符串准备好与您的通知一起使用,其中已替换所有变量、关系变量和文件路径。

要获取特定模板的 prob,您可以使用 getTemplateMessageProb() 方法并传递 keylangchannel

Bill::getTemplateMessageProb("anyKey", "ar", "sms");

这将返回一个数组,其中包含每个 prob 已准备好使用和处理。如果您想获取特定 prob 的值,您可以传递一个第四个参数,即 prob

Bill::getTemplateMessageProb("anyKey", "ar", "sms", "title");

这将返回给定 prob 的值字符串,如果不存在,则返回空字符串。

如果文件与模板一起包含,您可以使用 () 方法获取文件 URL 或文件对象,并传递 keylangchannel

Bill::getFile("anyKey", "ar", "sms");

这将返回文件 URL。如果您想获取文件参数,必须将第四个参数传递为 false

Bill::getFile("anyKey", "ar", "sms", false);

这将返回文件参数。请注意,您必须在自己的模型中配置 getFileObject() 方法。

测试

composer test

变更日志

请参阅 变更日志 了解最近有哪些变化。

贡献

请参阅 贡献指南 了解详细信息。

安全漏洞

请查阅 我们的安全策略 了解如何报告安全漏洞。

致谢

许可证

MIT 许可证 (MIT)。请参阅 许可证文件 了解更多信息。