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

ActiveLibrary

onmoon/dto-mapper
=================

Class to map any array or class tree into DTO

1.3.1(3y ago)0188MITPHPPHP ^7.4|^8.0CI failing

Since May 1Pushed 3y ago1 watchersCompare

[ Source](https://github.com/onmoon/dto-mapper)[ Packagist](https://packagist.org/packages/onmoon/dto-mapper)[ RSS](/packages/onmoon-dto-mapper/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (5)Dependencies (9)Versions (8)Used By (0)

DTO Mapper
==========

[](#dto-mapper)

About
-----

[](#about)

This class helps to map any class or associative array tree into DTO class tree.

It uses reflection to create DTOs but calls getters or reads properties to get values from source tree and only properties found in DTO will be fetched from source object. This makes mapper work fine e.g. with doctrine lazy loads.

For object `object` if key property `property` is required, following sources will be checked:

- `object->propery`
- `object[property]`
- `object->getPropery()`
- `object->isPropery()`
- `object->hasPropery()`
- `object->propery()`
- `object->__call('property')`

All method calls are case-insensitive.

DTOs must have all their properties typed and all array properties should have phpDoc specifying item types.

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Run

```
composer require onmoon/dto-mapper

```

Usage
-----

[](#usage)

```
public function showPetById(int $petId) : ShowPetByIdResponseDto
{
    $pet   = $this->pets->getById($petId);

    $mapper = new OnMoon\DtoMapper\DtoMapper();
    return $mapper->map($pet, ShowPetByIdResponseDto::class);
}
```

DateTime handling
-----------------

[](#datetime-handling)

`DateTime` class does not support creation with reflection. If DTO property is subclass of `DateTimeInterface` then this property is set to new `\Safe\DateTime`instance. Source values for `DateTime` can be both `DateTimeInterface` and `string`.

Rewriting source properties
---------------------------

[](#rewriting-source-properties)

By default, mapper searches for the same property in source as in DTO. If you want to read value for some property from another source property, you can pass a callable object as third argument. This callable should always return mapped property name.

```
$mapper->map($pet, ShowPetByIdResponseDto::class, function ($propertyName, $context) {
    $fullName = implode('->', [...$context, $propertyName]);
    if ($fullName === 'subObject->s2->property1') {
       return 'realProperty1';
    }
    return $propertyName;
});
```

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity66

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 ~249 days

Total

5

Last Release

1207d ago

PHP version history (2 changes)1.0.0PHP ^7.4

1.2.0PHP ^7.4|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/0685db39a6beb2ba171bdf1eb4963a82bf8bb2400e1624f96e40910d7a970af3?d=identicon)[DimanKuskov](/maintainers/DimanKuskov)

---

Top Contributors

[![DimanKuskov](https://avatars.githubusercontent.com/u/10142095?v=4)](https://github.com/DimanKuskov "DimanKuskov (3 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[nelmio/api-doc-bundle

Generates documentation for your REST API from attributes

2.3k63.6M233](/packages/nelmio-api-doc-bundle)[craftcms/cms

Craft CMS

3.6k3.6M2.6k](/packages/craftcms-cms)[symfony/serializer-pack

A pack for the Symfony serializer

1.1k28.2M221](/packages/symfony-serializer-pack)[humbug/box

Fast, zero config application bundler with PHARs.

1.3k801.5k69](/packages/humbug-box)[web-auth/webauthn-lib

FIDO2/Webauthn Support For PHP

1195.3M72](/packages/web-auth-webauthn-lib)[a2lix/auto-form-bundle

Automate form building

873.8M11](/packages/a2lix-auto-form-bundle)

PHPackages © 2026

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