承接 vrok/import-export 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vrok/import-export

Composer 安装命令:

composer require vrok/import-export

包简介

Helper to import from / export to JSON object graphs.

README 文档

README

Doctrine Helper to import from / export to JSON object graphs

CI Status Coverage Status

Uses Symfony's PropertyAccess to convert objects to arrays or generate objects from arrays. Which classes & properties can be exported/imported (and how) can be controlled with the ExportableProperty and ImportableProperty PHP attributes: Only classes that have at least one ExportableProperty can be converted to array, only classes that have at least one ImportableProperty can be instantiated from array. Uses Doctrines ClassUtils to safely handle proxies.

Usage

Short Example, for more details see ExportEntity / ImportEntity and ExportHelperTest / ImportHelperTest for all features. Allows to export referenced entities (or only their identifiers) and collections.

use Vrok\ImportExport\ExportableProperty;
use Vrok\ImportExport\ExportHelper;
use Vrok\ImportExport\ImportableProperty;
use Vrok\ImportExport\ImportHelper;

class Entity
{
    #[ExportableProperty]
    #[ImportableProperty]
    public int $id = 0;

    #[ExportableProperty]
    #[ImportableProperty]
    public ?\DateTimeImmutable $timestamp = null;
}

$entity = new Entity();
$entity->id = 1;
$entity->timestamp = new \DateTimeImmutable();

$helper = new ExportHelper();
$export = $helper->objectToArray($entity);
$exportList = $helper->collectionToArray([$entity]);

/*
  $export === [
    'id'        => 1,
    'timestamp' => '2022-03-23....',
  ]
*/

$helper = new ImportHelper();
$newInstance = $helper->objectFromArray($export, Entity::class);
$newInstances = $helper->collectionFromArray([$export], Entity::class);

Features

Export

  • can output object graphs, if a property pointing to another entity or DTO is marked with ExportableProperty
  • can export an identifier, to be later mapped to the actual record, by using the referenceByIdentifier argument of the ExportableProperty attribute, e.g. instead of exporting the whole User, only it's ID could be exported for the createdBy property of another record.
  • can export collections of objects (either by using collectionToArray or when a property is a Doctrine Collection or an array and marked with asList), even when they are of different types (e.g. through inheritance). An _entityClass field will contain the actual class.
  • can limit exported data to only allowed properties by using the propertyFilter argument of fromArray
  • can prevent (otherwise exportable property) from being exported by using the propertyFilter argument of fromArray and setting isExcludeFilter to true

Import

  • can reference existing records: if a property (collection or single object) points to an "importable entity" and the dataset contains an int/string the given ObjectManager is used to fetch the specified record from the database
  • handle object graphs:
    {
      "name": "base entity",
      "nested": {
        "description": "for a property with type hint",
        "child": {
          "value": "for a property that is e.g. typed as an Interface",
          "_entityClass": "\\My\\DtoClass"
        } 
      }
    }
  • import only permitted fields (even when the dataset contains more, potentially importable properties) by using the propertyFilter argument of fromArray
  • exclude fields from importing (even when they are in the dataset and potentially importable) by using the propertyFilter argument of fromArray and setting isExcludeFilter to true
  • Automatically persists generated entities when they are marked for identity mapping, keeps an internal map of old ⇒ new identifiers. So when a reference from entity A to entity B is found in a later import object, the identity map is checked for matches, to update the reference to the new identifier.

Dependencies

  • doctrine/common for the ClassUtils (proxy handling) and Collection properties
  • doctrine/persistence for the ObjectManager (importing references)
  • symfony/property-access for setting/reading (private, protected) properties
  • dev: symfony/uuid for testing import of UUID values

Future Improvements

  • when only doctrine/orm ^4.0 is supported, remove the "if" in AbstractOrmTestCase
  • what could be done with Symfony's serializer?
    • can already transform object (graphs) to array
    • properties can be marked with groups to be exportable
    • how to mark properties to only be exported as identifier? new attribute? how implement that, how is APIP doing this?
    • how to mark array properties to be treated as DTO-List?
    • how to ignore some properties without the need to hardcode groups? (exclude-filter)
    • how to limit to some properties without the need to hardcode groups? (include-filter)
  • evaluate Doctrine's ORM collection attributes to check for allowed element class
  • Improve union type handling (e.g. multiple base types: int|string)
  • not only export to arrays but also directly to files
    • support exporting to multiple files (split to reduce size for large datasets)
  • not only import from arrays but also directly from JSON files
    • support importing from multiple files (have been split to reduce size)

vrok/import-export 适用场景与选型建议

vrok/import-export 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 695 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 01 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「json」 「import」 「doctrine」 「export」 「object graph」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 vrok/import-export 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 vrok/import-export 我们能提供哪些服务?
定制开发 / 二次开发

基于 vrok/import-export 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 695
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-23