LaravelPackages.net
Acme Inc.
Toggle sidebar
fomvasss/laravel-str-tokens

A package to manage and generate string with tokens/shortcodes for Eloquent Models

8.499
4
2.7.0
About fomvasss/laravel-str-tokens

fomvasss/laravel-str-tokens is a Laravel package for a package to manage and generate string with tokens/shortcodes for eloquent models. It currently has 4 GitHub stars and 8.499 downloads on Packagist (latest version 2.7.0). Install it with composer require fomvasss/laravel-str-tokens. Discover more Laravel packages by fomvasss or browse all Laravel packages to compare alternatives.

Last updated

Laravel Str Tokens

License Build Status Latest Stable Version Total Downloads Quality Score

Support

If this package is useful to you, consider supporting its development:

Monobank Ko-Fi USDT TRC20

USDT TRC20 address: THLgp6DxiAtbNHvgnKV56vk1L38UuUagKf

With this package you can manage & generate strings with tokens/shortcodes, it seems like CMS Drupal.


Installation

Run from the command line:

composer require fomvasss/laravel-str-tokens

To publish the configs, run the following command:

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

Configuration file will be publish to config/str-tokens.php

Configuration

The configuration fill will allow you to control how tokens are parsed using token_match_pattern and token_split_character

You can decide if a token can traverse eloquent model relationships using can_traverse_relations

You can globally limit what model fields are allowed as tokens using disable_model_tokens

You can also limit what tokens are exposed via individual models by creating a strTokenWhitelist or strTokenBlacklist function that returns an array of valid patterns

For finesh prepare string - use formatters - methods an functions for modify string

Usage

$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();

Given result:

 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: [email protected], 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

Use method setEntities() for set many Eloquent models, for example:

<?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 / [email protected]
	User: Vasyl Fomin / [email protected]
	Article: "Laravel is awesome framework"
	*/

Use formatters

All formatters added after last symbol :

 StrToken::setText('User: [user:name:uppercase], Email: [user:email:lovercase]')->setEntities(['user' => $user])->replace();

Not use together setEntity and setEntities! setEntity has haight priority!

Defining custom tokens in Eloquent models

In your models you can create own methods for generate tokens.

The names of these methods must begin with strToken.

In next example, we create custom methods: strTokenTest(), strTokenCreatedAt()

And now we can use next token in string:

This is [article:test], created: [article:cretedAt]

And result:

This is "TEST TOKEN", created at: 23.11.2018

Example Article Eloquent model:

<?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');
    }
}

Example Term model:

<?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};
    }
}

Use in blade template

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

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

The MIT License (MIT). Please see License File for more information.

Star History Chart