PHPackages                             taylornetwork/variable-replacer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. taylornetwork/variable-replacer

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

taylornetwork/variable-replacer
===============================

Replaces variables in strings at defined stages

1.0.1(8y ago)111MITPHPPHP ^7.1

Since Mar 20Pushed 8y ago1 watchersCompare

[ Source](https://github.com/taylornetwork/variable-replacer)[ Packagist](https://packagist.org/packages/taylornetwork/variable-replacer)[ RSS](/packages/taylornetwork-variable-replacer/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (2)Dependencies (1)Versions (4)Used By (0)

VariableReplacer
================

[](#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)

Install using Composer

```
$ composer require taylornetwork/variable-replacer
```

Default Options
---------------

[](#default-options)

By default the syntax for a replaced variable is

```
'@stage{var}'
```

Note no `$` on the variable.

Usage
-----

[](#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

[](#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');
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

3

Last Release

2973d ago

Major Versions

0.0.1 → 1.0.12018-03-20

### Community

Maintainers

![](https://www.gravatar.com/avatar/83340094473f0bf5b2cf062bf394df221a52a30aa0e21cd0a77302977d6393ce?d=identicon)[samueljtaylor](/maintainers/samueljtaylor)

---

Top Contributors

[![samyrataylor](https://avatars.githubusercontent.com/u/15961687?v=4)](https://github.com/samyrataylor "samyrataylor (4 commits)")

### Embed Badge

![Health badge](/badges/taylornetwork-variable-replacer/health.svg)

```
[![Health](https://phpackages.com/badges/taylornetwork-variable-replacer/health.svg)](https://phpackages.com/packages/taylornetwork-variable-replacer)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M683](/packages/barryvdh-laravel-ide-helper)[orchestra/canvas

Code Generators for Laravel Applications and Packages

21017.2M157](/packages/orchestra-canvas)[illuminate/pipeline

The Illuminate Pipeline package.

9446.6M210](/packages/illuminate-pipeline)[illuminate/pagination

The Illuminate Pagination package.

10532.5M858](/packages/illuminate-pagination)[spatie/laravel-pjax

A pjax middleware for Laravel 5

513371.8k11](/packages/spatie-laravel-pjax)[spatie/laravel-mix-preload

Add preload and prefetch links based your Mix manifest

169176.0k2](/packages/spatie-laravel-mix-preload)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
