承接 mefworks/string-interpolation 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mefworks/string-interpolation

Composer 安装命令:

composer require mefworks/string-interpolation

包简介

Interpolate strings by replacing placeholders with data from an array.

README 文档

README

Total Downloads Latest Stable Version

mef\StringInterpolation defines the interface StringInterpolationInterface that describes a method to combine an array of arbitrary data with a string, much like the PHP methods vsprintf and strtr.

The core implementation of PlaceholderInterpolator replaces {0} and {key} with the corresponding values from the context array, but other implementations of the interface are free to use any syntax.

It is recommended that interpolators make use of mef\Stringifier to convert non-string values from the context array into a string.

Examples

PlaceholderInterpolator

<?php
$stringifier = new mef\Stringifier\Stringifier;
$interpolator = new mef\StringInterpolation\PlaceholderInterpolator($stringifier);

echo $interpolator('Hello, {name}!', ['name' => 'Matthew']), PHP_EOL;

echo $interpolator->getInterpolatedString('Index based 0: {0}, 1: {1}', ['one', 'two']), PHP_EOL;

Sample Output:

Hello, Matthew!
Index based 0: one, 1: two

The Stringifier is what is responsible for casting the value in the array to a string. In this case, all values were already strings, so it is a somewhat irrelevant detail. Note that the functor call (first echo) is from the AbstractStringInterpolator, and is not guaranteed to exist in all interpolators since the interface does not require it.

More complete examples are available in the examples directory.

Overview

The mef\StringInterpolation\StringInterpolatorInterface describes two methods:

<?php namespace mef\StringInterpolation;

interface StringInterpolatorInterface
{
	/**
	 * Given a string and an array of context, return a new Interpolation
	 * record, from which the interpolated string and unused context can be
	 * retrieved.
	 *
	 * The implementation details are left undefined.
	 *
	 * @param  string $string  The string to interpolate
	 * @param  array|mef\StringInterpolation\ContextInterface $context
	 *
	 * @return mef\StringInterpolation\InterpolationInterface
	 */
	public function interpolate($string, $context);

	/**
	 * Given a string and an array of context, return a new string with that
	 * context embedded in it.
	 *
	 * This MUST always give the same results as:
	 *
	 * ->interpolate($string, $context)->getString()
	 *
	 * This variation should be optimized when possible, as there's no need to
	 * create an Interpolation object or track unused context.
	 *
	 * @param  string $string  The string to interpolate
	 * @param  array|mef\StringInterpolation\ContextInterface $context
	 *
	 * @return string
	 */
	public function getInterpolatedString($string, $context);
}

Both methods perform the same underlying operation. getInterpolatedString(...) returns the same thing as interpolate(...)->getString() but is slightly faster since no Interpolation object is created and the context does not need to be updated.

The interpolator object is defined by this interface:

<?php namespace mef\StringInterpolation;

interface InterpolationInterface
{
	/**
	 * The interpolated string.
	 *
	 * @return string
	 */
	public function getString();

	/**
	 * The used context.
	 *
	 * @return array
	 */
	public function getUsedContext();
}

Contexts

The context supplied to an interplator can either be a native PHP array, or an object that implements ContextInterface.

  • ArrayContext - Wraps an array into an object that implements the ContextInterface.

Interpolators

  • ExpressionInterpolator - Treats {key} and {0} as placeholders, and uses a string interpolator to replace them with the corresponding values from the context array. Values can be filtered through expressions that mutate the output. e.g., {key|uppercase}
  • IndirectInterpolator - A decorator that looks up a string format from an ArrayAccess object before passing it to the internal interpolator. Useful for creating a resource bundle.
  • PlaceholderInterpolator - Similar to ExpressionInterpolator, but it does not support an expressive syntax.
  • PrintFInterpolator - Uses a printf style of interpolation.

License

Copyright (C) 2014 Matthew Leverton

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

mefworks/string-interpolation 适用场景与选型建议

mefworks/string-interpolation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24.15k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2014 年 09 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mefworks/string-interpolation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mefworks/string-interpolation 我们能提供哪些服务?
定制开发 / 二次开发

基于 mefworks/string-interpolation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 24.15k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 18
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-09-12