simklee / string-buffer
一个具有流畅接口和方法链的对象化字符串缓冲区。
1.0.8
2022-11-25 13:54 UTC
Requires
- php: ^8.0|^8.1
Requires (Dev)
- phpstan/phpstan: ^1.4
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^4
README
一个用于通过流畅接口和方法链操作字符串的小型库。
安装
composer require simklee/string-buffer
用法
use Simklee\LaravelStringBuffer\StringBuffer;
// Sample with append methods
$buffer = new StringBuffer();
$buffer->append('some string')
->appendIf($condition, 'true', 'false')
->appendIfNot($condition, 'false', 'true')
->appendFormatted('%s, %s', 'LastName', 'FirstName')
->appendIfNull($isNull, 'is null', 'is NOT null')
->appendIfNotNull($isNotNull, 'is NOT null', 'is null')
->appendImplode(['a', 'b', 'c'], ',');
echo $buffer->toString();
// Sample with prepend methods
echo StringBuffer::create('some string')
->prepend('some string')
->prependIf($condition, 'true', 'false')
->prependIfNot($condition, 'false', 'true')
->prependFormatted('%s, %s', 'LastName', 'FirstName')
->prependIfNull($isNull, 'is null', 'is NOT null')
->prependIfNotNull($isNotNull, 'is NOT null', 'is null')
->prependImplode(['a', 'b', 'c'], ',')
->toString();
方法
方法 | 描述 |
---|---|
__construct(string $string = null) | StringBuffer 构造函数 |
StringBuffer static create(string $string = null) | 创建 StringBuffer 实例。适用于方法链 |
string toString() | 获取 StringBuffer 的字符串值 |
string __toString() | 获取 StringBuffer 的字符串值 |
StringBuffer append(string $string) | 将字符串追加到缓冲区。 |
StringBuffer appendFormatted(string $format, string ...$values) | 将格式化字符串(使用 sprintf())追加到缓冲区。 |
StringBuffer appendIf(bool $condition, string $string, string $else = null) | 如果 $condition 为 true,则将字符串追加到缓冲区,否则 $else(如果非空) |
StringBuffer appendIfNot(bool $condition, string $string, string $else = null) | 如果 $condition 为 false,则将字符串追加到缓冲区,否则 $else(如果非空) |
StringBuffer appendIfNull(mixed $value, string $string, string $else = null) | 如果 $value 为 null,则将字符串追加到缓冲区,否则 $else(如果非空) |
StringBuffer appendIfNotNull(mixed $value, string $string, string $else = null) | 如果 $value 非 null,则将字符串追加到缓冲区,否则 $else(如果非空) |
StringBuffer appendImplode(array $values, string $separator = ' ') | 将 imploded 数组追加到缓冲区。 |
StringBuffer prepend(string $string) | 将字符串 prepends 到缓冲区。 |
StringBuffer prependFormatted(string $format, string ...$values) | 使用 sprintf() prepends 格式化字符串到缓冲区。 |
StringBuffer prependIf(bool $condition, string $string, string $else = null) | 如果 $condition 为 true,则将字符串 prepends 到缓冲区,否则 $else(如果非空) |
StringBuffer prependIfNot(bool $condition, string $string, string $else = null) | 如果 $condition 为 false,则将字符串 prepends 到缓冲区,否则 $else(如果非空) |
StringBuffer prependIfNull(mixed $value, string $string, string $else = null) | 如果 $value 为 null,则将字符串 prepends 到缓冲区,否则 $else(如果非空) |
StringBuffer prependIfNotNull(mixed $value, string $string, string $else = null) | 如果 $value 非 null,则将字符串 prepends 到缓冲区,否则 $else(如果非空) |
StringBuffer prependImplode(array $values, string $separator = ' ') | 将 imploded 数组 prepends 到缓冲区。 |