respect/stringifier 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

respect/stringifier

Composer 安装命令:

composer require respect/stringifier

包简介

Converts any value to a string

README 文档

README

Build Status Code Coverage Latest Stable Version Total Downloads License

Converts any PHP value into a string.

Installation

Package is available on Packagist, you can install it using Composer.

composer require respect/stringifier

This library requires PHP >= 8.3.

IMPORTANT: Be careful with version 2.0

If you are using version 2.0, please be aware of security concerns related to information leakage.

  1. Class/Interface/Enum Detection: Version 2.0 would automatically detect and format strings that matched internal class, interface, or enum names. This could expose your application's internal architecture.
  2. Callable String Detection: Version 2.0 would interpret strings and arrays as callables by default, potentially exposing sensitive data. .

If you're not stringifiying strings coming from and end-user, you're not at risk. However, later versions changed this to a "secure-by-default" approach, assuming that strings may come from untrusted sources.

Usage

Below a quick guide of how to use the library.

Using as a function

echo Respect\Stringifier\stringify($value);

Using as an object

use Respect\Stringifier\HandlerStringifier;

$stringifier = HandlerStringifier::create();

echo $stringifier->stringify($value);

Examples

use function Respect\Stringifier\stringify;

echo stringify('string') . PHP_EOL;
// "string"

echo stringify(implode(PHP_EOL, ['Multi-line', 'string'])) . PHP_EOL;
// "Multi-line\nstring"

echo stringify(1) . PHP_EOL;
// 1

echo stringify(0.5) . PHP_EOL;
// 0.5

echo stringify(true) . PHP_EOL;
// `true`

echo stringify(false) . PHP_EOL;
// `false`

echo stringify(null) . PHP_EOL;
// `null`

echo stringify(INF) . PHP_EOL;
// `INF`

echo stringify(-INF) . PHP_EOL;
// `-INF`

echo stringify(acos(8)) . PHP_EOL;
// `NaN`

echo stringify([1, 2, 3]) . PHP_EOL;
// `[1, 2, 3]`

echo stringify(['foo' => true, 'bar' => 42, 'baz' => ['qux' => INF, 'quux' => null]]) . PHP_EOL;
// `["foo": true, "bar": 42, "baz": ["qux": INF, "quux": null]]`

echo stringify(tmpfile()) . PHP_EOL;
// `resource <stream>`

echo stringify(new WithProperties()) . PHP_EOL;
// `WithProperties { +$publicProperty=true #$protectedProperty=42 -$privateProperty="something" }`

echo stringify(new WithUninitializedProperties()) . PHP_EOL;
// `WithUninitializedProperties { +$uninitializedProperty=*uninitialized* }`

echo stringify(new class { public int $property = 42; }) . PHP_EOL;
// `class { +$property=42 }`

echo stringify(new class extends WithProperties { }) . PHP_EOL;
// `WithProperties@anonymous { +$publicProperty=true #$protectedProperty=42 }`

echo stringify(fn(int $foo): string => '') . PHP_EOL;
// `Closure { fn(int $foo): string }`

echo stringify(new DateTime()) . PHP_EOL;
// `DateTime { 2023-04-21T11:29:03+00:00 }`

echo stringify(new DateTimeImmutable()) . PHP_EOL;
// `DateTimeImmutable { 2023-04-21T11:29:03+00:00 }`

echo stringify(new Fiber('strlen')) . PHP_EOL;
// `Fiber { strlen(string $string): int }`

echo stringify((static fn(int $number) => yield $number)(7)) . PHP_EOL;
// `Generator { current() => 7 }`

echo stringify(new ConcreteIterator()) . PHP_EOL;
// `ConcreteIterator { current() => 1 }`

echo stringify(new ConcreteStringable()) . PHP_EOL;
// `ConcreteStringable { __toString() => "This is the return of __toString()" }`

echo stringify(new ConcreteJsonSerializable()) . PHP_EOL;
// `ConcreteJsonSerializable { jsonSerialize() => {"0":1,"1":2,"2":3,"foo":true} }`

echo stringify(new WithDebugInfo()) . PHP_EOL;
// `WithDebugInfo { __debugInfo() => ["info": "This is the return of __debugInfo()"] }`

echo stringify(new ArrayObject([1, 2, 3])) . PHP_EOL;
// `ArrayObject { getArrayCopy() => [1, 2, 3] }`

echo stringify(new RuntimeException()) . PHP_EOL;
// `RuntimeException { in file.php:119 }`

echo stringify(new InvalidArgumentException('This is the exception message')) . PHP_EOL;
// `InvalidArgumentException { "This is the exception message" in file.php:112 }`

To see more examples of how to use the library check the integration tests.

Custom stringifiers

Stringifier library is extensible, you can create your own stringifiers and handlers. Considering the internal design, it's best to create an implementation of Handler, and then use it to create a HandlerStringifier.

use Respect\Stringifier\DumpStringifier;
use Respect\Stringifier\Handler;
use Respect\Stringifier\Handlers\CompositeHandler;
use Respect\Stringifier\HandlerStringifier;
use Respect\Stringifier\Stringify;

$compositeHandler = CompositeHandler::create();
$compositeHandler->prependStringifier(new class implements Handler {
    public function handle(mixed $raw, int $depth): ?string
    {
        if (is_object($raw) && method_exists($raw, 'toString')) {
            return $raw->toString();
        }

        return null;
    }
});

$stringifier = new HandlerStringifier($compositeHandler, new DumpStringifier());

echo $stringifier->stringify(new class {
    public function toString(): string
    {
        return 'Hello, world!';
    }
});
// Hello, world!

The DumpStringifier is a fallback stringifier that uses print_r-like output. You can replace it with any other.

respect/stringifier 适用场景与选型建议

respect/stringifier 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24.51M 次下载、GitHub Stars 达 26, 最近一次更新时间为 2017 年 12 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「respect」 「stringifier」 「stringify」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 respect/stringifier 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 24.51M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 27
  • 点击次数: 29
  • 依赖项目数: 7
  • 推荐数: 0

GitHub 信息

  • Stars: 26
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-12-28