keboola / csvmap
将对象展平为 CSV 文件(们)
2.2.0
2023-04-13 07:04 UTC
Requires
- php: ^8.1
- ext-json: *
- keboola/csv: ^4.0
- keboola/php-csvtable: ^2.2
- keboola/php-utils: ^4.1
Requires (Dev)
- keboola/coding-standard: ^13.0
- php-parallel-lint/php-parallel-lint: ^1.3
- phpstan/phpstan: ^1.4
- phpunit/phpunit: >=7.5
- dev-master
- 2.2.0
- 2.1.0
- 2.0.0
- 1.1.0
- 1.0.0
- 0.6.0
- 0.5.0
- 0.4.0
- 0.3.0
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- 0.0.8
- 0.0.7
- 0.0.6
- 0.0.5
- 0.0.4
- 0.0.3
- 0.0.2
- 0.0.1
- dev-michaljurecko-patch-1
- dev-adamvyborny-CM-573-update-keboola/csv
- dev-adamvyborny-upgrade-keboola-csv
- dev-adamvyborny-upgrade-php
- dev-webrouse-COM-469-enhancements
- dev-webrouse-COM-487-update-csvtable
This package is auto-updated.
Last update: 2024-09-13 10:16:37 UTC
README
安装
composer require keboola/csvmap
用法
例如,将每个 $data
数组项的键 key.nested
映射到 CSV 列 mappedKey
。
$data = [
[
'id => '123',
'key' => [
'nested' => 'value1'
]
]
];
$mapping = [
'id' => [
'type' => 'column',
'mapping' => [
'destination' => 'id',
'primaryKey' => true,
]
],
'key.nested' => 'mappedKey'
];
$rootType = 'rootName';
$userData = [];
$parser = new Mapper($mapping, $writeHeader, $rootType);
$parser->parse($data, $userData);
$files = $parser->getCsvFiles();
$tempFilePath = $files['rootName']->getPathName();
映射
- 映射是一个数组。
key
对应于源数据中的一个根/嵌套键。默认分隔符是.
,例如key
,key.nested
。value
是给定键的映射配置。它可以是string
(简写表示法)或array
。- 映射有 3 种类型,由
type
键定义 type
(可选),默认为column
。column
将将其键的值存储到 CSV 列中user
将查找解析函数的第二个参数中的数组并在 CSV 列中填充其值table
将创建一个“子”CSV 并通过主键或散列链接,如果没有定义主键
- 映射有 3 种类型,由
列映射
mapping
:必需的,必须包含destination
destination
:输出 CSV 文件中的目标列primaryKey
:可选的,布尔值。如果设置为 true,则该列将包含在主键中
forceType
:可选的,如果值不是标量,则将其 JSON 编码
简写表示法
- 如果
value
是string
,则它是column
映射的简写表示法。 - 字符串值对应于
mapping.destination
。
示例简写表示法
[
'key.nested' => 'mappedKey'
]
... 等于
[
'key.nested' => [
'type' => 'column',
'mapping' => [
'destination' => 'mappedKey'
]
]
]
示例
四种不同的 column
映射。
[
'id' => [
'type' => 'column',
'mapping' => [
'destination' => 'id',
'primaryKey' => true,
]
],
'name' => 'name',
'info.url' => 'url,
'info.tags' => [
'type' => 'column',
'forceType' => true,
'mapping' => [
'destination' => 'tags'
]
]
]
用户映射
与 column
相同,但不会在解析的数据中搜索对象的 key,而是在传递给解析器的数组中搜索以注入用户数据
表映射
destination
:必需的,目标 CSV 文件名tableMapping
:必需的,所有子表列的映射- 子映射具有与根
$mapping
相同的结构。
- 子映射具有与根
parentKey
:可选的,可用于将父/子链接作为子表的主键或覆盖子链接的列名primaryKey
:布尔值,与column
中相同destination
:链接列的名称(如果未使用,则使用父表的名称,默认使用_pk
)disable
:布尔值,如果设置为非 false 值,则子表中的父键以及父表中的列将不会保存
注意
如果 destination
与当前解析的 'type'(父目标的 destination)相同,
parentKey.disable
必须 为 true 以保留子表和父表的结构一致性。
将标量项目映射到分离的 CSV
- 表映射在您需要将对象数组映射到单独的 CSV 表时很有用。
- 但有时您需要将标量(非对象)值数组映射到,例如标签列表。
- 在这种情况下,您可以在
tableMapping
中使用一个空键来映射标量值。
例如,我们有以下数据
[
['id' => 1, 'name' => 'dog', 'tags' => ['useful', 'pet', 'animal']],
['id' => 2, 'name' => 'mouse', 'tags' => ['harmful', 'animal']]
]
示例映射
[
'id' => [
'type' => 'column',
'mapping' => [
'destination' => 'id',
'primaryKey' => true,
]
],
'name' => 'name',
'tags' => [
'type' => 'table',
'destination' => 'tags',
'tableMapping' => [
'' => 'tagName' // empty key used to map scalar value
]
]
]
结果
root.csv
:
"id","name"
"1","dog"
"2","mouse"
tags.csv
:
"tagName","root_pk"
"useful","1"
"pet","1"
"animal","1"
"harmful","2"
"animal","2"
示例
混合 column
和 table
映射。
[
'id' => [
'type' => 'column',
'mapping' => [
'destination' => 'id',
'primaryKey' => true,
]
],
'name' => "name,
'addresses' => [
'type' => 'table',
'destination' => 'addresses',
'tableMapping' => [
'number' => 'number',
'street' => [
'type' => 'table',
'destination' => 'streets',
'tableMapping' => [
'name' => 'name'
]
]
]
]
]
许可
MIT许可,请参阅授权文件。