PHPackages                             htfy96/magic-transformer.php - 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. htfy96/magic-transformer.php

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

htfy96/magic-transformer.php
============================

Bidirectional array-like struct mapper in PHP

26PHP

Since Jul 13Pushed 9y ago1 watchersCompare

[ Source](https://github.com/htfy96/MagicTransformer.php)[ Packagist](https://packagist.org/packages/htfy96/magic-transformer.php)[ RSS](/packages/htfy96-magic-transformerphp/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (2)Used By (0)

MagicTransformer.php
====================

[](#magictransformerphp)

Bidirectional array-like struct mapper in PHP.

[中文](https://github.com/htfy96/MagicTransformer.php/tree/cn)

Initiative
----------

[](#initiative)

Model transformation is common in PHP project. Traditional way is to write `A::transformToB` and `A::setFromB` method. However in most cases the logic behind these two methods is almost the same, except that one constructs object and the other extract information from object.

Then comes this library: define transformation once, use it bidirectionally.

Usage
-----

[](#usage)

```
require_once 'magic_transformer.php';
use MagicTransform\M as M;
```

A mapper is an object that implments `MagicTransform\iBidirectionTrans`:

```
interface iBidirectionTrans {
    public function forward_map($left_val);
    public function reverse_map($right_val, &$left_obj);
}
```

```
//Define transformation
$trans = M::make_mapper(
            [
                'abc' => M::$self[0], // the value of 'abc' is $left[0]
                'ccc' => M::make_chain(
                    M::$self[0], // The 2nd argument(which is a mapper) will receive $left[1] as $left
                    [
                        'yyy' => M::$self // Use self to reference $left
                    ]
                ),
                'ddd' => M::self['eee']['fff'], // to reference $left['eee']['fff']
                'eee' => M::make_chain(
                    M::$self[2], // now $left(in make_list_mapper) is $left[2]
                    M::make_list_mapper( // Apply the mapper to each item of list
                        M::make_func_mapper( // customize mapper!
                            function($left_val) { // forward
                                return $left_val + 1;
                            },
                            function ($right_val, &$left_obj) {
                                $left_obj = $right_val - 1;
                            }
                        )
                    )
                )
            ]
        );

$left = ['0th', '1st', [1,2,3], 'eee' => ['fff' => 4]];

$right = $trans->forward_map($left);
print_r($right);

/*
 * Array
(
    [abc] => 0th
    [ccc] => Array
        (
            [yyy] => 1st
        )

    [ddd] => 4
    [eee] => Array
        (
            [0] => 2
            [1] => 3
            [2] => 4
        )
 */

// Now let's modify $right

$right['abc'] = '0th0th';
$right['ccc']['yyy'] = ['1st1st'];
$right['ddd'] = 100;
$right['eee'][1] = 7;

$left = array(); // You can use your ORM here!
$trans->reverse_map($right, $left);

print_r($left);
/*
Array
(
    [0] => 0th0th
    [1] => Array
        (
            [0] => 1st1st
        )

    [eee] => Array
        (
            [fff] => 100
        )

    [2] => Array
        (
            [0] => 1
            [1] => 6
            [2] => 3
        )

)
*/
```

And that's all!

API
---

[](#api)

All following API is in `MagicTransform` namespace.

### Interface

[](#interface)

#### iBidirectionTrans

[](#ibidirectiontrans)

```
interface iBidirectionTrans {
    public function forward_map($left_val);
    public function reverse_map($right_val, &$left_obj);
}
```

All mapper shall implement this interface.

### Mapper/mapper maker

[](#mappermapper-maker)

All mapper maker is defined in class `M`:

#### make\_key\_mapper

[](#make_key_mapper)

maps `$left[$key1][$key2]...`

```
M::make_key_mapper('abc', 'def'); //$left['abc']['def']
```

#### \_\_0/\_\_1/\_\_2/\_\_3

[](#__0__1__2__3)

alias of make\_key\_mapper(0), ..., make\_key\_mapper(3)

#### self

[](#self)

A mapper which always returns itself and never modify left value when called.

Magic function \[\] overloaded as sugar of `make_key_mapper`

#### make\_chain

[](#make_chain)

Map in order Arg1, Arg2, ..., ArgN

```
M::make_chain(M::$__0, M::$__1); // $left[0][1], which is equivalent to make_key_mapper(0, 1)
```

#### make\_list\_mapper

[](#make_list_mapper)

accept a mapper and apply it to each item of $left

```
M::make_list_mapper(M::$__0); //[$left[0][0], $left[1][0], ...]
```

#### make\_\_func\_mapper

[](#make__func_mapper)

Customize your own mapper. The first argument is forward mapper and the second is reverse mapper.

```
M::make_func_mapper( // customize mapper!
                    function($left_val) { // forward
                        return $left_val + 1;
                    },
                    function ($right_val, &$left_obj) {
                        $left_obj = $right_val - 1;
                    }
                ); // $left + 1
```

#### make\_mapper

[](#make_mapper)

Transform a simple array to mapper automaticly.

Transformation should be defined with this method.

```
make_mapper(
            [
                'aaa' => M::$__0
            ]
        );
```

License
-------

[](#license)

Apache License.

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8121231?v=4)[Zheng Luo](/maintainers/htfy96)[@htfy96](https://github.com/htfy96)

---

Top Contributors

[![htfy96](https://avatars.githubusercontent.com/u/8121231?v=4)](https://github.com/htfy96 "htfy96 (7 commits)")

### Embed Badge

![Health badge](/badges/htfy96-magic-transformerphp/health.svg)

```
[![Health](https://phpackages.com/badges/htfy96-magic-transformerphp/health.svg)](https://phpackages.com/packages/htfy96-magic-transformerphp)
```

###  Alternatives

[contao-bootstrap/bundle

Install all modules of the Contao Bootstrap extension

1547.4k2](/packages/contao-bootstrap-bundle)

PHPackages © 2026

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