exayer/vdf-converter
Composer 安装命令:
composer require exayer/vdf-converter
包简介
Efficient, fast Valve Data Format (*.vdf) parser
关键字:
README 文档
README
VDF Converter
A memory efficient parser for the Valve Data Format (*.vdf) written in PHP.
Fully supports the VDF specification, except for the #include macro.
Requirements
PHP 7 or later. No production dependencies.
Install
You can install the package via composer:
composer require exayer/vdf-converter
Usage
Convert VDF to generator/array
Let's say we are parsing the following VDF:
$vdf = '{ "mercury" { "distance" "58" } "venus" { "distance" "108" } "earth" { "distance" "149" } }';
It can be parsed in one of these ways (based on input source):
<?php use EXayer\VdfConverter\VdfConverter; // $vdf declaration $planets = VdfConverter::fromString($vdf); $planets = VdfConverter::fromFile('data://text/plain,' . $vdf); // usually filename here $tempFile = tmpfile(); fwrite($tempFile, $vdf); fseek($tempFile, 0); $planets = VdfConverter::fromStream($tempFile); $planets = VdfConverter::fromIterable([substr($vdf, 0, -60), substr($vdf, -60)]);
To get the data you need to iterate over generator using foreach
foreach ($planets as $name => $data) { // #1 iteration: $name === "mercury" $data === ["distance" => "58"] // #2 iteration: $name === "venus" $data === ["distance" => "108"] // #3 iteration: $name === "earth" $data === ["distance" => "149"] }
Or simply convert it to array
$result = iterator_to_array($planets); // // $result = [ // "mercury" => [ // "distance" => "58" // ] // "venus" => [ // "distance" => "108" // ] // "earth" => [ // "distance" => "149" // ] // ]
Duplicate key handling
Some VDFs are known to contain duplicate keys.
To keep the result structure the same as the VDF and since the parser is generator based (not keeping in memory pairs)
the duplicated key will be modified - the counter will be concatenated to the end (e.g. key__[2]).
$vdf = '{ "mercury" { "distance" "58" "velocity" "35" "distance" "92" } "mercury" { "distance" "108" } "earth" { "distance" "149" } }'; $result = iterator_to_array(VdfConverter::fromString($vdf)); // // $result = [ // "mercury" => [ // "distance" => "58" // "velocity" => "35" // "distance__[2]" => "92" // ] // "mercury__[2]" => [ // "distance" => "108" // ] // "earth" => [ // "distance" => "149" // ] // ]
Customizing key format
If you want to customize the formatting key process, you can create your own custom formatter. A formatter is any class that implements EXayer\VdfConverter\UniqueKey\Formatter.
This is what that interface looks like.
namespace EXayer\VdfConverter\UniqueKey; interface Formatter { public function buildKeyName(string $key, int $index): string; }
After creating your formatter, you can specify its class name in the uniqueKeyFormatter method of the EXayer\VdfConverter\VdfConverterConfig object. The config can be passed as second argument to any from builder method. Your formatter will then be used by default for all duplicate key handling calls.
$config = VdfConverterConfig::create() ->uniqueKeyFormatter(YourCustomFormatter::class); $iterator = VdfConverter::fromString($vdf, $config);
Warning: Do not create formatters that create a key like __2, as some VDFs may have keys in this format.
Testing
composer test
Features to implement
- Key Pointer - Parse only the specific part of the VDF based on the path built from the keys (e.g. 'key1.key2.key3').
Inspiration
The code is inspired by @halaxa package json-machine. Thank you!
License
The MIT License (MIT). Please see License File for more information.
exayer/vdf-converter 适用场景与选型建议
exayer/vdf-converter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 69 次下载、GitHub Stars 达 4, 最近一次更新时间为 2021 年 09 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「parser」 「format」 「valve」 「vdf」 「keyvalues」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 exayer/vdf-converter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 exayer/vdf-converter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 exayer/vdf-converter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides a simple decimal/roman number converter
Softonic OpenApi validation formats extension
Tools to convert between .NET and PHP date formats
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
A correct and simple implementation of OpenID authentication for Steam
Adds the EDTF data type to Wikibase
统计信息
- 总下载量: 69
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-09-29
