定制 azbosakov/param-string-placeholders 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

azbosakov/param-string-placeholders

Composer 安装命令:

composer require azbosakov/param-string-placeholders

包简介

Template string with configurable placeholder delimiters.

README 文档

README

A template string with a configurable placeholder delimiters. Can take forms like, eg.:

  • 'aaa{PARAM}bbb' O:'{', C:'}'
  • 'aaa{{PARAM}}bbb' O:'{{', C:'}}'
  • 'aaa${PARAM}bbb' O:'${', C:'}'
  • 'aaa%PARAM%bbb' O:'%', C:'%'
  • 'aaabbb' O:'<', C:'>'

The placeholder structure is: E* O P C, where:

  • O: Opening delimiter
  • P: Parameter name
  • C: Closing delimiter
  • E: Escape string

None of the O,C,E can be empty ''.

The placeholders are passed as a constructor arguments: ($template, $open, $close, $escape). If any of $open, $close or $escape is null, the default value for the class is used (class-wide defaults can be set with ::setDefaultDelimiters($open, $close, $escape)).

The string is scanned for the closest non-empty pairs of OPEN/CLOSE.

OPEN directly followed by CLOSE is not treated like a placeholder, but taken verbatim.

The string between the O/C pair is the PARAM name. It can contain anything, except, of cource, OPEN or CLOSE strings.

If the placeholder is directly preceded with 1 or more ESCAPE strings, any two of them are converted to single one. If the E string count is odd, the placeholder is escaped and taken as part of the string in the form OPC, without the last ESC.

Examples

With O:'{', C:'}', E:'!'

  • {} : String '{}'
  • {P} : Parameter with name 'P'
  • !{P} : String '{P}'
  • !!{P} : Double escape, string '!' + param 'P'
  • !!!!!{P} : Two double escapes and a leftover one, escaping the placeholder: strings '!!' + '{P}'

So the template

'aaa{}{{P1}bbb!!!{P2}ccc!!{P3}ddd'

is parsed as:

'aaa{}{', @ P1, 'bbb!{P2}ccc!', @ P3, 'ddd'

With O:'%', C:'%', E:'%'

  • '%%%%%P%%%' is 2 double escapes (%%+%%) -> string '%%' + param P + string '%%'

Methods

public function __construct(string $template, ?string $open = null, ?string $close = null, ?string $escape = null)

If any of $open, $close or $escape is null, the default value for the class is used.

public function getPieces(): array

A debugging function - returns the pieces, to which the template string is split, with the placeholder pieces marked as '@ PHName'.

public function getDelimiters() : array

Returns array ['open'=>..., 'close'=>..., 'escape'=>...]

public static function getDefaultDelimiters() : array

Returns array - the default delimiters for the class: ['open'=>..., 'close'=>..., 'escape'=>...]

public static function setDefaultDelimiters(?string $open = null, ?string $close = null, ?string $escape = null) : void

Set the default delimiters for the entire class. If any of them is null, use the hardcoded default.

public function getTemplate() : string (ParamStringInterface)

Get the template string passed to the constructor

public function withParam(string $name, $value) : self (ParamStringInterface)

Clone the object and set the named param value.

public function getParam(string $name) (ParamStringInterface)

Get a parameter by name

public function withParams(array $params) : self (ParamStringInterface)

Clone the object and set multiple params at once.

public function getParams() : array (ParamStringInterface)

Get a snapshot of the parameters

public function __toString() : string (ParamStringInterface)

Complete example

$ph = new Placeholders(
    'The answer is <ans>, !<not a param>, !!<a param>, !!!<not a param>, !!!!<also param>',
    '<', '>', '!'
);

$ph = $ph->withParams(['ans'=>42, 'a param'=>'XXX', 'also param'=>'YYY']);

"$ph" == 'The answer is 42, <not a param>, !XXX, !<not a param>, !!YYY';

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-03-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固