fomvasss / laravel-str-tokens
A package to manage and generate string with tokens/shortcodes for Eloquent Models
Installs: 7 015
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 1
Open Issues: 0
Type:composer-package
pkg:composer/fomvasss/laravel-str-tokens
Requires
- php: >=7.0.0
- illuminate/support: ^9.0|^10.0|^11.0|^12.0
- nesbot/carbon: ^2.0|^3.0
README
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
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: 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
You can 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 / taylorotwell@gmail.com User: Vasyl Fomin / fomvasss@gmail.com Article: "Laravel is awesome framework" */
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 at: [article:creted_at]
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.
Links
License
The MIT License (MIT). Please see License File for more information.