8000 GitHub - acelot/automapper at 1.0.2
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

acelot/automapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AutoMapper

travis packagist deps license

Simple declarative data mapper for PHP 7.

Install

composer require acelot/automapper

Usage

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)
}

About

Powerful declarative data mapper for PHP 8

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  
0