artkoder / kvpparser
一个用于读取和解析键值对文件的库。
v0.1.5
2023-09-27 07:54 UTC
Requires (Dev)
- phpunit/phpunit: ^10.3
README
一个用于读取和解析键值对文件的库。
什么是KVP文件
一个KVP文件由空行分隔的键值对记录组成。
以下是一个示例
date: 2023-01-01
description: Electric company example Inc.
category: Electricity
amount: 120
date: 2023-02-01
description: Real estate example Inc.
category: Rent
amount: 1500
以下是一个带有注释的不同示例。以#开头的行将被解析器忽略。
# Budget report routes
name: budget-report
path: /budget/{start}/{end}
method: get
controller: ArtKoder\Peasy\Controllers\Budget::report
name: budget-index
path: /budget
method: get
controller: ArtKoder\Peasy\Controllers\Budget::index
我开始在不同的项目中使用这种类型的文件,因为它类似于yaml,但没有嵌套和解析复杂度。所以我决定构建一个库来使代码可重用。
安装
使用composer
composer require artkoder/kvpparser
如何使用此库
以下是一些示例
解析单个文件
假设我们有一个名为budget.kvp的文件。
date: 2023-01-01
description: some description
category: some category
amount: 1000
date: 2023-01-02
description: another description
category: another category
amount: 2000
date: 2023-01-03
description: one more description
category: one more category
amount: 3000
我们可以通过运行以下代码来解析文件并接收一个关联数组
$data = KvpParser::parseFile(__DIR__ . '/../data/budget.kvp'); print_r($data);
以下是输出
Array
(
[0] => Array
(
[date] => 2023-01-01
[description] => some description
[category] => some category
[amount] => 1000
)
[1] => Array
(
[date] => 2023-01-02
[description] => another description
[category] => another category
[amount] => 2000
)
[2] => Array
(
[date] => 2023-01-03
[description] => one more description
[category] => one more category
[amount] => 3000
)
)
递归地解析目录中的多个文件
您可以指定一个目录。KvpParser将解析该目录及其子目录中找到的所有文件。
$directory = '/path/to/your/data'; $data = KvpParser::parseRecursive($directory);
默认情况下,KvpParser将查找扩展名为.kvp的文件。但您可以指定任何文件扩展名。例如
$directory = '/path/to/your/data'; $includExtension = ['kvp', 'dat']; $data = KvpParser::parseRecursive($directory, $includeExtensions);
将CSV文件转换为KVP文件
这非常方便。假设您有一个包含以下数据的银行对账单的csv文件
"Account Type","Account Number","Transaction Date","Cheque Number","Description 1","Description 2","CAD$","USD$"
Chequing,123-4567,6/12/2023,,"C - IDP REFUND-0819","COSTCO WHOLESAL ",183.95,
Chequing,123-4567,6/12/2023,,"COSTCO WHOLESAL","IDP PURCHASE - 0746 ",-276.94,
Chequing,123-4567,6/13/2023,,"AUTOMOBILE RENT","TOYOTA FINANCE ",-129.12,
假设我们只对以下数据感兴趣:日期、描述1和CAD$。但我们还希望在结果.kdp文件中更改列名。
以下是我们可以做的
$csvFile = __DIR__ . '/../data/transactions.csv'; $kdpFile = __DIR__ . '/../data/transactions.kdp'; $columnMap = [ "Date" => "Transaction Date", "Description" => "Description 1", "Amount" => "CAD$" ]; KvpParser::csvToKvp($csvFile, $kdpFile, $columnMap);
以下是transactions.kdp的内容
Date: 6/12/2023
Description: C - IDP REFUND-0819
Amount: 183.95
Date: 6/12/2023
Description: COSTCO WHOLESAL
Amount: -276.94
Date: 6/13/2023
Description: AUTOMOBILE RENT
Amount: -129.12
如果没有指定$columnMap,csv文件中的列名将成为kvp文件中属性的名称。
有时您需要在映射过程中处理数据,我们可以通过将闭包添加到映射数组中来实现这一点。
在我们的上一个示例中,假设您想合并两个描述列并将日期格式化为Y-m-d。
以下是您应该怎么做
function formatDate($data) { $inputDate = $data['Transaction Date']; $inputFormat = 'm/d/Y'; $outputFormat = 'Y-m-d'; // Create a DateTime object from the input date with the specified format $dateTime = DateTime::createFromFormat($inputFormat, $inputDate); // Check for errors in parsing the date if ($dateTime === false) { return false; // Return false if the parsing fails } // Format the DateTime object in the desired output format $formattedDate = $dateTime->format($outputFormat); return $formattedDate; } $columnMap = [ "Date" => 'formatDate', "Account" => "Account Number", "Description" => function($data) { return $data['Description 1'] . " - " . $data['Description 2']; }, "Amount" => "CAD$", "Category" => "uncategorized"]; KvpParser::csvToKvp($csvFile, $kdpFile, $columnMap);
请注意,列uncategorized不存在。在这种情况下,将使用单词uncategorized作为值。