Giter Club home page Giter Club logo

variable-replacer's Introduction

VariableReplacer

This package will replace variables in a string. It includes functionality to use a stage based replace, meaning you can replace multiple variables in the same string at different times.

Install

Install using Composer

$ composer require taylornetwork/variable-replacer

Default Options

By default the syntax for a replaced variable is

'@stage{var}'

Note no $ on the variable.

Usage

Let's say you have a logger class that logs recent activity and you want to define the same message whenever any model is created but want information from the model in the description.

In this example my base model class is defined as:

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

In an observer class you could do the following.

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

The VariableReplacer gets the entire App\Models\Model $model object and as such, all methods are available to be used.

If a model App\Models\Customer with name 'John Smith' was created the $description that would be saved to the database is

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

When accessing the log you could run the description through the replacer with the runtime stage.

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

Assuming 'A Customer named @runtime{name} was created.' was passed the returned value would be

'A Customer named John Smith was created.'

Note

Because the VariableReplacer gets the entire object you pass to the replaceWith method, you can chain methods onto it if you need to get relations, etc.

Example:

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

variable-replacer's People

Contributors

itssamtaylor avatar

Stargazers

Pablo Veintimilla avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.