承接 exayer/vdf-converter 相关项目开发

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

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

exayer/vdf-converter

Composer 安装命令:

composer require exayer/vdf-converter

包简介

Efficient, fast Valve Data Format (*.vdf) parser

README 文档

README

VDF Converter

Tests Latest Stable Version

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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-09-29