Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Util Interpolator Laravel Package

phrity/util-interpolator

Lightweight PHP string interpolation helper. Replaces {key} tokens with values from an array/object, supports nested paths (default “.” separator, customizable), and can be used via Interpolator class or InterpolatorTrait. Uses Phrity Accessor/Transformer.

View on GitHub
Deep Wiki
Context7

Build Status Coverage Status

Introduction

Class and trait to perform string interpolation.

Installation

Install with Composer;

composer require phrity/util-interpolator

How to use

Using the Interpolator class

The interpolator will replace {$key} in input string with corresponding data in replacer array.

$interpolator = new Phrity\Util\Interpolator\Interpolator();
$result = $interpolator->interpolate('Interpolating {a} and {b}.', [
    'a' => 'first',
    'b' => 'second',
];
// $result -> 'Interpolating first and second.'

When replacers are nested in array or object, they can be accessed using path notation.

$interpolator = new Phrity\Util\Interpolator\Interpolator();
$result = $interpolator->interpolate('Interpolating {a.a} and {a.b} from {a}.', [
    'a' => [
        'a' => 'first',
        'b' => 'second',
    ],
];
// $result -> 'Interpolating first and second from array.'

Using the InterpolatorTrait trait

Interpolator is also available as trait method to be used in any class.

class MyClass
{
    use Phrity\Util\Interpolator\InterpolatorTrait;
    // ...
}

$myClass = new MyClass();
$result = $myClass->interpolate('Interpolating {a}', ['a' => 'b']);

Defining path separator

By default, paths are separated using "." but it is possible to define another separator.

Path access uses Phrity Accessor.

$separator = '/';
$input = 'Interpolating {a/b} and {a/c}.';
$replacers = ['a' => '{"b": "test", "c": 1234}'];

// Class
$interpolator = new Phrity\Util\Interpolator\Interpolator(separator: $separator);
$result = $interpolator->interpolate($input, $replacers);
// $result -> 'Interpolating test.'

// Trait
$myClass = new MyClass();
$result = $myClass->interpolate($input, $replacers, separator: $separator);
// $result -> 'Interpolating test.'

Defining value transformer

To convert replacer values to strings the library uses Phrity Transformers.

The default configuration uses;

$transformer = new Phrity\Util\Transformer\FirstMatchResolver([
    new Phrity\Util\Transformer\ReadableConverter(),
    new Phrity\Util\Transformer\ThrowableConverter(),
    new Phrity\Util\Transformer\BasicTypeConverter(),
]);

You can also set another transformer (or set of transformers).

$transformer = new Phrity\Util\Transformer\JsonDecoder();
$input = 'Interpolating {a.b} and {a.c}.';
$replacers = ['a' => '{"b": "test", "c": 1234}'];

// Class
$interpolator = new Phrity\Util\Interpolator\Interpolator(transformer: $transformer);
$result = $interpolator->interpolate($input, $replacers);
// $result -> Interpolating test and 1234.

// Trait
$myClass = new MyClass();
$result = $myClass->interpolate($input, $replacers, transformer: $transformer);
// $result -> Interpolating test and 1234.

Versions

Version PHP
1.0 ^8.1 Initial version
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky