PHPackages                             voilab/mapping - 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. voilab/mapping

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

voilab/mapping
==============

Mapping system to promote a constant output from a set of datas

1.2.5(9y ago)1122MITPHPPHP &gt;=5.5.0

Since Feb 12Pushed 9y ago3 watchersCompare

[ Source](https://github.com/voilab/mapping)[ Packagist](https://packagist.org/packages/voilab/mapping)[ Docs](http://www.voilab.ch)[ RSS](/packages/voilab-mapping/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (18)Used By (0)

Voilab Mapping system
=====================

[](#voilab-mapping-system)

Install
-------

[](#install)

Via Composer

Create a composer.json file in your project root:

```
{
    "require": {
        "voilab/mapping": "1.*"
    }
}
```

```
$ composer require voilab/mapping
```

Sample dataset
--------------

[](#sample-dataset)

```
// have a mapping instance
$mapping = new \voilab\mapping\Mapping();

// have a set of datas. This could also be an object with
// methods like getName(), getInterests(), etc.
$data = [
    'id' => 1,
    'name' => 'John',
    'email' => 'john@doe.ch',
    'interests' => [
        [
            'type' => 'intellect',
            'description' => 'Some book',
            'contact' => [
                'name' => 'Some author'
            ]
        ],
        [
            'type' => 'sport',
            'description' => 'Football'
        ]
    ],
    'bestFriend' => [
        'id' => 2,
        'name' => 'Fred'
        'age' => 30
    ],
    'work' => [
        'description' => 'Free worker',
        'section' => [
            'type' => 'Social'
        ]
    ]
];
```

Examples
--------

[](#examples)

This piece of script helps you to obtain a constant output structure no matter how the input data is hydrated (array or object).

### Simple mapping

[](#simple-mapping)

```
$mapped = $mapping->map($data, [
    'id',
    'name'
]);

// results in
[
    'id' => 1,
    'name' => 'John'
]
```

### Function call mapping

[](#function-call-mapping)

```
$mapped = $mapping->map($data, [
    'id',
    'email' => function ($data) {
        return $data['email'] == 'john@doe.ch'
            ? 'some.default@email.ch'
            : $data['email'];
    }
]);

// results in
[
    'id' => 1,
    'email' => 'some.default@email.ch'
]
```

### One-to-one or many-to-one relation mapping

[](#one-to-one-or-many-to-one-relation-mapping)

```
$mapped = $mapping->map($data, [
    'work' => [
        'section' => [
            'type'
        ]
    ]
]);

// results in
[
    'work' => [
        'section' => [
            'type' => 'Social'
        ]
    ]
]
```

### One-to-many or many-to-many relation mapping

[](#one-to-many-or-many-to-many-relation-mapping)

```
$mapped = $mapping->map($data, [
    'interests' => [[
        'description'
    ]]
]);

// results in
[
    'interests' => [
        ['description' => 'Some book'],
        ['description' => 'Football']
    ]
]
```

### Complex relation mapping

[](#complex-relation-mapping)

```
$mapped = $mapping->map($data, [
    'interests' => [[
        'contact' => [
            'name'
        ]
    ]]
]);

// results in
[
    'interests' => [
        ['contact' => [
            'name' => 'Some author'
        ]],
        ['contact' => null]
    ]
]
```

### Change mapping key

[](#change-mapping-key)

```
$mapped = $mapping->map($data, [
    'id',
    'personName' => 'name'
]);

// results in
[
    'id' => 1,
    'personName' => 'John'
]
```

### Change relation mapping key

[](#change-relation-mapping-key)

```
$mapped = $mapping->map($data, [
    \voilab\mapping\Mapping::rel('work', 'nextWork') => [
        'description'
    ]
]);

// results in
[
    'nextWork' => [
        'description' => 'Free worker'
    ]
]
```

As an alternative, you may use this notation below. Code should not change anytime soon.

```
$mapped = $mapping->map($data, [
    'work as nextWork' => [
        'description'
    ]
]);
```

### Wildcard mapping

[](#wildcard-mapping)

It's experimental with objects.

```
$mapped = $mapping->map($data, [
    'name',
    'bestFriend' => [
        '*'
    ]
]);

// results in
[
    'name' => 'John',
    'bestFriend' => [
        'id' => 2,
        'name' => 'Fred'
        'age' => 30
    ]
]
```

### Advanced function mapping usage

[](#advanced-function-mapping-usage)

You may want to access the current index inside your function if you are inside a collection. It's as easy as:

```
$special_for_interest = [
    ['data' => 1],
    ['data' => 2]
];

$mapped = $mapping->map($data, [
    'interests' => [[
        'description',
        'special' => function ($interest, $index) use ($special_for_interest) {
            return $special_for_interest[$index]['data'];
        },
        'contact' => [
            'name' => function ($contact, $index, $indexes, $parents) {
                // if you want to access the parent interest. The first parent
                // is the top parent: 0 => main data, 1 => interest
                return $contact['name'] . ' for ' . $parents[1]['description'];
            }
        ]
    ]]
]);

// results in
[
    'interests' => [
        [
            'description' => 'Some book',
            'special' => 1,
            'contact' => [
                'name' => 'Some author for Some book'
            ]
        ],
        [
            'description' => 'Football',
            'special' => 2,
            'contact' => null
        ]
    ]
]
```

Hydrators
---------

[](#hydrators)

Hydrators are objects that extract data from the initial source. Two defaults, packaged in this lib, are for array management and simple objects (with getters and setters camelcased).

### Change hydrators

[](#change-hydrators)

If your data needs to be handled differently, you will need to create your own hydrators (which must extend \\voilab\\mapping\\Hydrator) and then set them at construction time:

```
$mapping = new \voilab\mapping\Mapping(
    new \my\object\Hydrator(),
    new \my\array\Hydrator()
);
```

Please check packaged hydrators sources to see how it works. It's quite simple.

Plugins
-------

[](#plugins)

### Introduction

[](#introduction)

#### Configuration

[](#configuration)

The plugin "Deep one-to-one or many-to-one relation mapping" is always active. If you want to add other plugins, just do this when initializing the mapping object:

```
$mapping->addPlugin(new \voilab\mapping\plugin\FirstInCollection());
```

#### Disable plugin management

[](#disable-plugin-management)

If you don't want to call any plugin (even the default one), simply set the plugin key separator to null.

```
$mapping->setPluginKeySeparator(null);
```

### Deep one-to-one or many-to-one relation mapping

[](#deep-one-to-one-or-many-to-one-relation-mapping)

Goes through a tree of one-to-one or many-to-one relations, until it reaches the key (here: type for section and name for interests).

```
$mapped = $mapping->map($data, [
    'sectionType' => 'work.section.type',
    'interests' => [[
        'contactName' => 'contact.name'
    ]]
]);

// results in
[
    'sectionType' => 'Social',
    'interests' => [
        ['contactName' => 'Some author']
        ['contactName' => null]
    ]
]
```

### Deep first one-to-many or many-to-many relation mapping

[](#deep-first-one-to-many-or-many-to-many-relation-mapping)

When encountering a one-to-many or many-to-many relation, it fetch the 1st element in the collection, and then tries to fetch the other relations, before accessing the key (here: name).

```
$mapping->addPlugin(new \voilab\mapping\plugin\FirstInCollection());

$mapped = $mapping->map($data, [
    'firstInterestContactName' => 'interests[].contact.name'
]);

// results in
[
    'firstInterestContactName' => 'Some author'
]
```

Testing
-------

[](#testing)

```
$ phpunit
```

Security
--------

[](#security)

If you discover any security related issues, please use the issue tracker.

Credits
-------

[](#credits)

- [tafel](https://github.com/tafel)
- [voilab](https://github.com/voilab)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~48 days

Recently: every ~132 days

Total

15

Last Release

3444d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5629730?v=4)[tafel](/maintainers/tafel)[@tafel](https://github.com/tafel)

---

Top Contributors

[![tafel](https://avatars.githubusercontent.com/u/5629730?v=4)](https://github.com/tafel "tafel (55 commits)")

---

Tags

mappingvoilab

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/voilab-mapping/health.svg)

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

###  Alternatives

[cuyz/valinor

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

1.5k9.2M108](/packages/cuyz-valinor)[cornford/googlmapper

An easy way to integrate Google Maps with Laravel.

457447.9k4](/packages/cornford-googlmapper)[ballen/distical

A distance (Lat/Long) calculation library for PHP

40180.2k2](/packages/ballen-distical)

PHPackages © 2026

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