定制 coderofsalvation/datamapper-minimal 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

coderofsalvation/datamapper-minimal

Composer 安装命令:

composer require coderofsalvation/datamapper-minimal

包简介

Easily transform arrays and objects with different layouts into one format

README 文档

README

Easily transform arrays and objects with different layouts into one format. Think importing data from several sources in different formats to generate statistics or exports.

Usage

$ composer require coderofsalvation/datamapper-minimal

And then

<? 
  use coderofsalvation\DataMapper;
  $datamapper = new DataMapper();
?> 

Lets say this is our desired object format:

class MyObject {
  public $name;
  public $id;
}

But source A provide these object formats:

[{ "short_name": "foo", "ID": 12 },{ "short_name": "bar", "ID":13}]

And oh no! source B has this object format:

<items> 
  <item id="15">
    <Name>Boo</Name>
  </item>
</items>       

This is going to be a mess...or not? Nope, we can just define transformations and convert them in batch:

$datamapper->addMapping("A", array(
  array( "source" => "short_name", "destination" => "name", "transform" => function($s,&$d){ return $s->short_name;    } ), 
  array( "source" => "id",         "destination" => "id",   "transform" => function($s,&$d){ return $s->id;            } ) 
));

$datamapper->addMapping("B", array(
  array( "source" => "Name", "destination" => "name",       "transform" => function($s,&$d){ return $s->Name;                } ), 
  array( "source" => "Id",   "destination" => "id",         "transform" => function($s,&$d){ return $s->getAttribute("id");  } )
));

// lets do it!
$items = [];
foreach( $Aitems as $item ) $items[] = $datamapper->map("A", $item, new MyObject() );
foreach( $Bitems as $item ) $items[] = $datamapper->map("B", $item, new MyObject() );

print_r($items);
// voila there you go :D

License

BSD

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2015-08-21