fastwf/interpolation
Composer 安装命令:
composer require fastwf/interpolation
包简介
String interpolation library
README 文档
README
Introduction
String interpolation library.
For all exemples,
vendor/autoload.phpmust be included to allows auto include.
StringInterpolator
Usage
<?php // test.php // ... use Fastwf\Interpolation\StringInterpolator; $interpolator = new StringInterpolator(); echo $interpolator->interpolate( "Hello %{name} with injection escaped \%{name} or %{undefined} not injected.", ['name' => 'Fast Web Framework'] ) . PHP_EOL;
This script execution produce the next output.
$ php test.php
Hello Fast Web Framework with injection escaped %{name} or %{undefined} not injected.
Customise
It's possible to customise the behaviour of the interpolator.
Strict interpolation
Interpolation can be strict, in that case when a variable is not provided in
the parameter array an InterpolationException is thrown.
<?php ///... new StringInterpolator(true);
Different markers
Interpolation markers can be customized. For example use #[...] instead of %{...}.
<?php ///... new StringInterpolator(false, '#', '[', ']');
LexInterpolator
Usage
<?php // test.php // ... use Fastwf\Interpolation\LexInterpolator; $interpolator = new LexInterpolator(); echo $interpolator->interpolate( "Hello %{name} with injection escaped \%{name}.", ['name' => 'Fast Web Framework'] ) . PHP_EOL;
This script execution produce the next output.
$ php test.php
Hello Fast Web Framework with injection escaped \%{name}.
It's not possible to inject undefined variables like
StringInterpolatorbecause lex parse the template as node and node cannot be restored from its original form.
Add transform functions
The LexInterpolator allows to transform values thanks to the pipe syntax.
A pipe is required when the value cannot be transformed as string or when the value must be updated before injection.
The next example show how to format a date and transform it before injection in the template.
// test.php // ... $interpolator = new LexInterpolator(); $interpolator->getEnvironment() ->setPipe('date', new PipeFunction( function ($date, $format) { return $date->format($format); }) ) ->setPipe('lower', new PipeFunction( function ($str) { return mb_strtolower($str); } )) ; echo $interpolator->interpolate( "Today is %{ today | date('l jS F Y') | lower }.", ['today' => new DateTime('2022-03-16')] ) . PHP_EOL;
This script execution produce the next output.
$ php test.php Today is wednesday 16th march 2022.
Customise
Like StringInterpolator, interpolation markers can be customized for LexInterpolator.
For example for #[...]:
<?php // ... $interpolator = new LexInterpolator("#", "[", "]"); // ...
fastwf/interpolation 适用场景与选型建议
fastwf/interpolation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 620 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 11 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 fastwf/interpolation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 fastwf/interpolation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 620
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-11-08