PHPackages                             acelot/automapper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. acelot/automapper

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

acelot/automapper
=================

Powerful declarative data mapper for PHP 8

2.0.0(3y ago)694.2k↑66.7%11MITPHPPHP ^8.0CI passing

Since Nov 28Pushed 1y ago5 watchersCompare

[ Source](https://github.com/acelot/automapper)[ Packagist](https://packagist.org/packages/acelot/automapper)[ Docs](https://github.com/acelot/automapper)[ RSS](/packages/acelot-automapper/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (9)Dependencies (4)Versions (13)Used By (1)

ℹ️ You are on a branch with the second version of the `acelot/automapper`. If you want a previous version, then proceed to [1.x](https://github.com/acelot/automapper/tree/1.x) branch.

![AutoMapper](./logo-light.png)

[![build](https://github.com/acelot/automapper/actions/workflows/pipeline.yml/badge.svg)](https://github.com/acelot/automapper/actions)[![coverage](https://raw.githubusercontent.com/acelot/automapper/master/badge-coverage.svg)](./clover.xml)[![packagist](https://camo.githubusercontent.com/0b3c337ccf8a91c4877c42508b88b070f4ea5a1094de37aeed99cdd8e4f5a5be/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6163656c6f742f6175746f6d61707065722e7376673f7374796c653d666c6174)](https://packagist.org/packages/acelot/automapper)[![dependencies](https://camo.githubusercontent.com/288eb68a2e63d5fff3f013b2b6a24d46435dae2ea57689deee0cda2e3230641d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646570656e64656e636965732d7a65726f2d626c75652e7376673f7374796c653d666c6174)](https://camo.githubusercontent.com/288eb68a2e63d5fff3f013b2b6a24d46435dae2ea57689deee0cda2e3230641d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646570656e64656e636965732d7a65726f2d626c75652e7376673f7374796c653d666c6174)[![MIT](https://camo.githubusercontent.com/7850900b3b782ad3f20929d721f29d3e07b08799877c37c5c2862d18ae60841c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6163656c6f742f6175746f6d61707065722e7376673f7374796c653d666c6174)](https://camo.githubusercontent.com/7850900b3b782ad3f20929d721f29d3e07b08799877c37c5c2862d18ae60841c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6163656c6f742f6175746f6d61707065722e7376673f7374796c653d666c6174)

**AutoMapper** is a powerful declarative data mapper for PHP 8. AutoMapper can map data from any source data (usually array/object) to the existing array/object or marshal a new ones.

💿 Install
---------

[](#-install)

```
composer require acelot/automapper:^2.0
```

🗿 Usage
-------

[](#-usage)

### How to marshal new array from the another?

[](#how-to-marshal-new-array-from-the-another)

```
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::marshalNestedArray(
            a::toKey(0, a::get('[name][firstname]')),
            a::toKey(1, a::get('[name][lastname]')),
        ),
        a::joinArray(' ')
    )),
    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',
  ],
)
```

### How to map data from source to the existing array?

[](#how-to-map-data-from-source-to-the-existing-array)

Show the code```
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',
)
```

📌 Examples
----------

[](#-examples)

All examples can be found in [`tests/Functional`](tests/Functional) directory.

🗄️ Reference
------------

[](#️-reference)

No need to use concrete classes, it's better to use the AutoMapper API [static functions](src/AutoMapper.php). It is very convenient to import the AutoMapper as a short alias, for example `use Acelot\AutoMapper\AutoMapper as a`.

### Main functions

[](#main-functions)

The main functions of AutoMapper.

FunctionDescription[`map`](tests/Functional/mapTest.php)Maps data from the source to the target. Target is passing by reference.[`marshalArray`](tests/Functional/marshalArrayTest.php)Maps data from the source to the keys of the new array.[`marshalObject`](tests/Functional/marshalObjectTest.php)Maps data from the source to the properties of the new `stdClass`.### Field definitions

[](#field-definitions)

Definitions that helps you to shape the target structure.

FunctionDescription[`toKey`](tests/Functional/marshalArrayTest.php)Sets/creates the value by key with given name in the target array.[`toProp`](tests/Functional/marshalObjectTest.php)Sets/creates the value by property with given name in the target object.[`toMethod`](tests/Functional/toMethodTest.php)Calls a method with given name with value as an argument in the target object.[`toSelf`](tests/Functional/toSelfTest.php)Assigns a value to the target.### Processors

[](#processors)

Core value processors. The purpose of processors is to retrieve the value or mutate the incoming value and pass it to the next one.

FunctionDescription[`assertType`](tests/Functional/assertTypeTest.php)Asserts the type of value. Throws `UnexpectedValueException` if assert is failed.[`call`](tests/Functional/callTest.php)Process the value by user defined function.[`callCtx`](tests/Functional/callCtxTest.php)Same as `call` but [context](#what-is-context) will be passed as a first argument.[`condition`](tests/Functional/conditionTest.php)Condition processor. If the user-defined function returns `true`, then the value will be passed to the first processor, otherwise to the second.[`conditionCtx`](tests/Functional/conditionCtxTest.php)Same as `condition` but [context](#what-is-context) will be passed to the user-defined function.[`find`](tests/Functional/findTest.php)Finds the element in iterable by the given predicate function.[`findCtx`](tests/Functional/findCtxTest.php)Same as `find` but [context](#what-is-context) will be passed to the predicate function.[`get`](tests/Functional/getTest.php)Most useful processor. Fetches the value from the source by given [path](#how-to-use-get-processor).[`getFromCtx`](tests/Functional/getFromCtxTest.php)Fetches the value from the [context](#what-is-context).[`ignore`](tests/Functional/ignoreTest.php)Always returns the `IgnoreValue`. This value will be ignored by field definition, `mapArray` and `mapIterator`[`mapIterable`](tests/Functional/mapIterableTest.php)Iterates over elements of an iterable and applies the given sub-processor. ℹ️ Produces values by `yield` operator, so in order to retrieve them you should to iterate the result or call `toArray` helper.[`marshalNestedArray`](tests/Functional/marshalNestedArrayTest.php)The same function as `mapArray` only in the form of a processor. Accepts the value from the previous processor as a source.[`marshalNestedObject`](tests/Functional/marshalNestedObjectTest.php)Same as `marshalNestedArray` only produces object.[`notFound`](tests/Functional/notFoundTest.php)Always returns the `NotFoundValue`.[`pass`](tests/Functional/passTest.php)This processor do nothing and just returns the value untouched.[`pipe`](tests/Functional/marshalNestedArrayTest.php)Conveyor processor. Accepts multiple sub-processors and pass the value to the first sub-processor, then pass the result of the first to the second, then to the third and so on.[`value`](tests/Functional/conditionTest.php)Just returns the given value.### Helpers

[](#helpers)

Helpers are the processors that built on top of the another processors. Some helpers are just a shorthands to the core processors with specific arguments, some of them are combination of the multiple processors.

FunctionDescription[`joinArray`](tests/Functional/joinArrayTest.php)Joins the incoming array using the given separator. Throws `UnexpectedValueException` if incoming value is not an array.[`sortArray`](tests/Functional/sortArrayTest.php)Sorts the incoming array using built-in `sort` function. Throws `UnexpectedValueException` if incoming value is not an array.[`uniqueArray`](tests/Functional/uniqueArrayTest.php)Returns only unique elements of the incoming array. Throws `UnexpectedValueException` if incoming value is not an array.[`ifNotFound`](tests/Functional/notFoundTest.php)Checks if the incoming value is `NotFoundValue` and passes the value to other processors depending on the result.[`ifEmpty`](tests/Functional/ifEmptyTest.php)Same as `ifNotFound` but checks if the value is `empty`.[`ifNull`](tests/Functional/ifNullTest.php)Same as `ifNotFound` but checks if the value `is_null`.[`IfEqual`](tests/Functional/ifEqualTest.php)Checks if the incoming value is equal to the given value.[`ifNotEqual`](tests/Functional/ifEqualTest.php)Checks if the incoming value is not equal to the given value.[`explodeString`](tests/Functional/explodeStringTest.php)Splits the incoming string into parts using built-in `explode` function. Throws `UnexpectedValueException` if incoming value is not a string.[`trimString`](tests/Functional/trimStringTest.php)Trims the incoming string using built-in `trim` function. Throws `UnexpectedValueException` if incoming value is not a string.[`toBool`](tests/Functional/toBoolTest.php)Converts the incoming value to boolean type.[`toFloat`](tests/Functional/toFloatTest.php)Converts the incoming value to float type. Throws `UnexpectedValueException` if incoming value is not a scalar.[`toInt`](tests/Functional/toIntTest.php)Converts the incoming value to integer type. Throws `UnexpectedValueException` if incoming value is not a scalar.[`toString`](tests/Functional/toStringTest.php)Converts the incoming value to string type. Throws `UnexpectedValueException` if incoming value is not a scalar or an object that not implements `__toString`.[`toArray`](tests/Functional/toArrayTest.php)Converts the incoming value to array. Usually used with `mapIterable` processor.🧩 Integrations
--------------

[](#-integrations)

NameDescription[`RespectValidation`](integrations/respect-validation)Provides validation processor via [`respect/validation`](https://github.com/Respect/Validation) library.🤨 FAQ
-----

[](#-faq)

### What is Context?

[](#what-is-context)

The `Context` is a special DTO class for storing any kind of data: configs, DB connections, fixtures, etc. This DTO is passed to the mapper, and you can use your data inside the processors. Processors capable of working with the context end with `Ctx` suffix, [`callCtx`](tests/Functional/callCtxTest.php) for example.

### How to use `get` processor?

[](#how-to-use-get-processor)

You can obtain any key/prop/method from the source using the [`get`](tests/Functional/getTest.php) processor which accepts a special path string. The processor parses the given path and divides it into parts, then pulls out the data following the parts of the path.

Available path parts:

PartDescription`@`"Self Pointer" – returns the source itself`[0]`Returns an array value by index`[key]`Returns an array value by key`[some key]`Returns an array value by key with spaces`[#first]`Returns an array first value`[#last]`Returns an array last value`->property`Returns an object value by property`->{some property}`Returns an object value by property name with spaces`->someMethod()`Calls an object method and returns the valueYou can combine the parts to obtain the deep values:

```
[array_key][array key with spaces][#first][#last]->property->{property with spaces}->someMethod()

```

If any part of the path is not found, then the processor will return `NotFoundValue` value. This value throws an `NotFoundException` but you can recover it using [`ifNotFound`](tests/Functional/notFoundTest.php) helper.

🖋️ License
----------

[](#️-license)

Licensed under [MIT](LICENSE). Copyright (c) 2017-present, Valeriy Protopopov

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance33

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 61.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~171 days

Recently: every ~118 days

Total

12

Last Release

1205d ago

Major Versions

1.x-dev → 2.0.0-rc.12023-01-27

PHP version history (3 changes)1.0.0PHP ^7.1

1.2.2PHP ^7.1 || ^8.0

2.0.0-rc.1PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/3e8e4631dcd1e5485b4fe6a80f5c6feea95c388ca6b522c3cc69cb4eee8cc284?d=identicon)[acelot](/maintainers/acelot)

---

Top Contributors

[![acelot](https://avatars.githubusercontent.com/u/1065215?v=4)](https://github.com/acelot "acelot (43 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (27 commits)")

---

Tags

automapperdatamappermapperphp8mapperdata mappermarshallerautomapper

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/acelot-automapper/health.svg)

```
[![Health](https://phpackages.com/badges/acelot-automapper/health.svg)](https://phpackages.com/packages/acelot-automapper)
```

###  Alternatives

[cuyz/valinor

Dependency free PHP library that helps to map any input into a strongly-typed structure.

1.5k9.2M108](/packages/cuyz-valinor)[mark-gerarts/auto-mapper-plus

An AutoMapper for PHP

5623.2M21](/packages/mark-gerarts-auto-mapper-plus)[eventsauce/object-hydrator

Converts structured data into strict objects.

3322.3M20](/packages/eventsauce-object-hydrator)[symfonycasts/micro-mapper

A tiny, underwhelming data mapper to map one object to another!

82597.6k](/packages/symfonycasts-micro-mapper)[nutgram/hydrator

Hydrator for PHP 8.0+

12265.2k6](/packages/nutgram-hydrator)[selective/transformer

A strictly typed array transformer with dot-access, fluent interface and filters.

3817.8k1](/packages/selective-transformer)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
