3

为什么要写转换程序

最近在做旧版程序迁移,旧的架构为常规的MVC模式,新版架构全部改成restful架构。

由于改版数据是一致的,但是请求结构不一致,新版的请求全部以json形式提交,为了方便测试,之前一直都是直接在浏览器打开开发者工具,然后把请求内容复制过来,然后手动改成json形式,由于之前数据量比较少,暂时未发现测试时候不方便的情况,但是今天遇到了数据比较多的情况,于是我想,为啥不写一段转换程序呢?

本身来说,程序的初衷应该是提高工作效率,这也是我之前缺少反思的地方,因此写下这篇文章做个记录。

代码

废话不多说,直接上代码,有啥不足的地方,后续有时间再做研究。

<?php
// 文件路经
$f = './t.data';

$content = file_get_contents($f);

$rows = explode("\n", $content);

$data = [];
foreach ($rows as $row) {
    // 分割key和value
    $rowInfo = explode(':', $row);
    $keyInfo = array_shift($rowInfo);
    $value   = trim(implode(':', $rowInfo));

    $keyArray = getCharArray($keyInfo);
    $keyItem  = '';
    $keys     = [];
    foreach ($keyArray as $char) {
        if ($char == '[') {
            $keys[]  = $keyItem;
            $keyItem = '';
        } elseif ($char == ']') {
            continue;
        } else {
            $keyItem .= $char;
        }
    }
    $keys[] = $keyItem;

    // 驼峰转下划线
    $keys = array_map(function ($item) {
        return strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $item));
    }, $keys);

    $firstKey = array_shift($keys);
    if (empty($keys)) {
        $data[$firstKey] = $value;
    } else {
        if (!isset($data[$firstKey])) {
            $data[$firstKey] = [];
        }
        $item = &$data[$firstKey];
        while (!empty($keys)) {
            $currentKey = array_shift($keys);
            if (!isset($item[$currentKey])) {
                $item[$currentKey] = [];
            }
            $item = &$item[$currentKey];
        }
        $item = $value;
        unset($item);
    }
}
echo json_encode($data, JSON_UNESCAPED_UNICODE);

/**
 * 字符串转字符数组
 *
 * @param string $string
 *
 * @return array
 */
function getCharArray(string $string)
{
    $strlen    = mb_strlen($string);
    $i         = 0;
    $charArray = [];
    while ($i < $strlen) {
        $charArray[] = mb_substr($string, $i, 1);
        $i++;
    }
    return $charArray;
}

示例文件:

thumbnail: https://avatar-static.segmentfault.com/393/562/3935623170-1140000000142909
name: 101 新手上路
description: 面向新手开发者的问题集中营
sidebar: 欢迎来到 SegmentFault 101 新手上路问答。如果你是开发新手,有各种各样的疑惑,但又害怕提出来被嘲笑,欢迎在此提问。本区禁止嘲讽,请大家认真解答问题。
admins[0][user_id]: 1030000000094051
admins[0][isNoticeSite]: 0
admins[0][isNoticeMail]: 0
admins[1][user_id]: 1030000000091305
admins[1][isNoticeSite]: 0
admins[1][isNoticeMail]: 0
admins[2][user_id]: 1030000000146026
admins[2][isNoticeSite]: 0
admins[2][isNoticeMail]: 0
admins[3][user_id]: 1030000000141721
admins[3][isNoticeSite]: 0
admins[3][isNoticeMail]: 0
admins[4][user_id]: 1030000000142354
admins[4][isNoticeSite]: 0
admins[4][isNoticeMail]: 0
admins[5][user_id]: 1030000000144141
admins[5][isNoticeSite]: 0
admins[5][isNoticeMail]: 0
admins[6][user_id]: 1030000000091353
admins[6][isNoticeSite]: 0
admins[6][isNoticeMail]: 0

输出json

{
    "thumbnail": " https:\/\/avatar-static.segmentfault.com\/393\/562\/3935623170-1140000000142909",
    "name": " 101 新手上路",
    "description": " 面向新手开发者的问题集中营",
    "sidebar": " 欢迎来到 SegmentFault 101 新手上路问答。如果你是开发新手,有各种各样的疑惑,但又害怕提出来被嘲笑,欢迎在此提问。本区禁止嘲讽,请大家认真解答问题。",
    "admins": [
        {
            "user_id": " 1030000000094051",
            "is_notice_site": " 0",
            "is_notice_mail": " 0"
        },
        {
            "user_id": " 1030000000091305",
            "is_notice_site": " 0",
            "is_notice_mail": " 0"
        },
        {
            "user_id": " 1030000000146026",
            "is_notice_site": " 0",
            "is_notice_mail": " 0"
        },
        {
            "user_id": " 1030000000141721",
            "is_notice_site": " 0",
            "is_notice_mail": " 0"
        },
        {
            "user_id": " 1030000000142354",
            "is_notice_site": " 0",
            "is_notice_mail": " 0"
        },
        {
            "user_id": " 1030000000144141",
            "is_notice_site": " 0",
            "is_notice_mail": " 0"
        },
        {
            "user_id": " 1030000000091353",
            "is_notice_site": " 0",
            "is_notice_mail": " 0"
        }
    ]
}

kumfo
6.7k 声望4.1k 粉丝

程序生存法则: