2017 © Pedro Pelรกez
 

library automapper

Simple declarative data mapper for PHP 7

image

acelot/automapper

Simple declarative data mapper for PHP 7

  • Friday, July 13, 2018
  • by acelot
  • Repository
  • 2 Watchers
  • 47 Stars
  • 79 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 6 Versions
  • 39 % Grown

The README.md

AutoMapper

travis Code Climate packagist depslicense, (*1)

Simple declarative data mapper for PHP 7., (*2)

AutoMapper can map data from array/object to existing array/object or marshal new ones. Mapping rules specified in declarative way using three simple definitions: - From definition (From::create or via short function from) โ€” maps single field from source to target. Supports chainable functions: - ->convert(callable $callable) โ€” converts input value to another one via any callable; - ->trim() โ€” trims value to eliminate whitespaces (suitable for strings); - ->default($defaultValue) โ€” returns default value if source field is missing; - ->ignoreMissing() โ€” ignores target field if source field is missing; - ->ignoreEmpty() โ€” ignores target field if source field is empty. - Aggregate definition (Aggregate::create or via short function aggregate) โ€” maps multiple fields from source to single target field. Supports chainable functions: - ->trim() โ€” trims aggregated value - ->ignoreEmpty() โ€” ignores target field if aggregated value is empty. - Ignore definition (Ignore::create or via short function ignore) โ€” simply ignores the field. - Value definition (Value::create or via short function value) โ€” maps constant value to target field. Supports chainable functions: - ->trim() - ->ignoreEmpty(), (*3)

All missing source fields can be ignored via AutoMapper::create(...)->ignoreAllMissing() modifier., (*4)

Install

composer require acelot/automapper

Example

use function Acelot\AutoMapper\{
    field, from, value, aggregate
}

// Define mapper
$mapper = AutoMapper::create(
    field('id', from('id')->convert('intval')),
    field('title', from('text')->trim()),
    field('url', from('link')->trim()),
    field('isActive', from('is_active')->convert(function ($value) {
        return $value === 1;
    })->default(false)),
    field('count', value(100)),
    field('isEmpty', aggregate(function (SourceInterface $source) {
        return !empty($source->get('title')) && !empty($source->get('url'));
    })),
    field('ignoreThisField', ignore())
)->ignoreAllMissing();

// Source data
$source = [
    'id' => '100',
    'text' => 'Very useful text. ',
    'is_active' => 0
];

// Target
$target = $mapper->marshalArray($source);
var_dump($target);

Output:, (*5)

array(5) {
  ["id"]=>
  int(100)
  ["title"]=>
  string(17) "Very useful text."
  ["isActive"]=>
  bool(false)
  ["count"]=>
  int(100)
  ["isEmpty"]=>
  bool(false)
}

The Versions

13/07 2018

dev-master

9999999-dev https://github.com/acelot/automapper

Simple declarative data mapper for PHP 7

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

mapper automapper data-mapper

13/07 2018

1.1.0

1.1.0.0 https://github.com/acelot/automapper

Simple declarative data mapper for PHP 7

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

mapper automapper data-mapper

29/11 2017

1.0.3

1.0.3.0 https://github.com/acelot/automapper

Simple declarative data mapper for PHP 7

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

mapper automapper data-mapper

29/11 2017

1.0.2

1.0.2.0 https://github.com/acelot/automapper

Simple declarative data mapper for PHP 7

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

mapper automapper data-mapper

29/11 2017

1.0.1

1.0.1.0 https://github.com/acelot/automapper

Simple declarative data mapper for PHP 7

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

mapper automapper data-mapper

28/11 2017

1.0.0

1.0.0.0 https://github.com/acelot/automapper

Simple declarative data mapper for PHP 7

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

mapper automapper data-mapper