PHP code example of taylornetwork / variable-replacer

1. Go to this page and download the library: Download taylornetwork/variable-replacer 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/ */

    

taylornetwork / variable-replacer example snippets


'@stage{var}'


// App\Models\Model.php

use Illuminate\Database\Eloquent\Model;

abstract class Model
{

    /**
     * All models will define what the model's name is
     *
     * @return string
     */
    abstract public funtion getNameAttribute();

    /**
     * Returns the model name
     *
     * @return string
     */
    public function getModelNameAttribute()
    {
        return class_basename(get_class($this));
    }
}



// App\Observers\BaseObserver.php

use TaylorNetwork\VariableReplacer\VariableReplacer;
use App\Models\Model;
use Some\Logger\Package\Logger;

class BaseObserver
{
    protected $descriptions = [
        'created' => 'A @entry{modelName} named @runtime{name} was created.',
    ];
    
    public function created(Model $model) 
    {
        $description = (new VariableReplacer)->stage('entry')
                                             ->replaceWith($model)
                                             ->parse($this->descriptions['created']);
      
        return (new Logger)->log($description);
    }
}


'A Customer name @runtime{name} was created.'


// App\Models\Log.php

use TaylorNetwork\VariableReplacer\VariableReplacer;
use Illuminate\Database\Eloquent\Model;

class Log extends Model
{
    public function getDescriptionAttribute()
    {
        return (new VariableReplacer)->stage('runtime')
                                     ->replaceWith($this->relatedModel)
                                     ->parse($this->attributes['description']);
    }
}


'A Customer named John Smith was created.'


(new VariableReplacer)->stage('runtime')
                      ->replaceWith($someModel)
                      ->parse('A @runtime{relatedModel->someRelation->someOtherMethod()} did something');