PHP code example of fomvasss / laravel-str-tokens

1. Go to this page and download the library: Download fomvasss/laravel-str-tokens library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fomvasss / laravel-str-tokens example snippets


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


 
$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"
	*/



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



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

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

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