juanchosl/datatransfer
Composer 安装命令:
composer require juanchosl/datatransfer
包简介
A small, lightweight utility to read values and properties from distinct sources using the same methodology
关键字:
README 文档
README
Description
A small, lightweight utility to read values and properties from distinct sources using the same methodology and convert it to a standard DTO. Include a second tool that can convert any of DTOs to any of the know formats, creating a simple format converter from format A to format B.
Install
composer require juanchosl/datatransfer composer update
How use it
Load composer autoload and use it
Data Transfer Objects
Using the provided Factory
The $element parameter can be:
- array indexed or associtive
- object that implements JsonSerializable interface
- string with serialized object or array
- filepath containing a serialized object or array
- json encoded object or array
- json filepath with json extension containing a json encoded object or array
- SimpleXMLElement object
- Excel filepath with xlsx extension
- ODS string or filepath with ods extension
- XML string or filepath with xml extension
- CSV string or filepath with csv extension
- INI string or filepath with ini extension
- TAB separated values string or filepath with tsv extension
- YAML string or filepath with yml or yaml extension (require yaml php extension)
- primitive value
Using a filepath
Open and convert the contents included into file, alternatively you can specify the Format if the extension is not knowed
$dto = JuanchoSL\DataTransfer\Factories\DataTransferFactory::byFile($element, Format $original_format = null);
Using a string
Try to detect and convert the contents included into string (json encoded, serialized object or array, xml, csv, yaml), alternatively you can specify the Format if the type is INI
$dto = JuanchoSL\DataTransfer\Factories\DataTransferFactory::byString($element, Format $original_format = null);
Using a mime-type
You can try passing an iterable number of strings as standard mimetypes ir order to process with the first compatible
$dto = JuanchoSL\DataTransfer\Factories\DataTransferFactory::byMimeType($element, string|iterable $mime_types);
Using a trasversable element
Detect and convert the trasversable element (object or array)
$dto = JuanchoSL\DataTransfer\Factories\DataTransferFactory::byTrasversable($element);
Using a specific repository
Alternative you can use the distincts repositories
$dto = new JuanchoSL\DataTransfer\Repositories\{SOURCE_READER}($element)
Available origins and repositories
| Type | Compatibility | Reader |
|---|---|---|
| Array | filepath | array | string from array serialized | ArrayDataTransfer |
| stdClass | filepath | object | string from object serialized | ObjectDataTransfer |
| CSV | filepath | array of lines | string | CsvDataTransfer(,) or ExcelCsvDataTransfer(;) |
| INI | filepath | string | IniDataTransfer |
| TAB | filepath | string | TabsvDataTransfer |
| JSON | filepath | string | JsonDataTransfer |
| XML | filepath | string | SimpleXmlElement | XmlDataTransfer |
| YAML | filepath | string | YamlDataTransfer |
| XLSX | filepath | string | ExcelXlsxDataTransfer |
| ODS | filepath | string | OdsDataTransfer |
The $element parameter needs to be the required type for the selected repo
Is more efective use a magic factory in order to avoid know the type of the original data, and you can change it with no need to adapt your existing code.
The resultant $dto variable is a recursive data transfer object, iterable, countable, clonable and json serializable
Use data
$dto = DataTransferFactory::create(['key' => 'value']); echo $dto->has('key'); //true echo $dto->get('key'); //value echo $dto['key']; //value echo $dto->key; //value echo $dto->has('other_key'); //false $dto->other_key = 'other_value';// alias for $dto->set('other_key','other_value') echo $dto->has('other_key'); //true echo $dto->other_key; //other_value
Data Converters
You can convert any DataTransferObject to a standar format, as:
- array
- stdClass
- SimpleXMLElement
- json
- xml
- ini
- yaml
- csv
- tsv (tab separated)
- excel csv
- xlsx
Using the provided Factory
$json = JuanchoSL\DataTransfer\Factories\DataConverterFactory::asJson($dto);
Using a mime-type
You can try passing an iterable number of strings as standard mimetypes ir order to convert with the first compatible
$json = JuanchoSL\DataTransfer\Factories\DataConverterFactory::asMimeType($dto, 'application/json');
Using a specific converter
Alternative you can use the distincts converters
$converter = new JuanchoSL\DataTransfer\Converters\{SOURCE_CONVERTER}($dto); $result = $converter->getData();
$converter = new JuanchoSL\DataTransfer\Converters\{SOURCE_CONVERTER}(); $converter->setData($dto); $result = $converter->getData();
The available converters are:
- CsvConverter (,)
- ExcelCsvConverter(;)
- ExcelXlsxConverter
- JsonConverter
- XmlConverter
- YamlConverter
- IniConverter
- TabsvConverter
- ArrayConverter
- ObjectConverter
- XmlObjectConverter
juanchosl/datatransfer 适用场景与选型建议
juanchosl/datatransfer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 336 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 09 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「yaml」 「json」 「xml」 「xlsx」 「csv」 「ini」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 juanchosl/datatransfer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 juanchosl/datatransfer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 juanchosl/datatransfer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
Load DOM document safety
SUML for PHP
SUML support for Symfony
统计信息
- 总下载量: 336
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 28
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-09-27