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

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

fivepercent/object-mapper
=========================

Map array data to objects

v1.0(10y ago)02741MITPHPPHP &gt;=5.4

Since Jun 4Pushed 10y ago6 watchersCompare

[ Source](https://github.com/InnoGr/FivePercent-ObjectMapper)[ Packagist](https://packagist.org/packages/fivepercent/object-mapper)[ RSS](/packages/fivepercent-object-mapper/feed)WikiDiscussions master Synced 1mo ago

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

Object Mapper
=============

[](#object-mapper)

With this package, you can map `array` data to `object` instances.

Installation
------------

[](#installation)

Add **FivePercent/ObjectMapper** in your composer.json:

```
{
    "require": {
        "fivepercent/object-mapper": "~1.0"
    }
}
```

Now tell composer to download the library by running the command:

```
$ php composer.phar update fivepercent/object-mapper
```

Basic usage
-----------

[](#basic-usage)

Before use **ObjectMapper** you must configure instance:

1. Create a metadata factory for loads metadata from objects
2. Create a strategy manager

```
use Doctrine\Common\Annotations\AnnotationReader;
use FivePercent\Component\ObjectMapper\Strategy\StrategyManager;
use FivePercent\Component\ObjectMapper\Metadata\MetadataFactory;
use FivePercent\Component\ObjectMapper\Metadata\Loader\AnnotationLoader;
use FivePercent\Component\ObjectMapper\ObjectMapper;
use FivePercent\Component\ObjectMapper\Metadata\ObjectMetadata;

$strategyManager = StrategyManager::createDefault();

$annotationLoader = new AnnotationLoader(new AnnotationReader());
$metadataFactory = new MetadataFactory($annotationLoader);

$objectMapper = new ObjectMapper($metadataFactory, $strategyManager);

// Or create default object mapper
$objectMapper = ObjectMapper::createDefault(); // Used AnnotationLoader for load metadata
```

**Attention:** now supported only annotation metadata loader.

After configure and create instance, you can use `ObjectMapper` map function.

Example object for maps:

```
use FivePercent\Component\ObjectMapper\Annotation\Object;
use FivePercent\Component\ObjectMapper\Annotation\Property;

/**
 * @Object()
 */
class MyClass
{
    /**
     * @Property()
     */
    public $id;

    /**
     * @Property()
     */
    public $name;
}
```

And map array data:

```
$object = new MyClass();
$objectMapper->map($object, [
    'id' => 1,
    'name' => 'Foo Bar'
]);
```

If you want map all properties in object, you can set attribute **allProperties** for **@Object**, then this indicate for load all properties from class.

```
use FivePercent\Component\ObjectMapper\Annotation\Object;

/**
 * @Object(allProperties=true)
 */
class MyClass
{
    public $id;
    public $name;
}
```

If key of array not equals to property name of object, you can set attribute **fieldName** for **@Property**

```
use FivePercent\Component\ObjectMapper\Annotation\Object;
use FivePercent\Component\ObjectMapper\Annotation\Property;

/**
 * @Object()
 */
class MyClass
{
    /**
     * @Property()
     */
    public $id;

    /**
     * @Property(fieldName="first_name")
     */
    public $firstName;
}

$object = new MyClass();
$objectMapper->map($object, [
    'id' => 1,
    'first_name' => 'Foo Bar'
]);
```

### Recursive mapping

[](#recursive-mapping)

You can recursive map data to object.

With simple object:

```
use FivePercent\Component\ObjectMapper\Annotation\Object;
use FivePercent\Component\ObjectMapper\Annotation\Property;

class MyClass
{
    /**
     * @DataMapping\Property(class="Tag")
     */
    protected $tag;
}

/**
 * @DataMapping\Object(allProperties=true)
 */
class Tag
{
    protected $name;
}

$object = new MyClass();
$objectMapper->map($object, [
    'tag' => [
        'name' => 'Foo Bar'
    ]
]);
```

With collection:

```
use FivePercent\Component\ObjectMapper\Annotation\Object;
use FivePercent\Component\ObjectMapper\Annotation\Property;

class MyClass
{
    /**
     * @DataMapping\Property(collection=true, class="Tag")
     */
    protected $tag;
}

/**
 * @DataMapping\Object(allProperties=true)
 */
class Tag
{
    protected $name;
}

$object = new MyClass();
$objectMapper->map($object, [
    'tag' => [
        ['name' => 'Foo Bar'],
        ['name' => 'Bar Foo']
    ]
]);
```

And you can set the collection class if necessary, to attribute **collection** [``](#id1)collection="MyCollectionClass"` All keys as default will be saved.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity58

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

Unknown

Total

1

Last Release

4002d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/16cc6c4d32a2c7f1cd584f31db9428341ad57682521f0f6e607cfe19f1f9157d?d=identicon)[inno-group](/maintainers/inno-group)

---

Top Contributors

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

---

Tags

objectmapperobject mapper

### Embed Badge

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

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

###  Alternatives

[myclabs/deep-copy

Create deep copies (clones) of your objects

8.9k849.8M169](/packages/myclabs-deep-copy)[symfony/property-access

Provides functions to read and write from/to an object or array using a simple string notation

2.8k295.3M2.5k](/packages/symfony-property-access)[cuyz/valinor

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

1.5k9.2M108](/packages/cuyz-valinor)[eventsauce/object-hydrator

Converts structured data into strict objects.

3322.3M20](/packages/eventsauce-object-hydrator)

PHPackages © 2026

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