ermac/text_builder

There is no license information available for the latest version (dev-master) of this package.

Create various dynamic texts

dev-master 2022-03-09 10:43 UTC

This package is auto-updated.

Last update: 2024-11-09 16:42:55 UTC


README

TextBuilder is a Laravel package for create various dynamic texts.

Installation

Use the package manager composer to install TextBuilder.

composer require ermac/text_builder

Put provider inside the config/app.php file and Service Providers section

'providers' => [    
    ...
    Ermac\TextBuilder\TextBuilderServiceProvider::class,
    

And then enter in the alias class

'aliases' => [    
    ...
    'TextBuilder' => Ermac\TextBuilder\TextBuilderFacade::class,

And run command

php artisan vendor:publish --provider="Ermac\TextBuilder\TextBuilderServiceProvider" --tag=config

Configuration

Inside the config path and textBuilder.php file You can specify the symbol between which your parameters are to be placed

'sign' => '%'

You can also define parameters globally and explain each one

'global_parameters' => [
        'date' => 'description',
        'time',
    ]

Usage

In the model where you want to define parameters, first call the HasParams trait

use Ermac\TextBuilder\HasParams;
class User extends Authenticatable
{
    ...
    use HasParams;
}

Parameters

In the model where you want to define parameters, first call the params property, then enter the desired parameters in the params property. Note that the defined parameters must be the same as the names of the columns in the database table.

protected $params = [
    'name' => 'description',
    'email',
     ];

If you want to enter all the columns, you can use the * sign

protected $params = [
    '*'
     ];

And if you want to ignore a number of columns, you can use the ^ symbol.

protected $params = [
    '^password,created_at'
     ];

Relations

You can use one-to-one or one-to-many or many-to-many relationship methods to configure such parameters.

one-to-one

protected $params = [
       'wallet-balance',
    ];

public function wallet()
{
    return $this->hasOne(Wallet::class);
}

one-to-many

protected $params = [
       'orders-tracking_code',
       'orders-price',
       'orders-product-title',
    ];

public function orders()
{
    return $this->hasMany(Order::class);
}

You can use different relationships to infinity

Order.php
class Order extends Model
{
    public function product()
    {
        return $this->belongsTo(Product::class);
    }
}

many-to-many

protected $params = [
      'roles-name'
    ];

public function roles()
{
    return $this->belongsToMany(Role::class,'user_has_roles');
}

Methods

Get parameters

$params = TextBuilder::getParameters();

output

Array ( 
    [0] => Array ( [param] => %orders_price% [description] => ) 
    [1] => Array ( [param] => %users_name% [description] => description ) 
    [2] => Array ( [param] => %users_email% [description] => ) 
    [3] => Array ( [param] => %users_wallet-balance% [description] => ) 
    [4] => Array ( [param] => %users_orders-price% [description] => ) 
    [5] => Array ( [param] => %date% [description] => description ) 
    [6] => Array ( [param] => %time% ) 
) 

Do this if you want to get the parameters of certain models

$params = TextBuilder::getParameters([Model::class,...]);

And if you want to ignore parameters, you can use this argument

$params = TextBuilder::getParameters('',\App\Models\User::class , ...);

Now

$text = "Hi %users_name%, your wallet balance is $ %wallet-balance%";
$text = TextBuilder::make($text,\App\Models\User::find(1) );

output

"Hi james, your wallet balance is $ 1000";

You can use this mode if you need to enter multiple models

$text = TextBuilder::make($text,[\App\Models\User::find(1),\App\Models\Order::find(1)] );

And if you need to ignore a parameter, you can use this mode

$text = TextBuilder::make($text,[\App\Models\User::find(1),\App\Models\Order::find(1)] ,['users_password',...]);

If you need to manually enter the parameters like the global parameters, you can use the following method

$text = TextBuilder::set($text,['time',...],[\Carbon\Carbon::make(now())->format('H:m'),...]);

License

MIT