PHP code example of acelot / automapper
1. Go to this page and download the library: Download acelot/automapper library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
acelot / automapper example snippets
use Acelot\AutoMapper\Context;
use Acelot\AutoMapper\AutoMapper as a;
$source = [
'id' => '99',
'name' => [
'lastname' => 'Doe',
'firstname' => 'John',
],
'skills' => 'Php, CSS,JS,html, MySql, brainfuck,'
];
$result = a::marshalArray(
new Context(),
$source,
a::toKey('id', a::pipe(
a::get('[id]'),
a::toInt()
)),
a::toKey('fullname', a::pipe(
a::get('[name]'),
a::call(fn($v) => $v['firstname'] . ' ' . $v['lastname'])
)),
a::toKey('skills', a::pipe(
a::get('[skills]'),
a::explodeString(','),
a::mapIterable(a::pipe(
a::trimString(),
a::ifEmpty(a::ignore()),
a::call('strtolower')
)),
a::toArray(),
a::sortArray()
))
);
// Output of `var_export($result)`
array(
'id' => 99,
'fullname' => 'John Doe',
'skills' => [
0 => 'brainfuck',
1 => 'css',
2 => 'html',
3 => 'js',
4 => 'mysql',
5 => 'php',
],
)
use Acelot\AutoMapper\Context;
use Acelot\AutoMapper\AutoMapper as a;
$source = [
'title' => ' Product title ',
'desc' => [
'Product short description',
'Product regular description',
'Product descriptive description',
]
];
$target = [
'id' => 5,
'title' => 'Current title',
];
$result = a::map(
new Context(),
$source,
$target,
a::toKey('title', a::pipe(
a::get('[title]'),
a::trimString()
)),
a::toKey('description', a::get('[desc][#last]')),
);
// Output of `var_export($result)`
array (
'id' => 5,
'title' => 'Product title',
'description' => 'Product descriptive description',
)
[array_key][array key with spaces][#first][#last]->property->{property with spaces}->someMethod()