PHPackages                             phrity/util-interpolator - 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. phrity/util-interpolator

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

phrity/util-interpolator
========================

Interpolator class and trait.

1.1.1(5mo ago)02.4k↑2740%2MITPHPPHP ^8.1CI passing

Since Jul 3Pushed 5mo agoCompare

[ Source](https://github.com/sirn-se/phrity-util-interpolator)[ Packagist](https://packagist.org/packages/phrity/util-interpolator)[ Docs](https://phrity.sirn.se/util-interpolator)[ RSS](/packages/phrity-util-interpolator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (6)Versions (4)Used By (2)

[![Phrity Util Interpolator](docs/logotype.png)](docs/logotype.png)

[![Build Status](https://github.com/sirn-se/phrity-util-interpolator/actions/workflows/acceptance.yml/badge.svg)](https://github.com/sirn-se/phrity-util-interpolator/actions)[![Coverage Status](https://camo.githubusercontent.com/ec158b456019fec8ac664ea28856e01e6e3906020f8568f09a1aeb01dcf5c1ba/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f7369726e2d73652f7068726974792d7574696c2d696e746572706f6c61746f722f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/github/sirn-se/phrity-util-interpolator?branch=main)

Introduction
============

[](#introduction)

Class and trait to perform string interpolation.

Installation
------------

[](#installation)

Install with [Composer](https://getcomposer.org/);

```
composer require phrity/util-interpolator

```

How to use
==========

[](#how-to-use)

Using the Interpolator class
----------------------------

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

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

[](#defining-path-separator)

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

Path access uses [Phrity Accessor](https://phrity.sirn.se/util-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
--------------------------

[](#defining-value-transformer)

To convert replacer values to strings the library uses [Phrity Transformers](https://phrity.sirn.se/util-transformer).

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
========

[](#versions)

VersionPHP`1.0``^8.1`Initial version

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance71

Regular maintenance activity

Popularity23

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 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 ~78 days

Total

3

Last Release

164d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4255391?v=4)[Sören Jensen](/maintainers/sirn-se)[@sirn-se](https://github.com/sirn-se)

---

Top Contributors

[![sirn-se](https://avatars.githubusercontent.com/u/4255391?v=4)](https://github.com/sirn-se "sirn-se (6 commits)")

---

Tags

interpolatorphpphp-libraryinterpolateinterpolator

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phrity-util-interpolator/health.svg)

```
[![Health](https://phpackages.com/badges/phrity-util-interpolator/health.svg)](https://phpackages.com/packages/phrity-util-interpolator)
```

PHPackages © 2026

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