2017 © Pedro Peláez
 

library datamapper-minimal

Easily transform arrays and objects with different layouts into one format

image

coderofsalvation/datamapper-minimal

Easily transform arrays and objects with different layouts into one format

  • Friday, August 21, 2015
  • by coderofsalvation
  • Repository
  • 1 Watchers
  • 1 Stars
  • 7 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

DataMapper-Minimal

, (*1)

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., (*2)

Usage

$ composer require coderofsalvation/datamapper-minimal

And then, (*3)

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

Lets say this is our desired object format:, (*4)

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

But source A provide these object formats:, (*5)

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

And oh no! source B has this object format:, (*6)

<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:, (*7)

$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, (*8)

The Versions

21/08 2015

dev-master

9999999-dev

Easily transform arrays and objects with different layouts into one format

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0