Giter VIP home page Giter VIP logo

automapper's Introduction

AutoMapper

travis packagist deps license

Simple declarative data mapper for PHP 7.

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.
  • Value definition (Value::create or via short function value) — maps constant value to target field. Supports chainable functions:
    • ->trim()
    • ->ignoreEmpty()

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

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'));
    }))
)->ignoreAllMissing();

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

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

Output:

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

automapper's People

Contributors

acelot avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.