keboola/csvmap
Composer 安装命令:
composer require keboola/csvmap
包简介
Flatten an object into a CSV file(s)
README 文档
README
Installation
composer require keboola/csvmap
Usage
For example, map key key.nested of each $data array item, to CSV column 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();
Mapping
- The mapping is an array.
- The
keycorresponds to the one root/nested key in the source data. Default delimiter is., eg.key,key.nested. - The
valueis the mapping configuration for the given key. It isstring(shorthand notation) orarray.- There are 3 types of the mapping, defined by the
typekey: type(optional),columnby default.columnwill store the value from its key into a CSV columnuserwill look into an array in the second argument of the parse function and fill a CSV column with its valuetablewill create a "child" CSV and link through a primary key or a hash, if no primary key is defined
- There are 3 types of the mapping, defined by the
Column mapping
mapping: Required, must containdestination:destination: Target column in the output CSV fileprimaryKey: Optional, boolean. If set to true, the column will be included in the primary key
forceType: Optional, if a value is not scalar, it'll be JSON encoded
Shorthand notation
- If the
valueis thestring, then it is a shorthand notation for thecolumnmapping. - The string value corresponds to
mapping.destination.
Example shorthand notation:
[
'key.nested' => 'mappedKey'
]
... is equal to
[
'key.nested' => [
'type' => 'column',
'mapping' => [
'destination' => 'mappedKey'
]
]
]
Examples
Four different column mappings.
[
'id' => [
'type' => 'column',
'mapping' => [
'destination' => 'id',
'primaryKey' => true,
]
],
'name' => 'name',
'info.url' => 'url,
'info.tags' => [
'type' => 'column',
'forceType' => true,
'mapping' => [
'destination' => 'tags'
]
]
]
User mapping
Same as column, except the key of the object is not searched for in the parsed data, but in an array passed to the parser to inject user data
Table mapping
destination: Required, a target CSV file nametableMapping: Required, mapping of all child table's columns- Sub-mapping has the same structure as the root
$mapping.
- Sub-mapping has the same structure as the root
parentKey: Optional, can be used to set the parent/child link as a primary key in the child or override the link's column name in the childprimaryKey: boolean, same as incolumndestination: Name of the link column (if not used, name of the parent table ._pkis used by default)disable: boolean, if set to non-false value, the parent key in the child table, as well as the column in the parent will not be saved
Note:
If the destination is the same as the current parsed 'type' (destination of the parent),
parentKey.disable must be true to preserve consistency of structure of the child and parent.
Map scalar items to a separated CSV
- Table mapping is useful when you need to map array of the objects to separate CSV tables.
- But sometimes you need to map an array of the scalar (not object) values, for example a list of tags.
- In this case, you can use an empty key in
tableMappingto map a scalar value.
For example, we have this data:
[
['id' => 1, 'name' => 'dog', 'tags' => ['useful', 'pet', 'animal']],
['id' => 2, 'name' => 'mouse', 'tags' => ['harmful', 'animal']]
]
Example mapping:
[
'id' => [
'type' => 'column',
'mapping' => [
'destination' => 'id',
'primaryKey' => true,
]
],
'name' => 'name',
'tags' => [
'type' => 'table',
'destination' => 'tags',
'tableMapping' => [
'' => 'tagName' // empty key used to map scalar value
]
]
]
Results:
root.csv:
"id","name" "1","dog" "2","mouse"
tags.csv:
"tagName","root_pk" "useful","1" "pet","1" "animal","1" "harmful","2" "animal","2"
Examples
Mixed column and table mappings.
[
'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'
]
]
]
]
]
License
MIT licensed, see LICENSE file.
keboola/csvmap 适用场景与选型建议
keboola/csvmap 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.01k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2016 年 03 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「csv」 「object」 「flatten」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 keboola/csvmap 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 keboola/csvmap 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 keboola/csvmap 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Aplus Framework Image Library
Non-standard PHP library (NSPL) - functional primitives toolbox and more
A php package to flatten nested json objects and nested arrays. It also allows you to create csv files from the flattened data.
A set of useful PHP classes.
Runn Me! Value Objects Library
Flattens a multi-dimensional array or any \Traversable into a one-dimensional array. Elevates an one-dimensional or any \Traversable to a multi-dimensional array.
统计信息
- 总下载量: 5.01k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 29
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-03-29