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.
Strengths:
InterpolatorTrait enables composition over inheritance, fitting Laravel’s service container and dependency injection patterns.Gaps:
composer require, with no breaking changes in recent versions (1.0+).phrity/util-accessor, phrity/util-transformer), but these are internal to the package and unlikely to conflict with Laravel’s core.str_replace or vsprintf.{/} (mitigate via input sanitization or regex validation).Str::replace() or vsprintf for critical paths.[a-zA-Z_][a-zA-Z0-9_]*).{key} unchanged if not found).str_replace/vsprintf for dynamic templates (e.g., emails, notifications)?phrity/ namespace sustainable? (Low stars/dependents suggest niche adoption.)@include or translation system suffice? If not, why?Laravel Compatibility:
Interpolator class as a singleton or resolve dynamically.__() interpolation).Recommended Integration Points:
$this->interpolator->interpolate($template, $data);
// app/Providers/BladeServiceProvider.php
Blade::directive('interpolate', function ($expression) {
return "<?php echo app(\\Phrity\\Util\\Interpolator\\Interpolator::class)->interpolate($expression[0], $expression[1]); ?>";
});
Usage: @interpolate('Hello {name}', ['name' => $user->name])__() function for interpolation.
Str::macro('interpolate', function ($string, $replace) {
return app(\Phrity\Util\Interpolator\Interpolator::class)->interpolate($string, $replace);
});
Usage: __('Welcome, :name!', ['name' => Str::interpolate('{user.name}', $user)])str_replace/vsprintf logic.str_replace(['{name}'], [$name])) with Interpolator.config('interpolator.fallback')) to revert to str_replace if issues arise.phrity/util-accessor and phrity/util-transformer are internal and unlikely to conflict.$this->app->singleton(\Phrity\Util\Interpolator\Interpolator::class);
DateTime support if needed)./ for JSON-like access).phrity/ package updates.{/} in templates).Xdebug to trace nested path access.vsprintf for simple cases.How can I help you explore Laravel packages today?