fomvasss/laravel-str-tokens

一个用于管理和生成带有标记/短代码的字符串的Eloquent模型包

安装次数: 5 191

依赖者: 0

建议者: 0

安全: 0

星星: 3

关注者: 3

分支: 1

公开问题: 0

类型:composer-package

2.0 2024-03-20 17:03 UTC

This package is auto-updated.

Last update: 2024-09-20 18:18:37 UTC


README

License Build Status Latest Stable Version Total Downloads Quality Score

使用此包,您可以管理和生成带有标记/短代码的字符串,类似于CMS Drupal。

安装

从命令行运行

composer require fomvasss/laravel-str-tokens

要发布配置,请运行以下命令

php artisan vendor:publish --provider="Fomvasss\LaravelStrTokens\ServiceProvider"

配置文件将发布到 config/str-tokens.php

配置

配置将允许您使用 token_match_patterntoken_split_character 控制标记的解析方式

您可以使用 can_traverse_relations 决定标记是否可以遍历Eloquent模型关系

您可以使用 disable_model_tokens 全局限制哪些模型字段可以作为标记使用

您还可以通过创建返回有效模式数组的 strTokenWhiteliststrTokenBlacklist 函数,来限制通过单个模型公开的标记

用法

$str = StrToken::setText('
            Example str with tokens for article: "[article:title] ([article:id])",
            Article created at date: [article:created_at],
            Author: [article:user:name]([article:user:id]).
            Article status: [article:txArticleStatus:name],
            Article root category: [article:txArticleCategories:root:name],
            User: [article:user:email], [article:user:city:country:title], [article:user:city:title].
            Generated token at: [config:app.name], [date:raw]
            [article:test:Hello]!!!
            Length: [var:length];
            Width: [var:width];
            Price: [var:price]
        ')
    ->setDate(\Carbon\Carbon::tomorrow())
    ->setEntity(\App\Model\Article::findOrFail(13))
    ->setVars(['length' => '2.2 m.', 'width' => '3.35 m.'])
    ->setVar('price', '$13')
    ->replace();

给定结果

 Example str with tokens for article: "Test article title(23)",
 Article created at date: 15.07.2018,
 Author: Taylor Otwell(1),
 Article status: published,
 Article root category: Programming,
 User: taylorotwell@gmail.com, AR, Little Rock.
 Generated token at: Laravel, 2018-10-27 00:00:00
 TEST TOKEN:Hello!!! 
 Length: 2.2 m.;
 Width: 3.35 m.;
 Price: $13

您可以使用 setEntities() 方法为多个Eloquent模型设置,例如

<?php 
$user1 = User::find(1);
$user2 = User::find(2);
$article = Article::first();

$str = StrToken::setText('
		User1: [user1:name] / [user1:email]
		User2: [user2:name] / [user2:email]
		Article: "[firstArticle:title]"
	')->setEntities([
        'user1' => $user1,
        'user2' => $user2,
        'firstArticle' => $article,
    ])->replace();
	
	/*
	User: Taylor Otwell / taylorotwell@gmail.com
	User: Vasyl Fomin / fomvasss@gmail.com
	Article: "Laravel is awesome framework"
	*/

在Eloquent模型中定义自定义标记

在您的模型中,您可以创建自己的方法来生成标记。

这些方法的名称必须以 strToken 开头。

在下一个示例中,我们创建自定义方法:strTokenTest()strTokenCreatedAt()

现在我们可以在字符串中使用下一个标记

This is [article:test], created at: [article:creted_at]

结果

This is "TEST TOKEN", created at: 23.11.2018

示例 Article Eloquent模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Fomvasss\Taxonomy\Models\Traits\HasTaxonomies;

class Article extends Model
{
    use HasTaxonomies;
    
    //...
    
    public function strTokenTest($entity, $method, $attr): string
    {
        // $entity - this article
        // $method - "test"
        // $attr - additional args
        return 'TEST TOKEN:' . $attr;
    }
    
    public function strTokenCreatedAt(): string
    {
        return $this->created_at->format('d.m.Y');	
    }
    
    // For package https://github.com/fomvasss/laravel-simple-taxonomy
    public function txArticleStatus()
    {
        return $this->term('status', 'system_name')
            ->where('vocabulary', 'post_statuses');
    }
}

示例 Term 模型

<?php

namespace App\Models\Taxonomies;

use App\Article;

class Term extends \Fomvasss\Taxonomy\Models\Term
{
    public function articles()
    {
        return $this->morphedByMany(Article::class, 'termable');
    }

	/**
 	* Method for generate next example token for article model:
 	* [article:txArticleCategories:root:name]
	*	 
	* @param $entity
	* @param $r
	* @param $param
	* @return mixed
 	*/
    public function strTokenRoot($entity, $r, $param)
    {
        if ($root = $entity->ancestors->first()) {
            return $root->{$param};
        }

        return $entity->{$param};
    }
}

在blade模板中使用

@php(\StrToken::setEntity($article)->setDate($article->created_at))
@php(\StrToken::setText('[article:title] - [date:short]'))
<h3>{!! \StrToken::replace() !!}</h3>

更新日志

有关最近更改的更多信息,请参阅 CHANGELOG

链接

许可证

MIT许可证(MIT)。有关更多信息,请参阅 许可证文件