PHPackages                             kanel/mapper - 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. kanel/mapper

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

kanel/mapper
============

Simply map JSON structures onto PHP classes Edit

1.1(9y ago)11.8k1PHPPHP &gt;=7.0

Since May 16Pushed 9y agoCompare

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

READMEChangelog (2)Dependencies (1)Versions (3)Used By (1)

mapper
======

[](#mapper)

Simply map JSON structures onto PHP classes

[![build](https://camo.githubusercontent.com/019c878664c037f7d1d9442b0b3c48b764e0a8aff4e00b6666ac83b7d824e571/68747470733a2f2f7472617669732d63692e6f72672f656c6b6161646b612f6d61707065722e7376673f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/019c878664c037f7d1d9442b0b3c48b764e0a8aff4e00b6666ac83b7d824e571/68747470733a2f2f7472617669732d63692e6f72672f656c6b6161646b612f6d61707065722e7376673f6272616e63683d6d6173746572)

\#How it works

- Map an array, an object or a json string into an object :

    ```
        $data = ['testOne' => 1, 'testTwo' => 2, 'testThree' => 3]
        $myClass = new MyClass();
        $mapper = new Mapper();
        $mapper->map($data, $myClass);
        print_r($myClass);

        Where MyClass is:

        class MyClass {
           protected $testOne;
           public $testTwo;
           private $testThree;
        }

        The result is :

        Object
        (
            [testOne] => 1
            [testTwo] => 2
            [testThree] => 3
        )

    ```

    - the first parameter ($from) can be either an array, an object or a string
    - if the first parameter is a string but isn't a valid json, an InvalidDataException is raised
    - if the second parameter isn't an object, an InvalidDataException is raised
    - if a property (when $from is an object) or key (when $from is an array) does not exist, it will not be created.
- To create a property in the target class when it does not exist:

    ```
    $options = new Options();
    $options->createNewProperty(true);
    $mapper = new Mapper($options);
    $mapper->map($from, $myClass);

    ```
- Transform properties:

    - You can transform array keys or object properties during mapping,
    - example :

        - Map the array :

            ```
            ['test_one' => 1]

            ```

            to the object :

            ```
                class Test {
                   protected $testOne;
                }

            ```

            where the array key 'test\_one' corresponds to the property $testOne;
        - Or Map the object :

            ```
                class A {
                   protected $testA;
                }

            ```

            to the object :

            ```
                class B {
                   protected $testB;
                }

            ```

            where the property $testA of the class A corresponds to the property $testB of class B

    To do so you need to use Transformers :

    There are 4 built in Transformers :

    - CamelCaseTransformer : snake or pascal case to camel case

        ```
        attribute-name => attributeName
        attribute_name => attributeName
        attribute name => attributeName
        Attribute_NAME => attributeName
        AttributeName  => attributeName
        attributeName  => attributeName
        attribute_firstName => attributeFirstname

        ```
    - SnakeCaseTransformer: pascal or camel case to snake case

        ```
        attributeName => attribute_name
        attribute => attribute

        ```
    - PascalCaseTransformer: snake or camel case to pascal case

        ```
        attribute-name => AttributeName
        attribute_name => AttributeName
        attribute name => AttributeName
        Attribute_NAME => AttributeName
        AttributeName  => AttributeName
        attributeName  => AttributeName
        attribute_firstName => AttributeFirstname

        ```
    - **CustomTransformer : allows to add your own transformer**

    You can specify a transformer for all the properties/keys or one for every property/key

    Examples :

    - Apply a CamelCase Transformer to all the properties/keys

        ```
        $options = new Options();
        $options->setTransformer(new CamelCaseTransformer());
        $mapper = new Mapper($options);

        $class = new MyClassExample(); //Contains only one propery named $testOne;
        $data = ['test_one' => 'Hellow World'];

        $mapper->map($data, $class);

        Result :

        print_r($class);

        Object
        (
            [testOne] => 'Hellow World'
        )

        ```
    - Apply a Custom Transformer to all the properties/keys

        ```
        $customTransformer = new CustomTransformer();
        $customTransformer->setTransformer(function($property) {
            if ($property == 'test_one') {
                return 'name';
            }

            return $property; //very important or else only test_one will be mapped
        });
        $options = new Options();
        $options->setTransformer($customTransformer);
        $mapper = new Mapper($options);

        $class =  new MyClassExample(); //Having two Properties $name and $phone
        $data = ['test_one' => 'Jhon doe', 'phone' => 1234];
        $mapper->map($data, $class);

        Result :

        print_r($class);

        Object
        (
            [name] => 'Jhon doe',
            [phone] => 1234
        )

        ```
    - Apply different Transformers

        ```
        $customTransformer = new CustomTransformer();
        $customTransformer->setTransformer(function($property) {
           return 'name';
        });
        $options = new Options();
        $options->addPropertyTransformer('test_one', $customTransformer);
        $options->addPropertyTransformer('phone_number', new CamelCaseTransformer());
        $mapper = new Mapper($options);

        $class =  new MyClassExample(); //Having two Properties $name, $phoneNumber
        $data = ['test_one' => 'Jhon doe', 'phone_number' => 1234];
        $mapper->map($data, $class);

        Result :

        print_r($class);

        Object
        (
            [name]  => 'Jhon doe',
            [phoneNumber] => 1234
        )

        ```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity59

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.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

3287d ago

PHP version history (2 changes)1.0PHP &gt;=5.6

1.1PHP &gt;=7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/3215e882ee8ae39a8b904d0958420f68bf23b2cf0e22175d5a61513f98e6ef64?d=identicon)[elkaadka](/maintainers/elkaadka)

---

Top Contributors

[![elkaadka](https://avatars.githubusercontent.com/u/24205608?v=4)](https://github.com/elkaadka "elkaadka (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/kanel-mapper/health.svg)

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

###  Alternatives

[ircmaxell/php-math-parser

A simple shunting-yard based math parser and executor for PHP

2916.7k](/packages/ircmaxell-php-math-parser)[bmcclure/cakephp-media-plugin

CakePHP Media Plugin

591.4k](/packages/bmcclure-cakephp-media-plugin)

PHPackages © 2026

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