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

AbandonedArchivedLibrary

zelak/mapper
============

1.2.0(4y ago)1121[1 PRs](https://github.com/ZelAk312/php-mapper/pulls)PHPPHP &gt;=7.4

Since Jun 13Pushed 3y ago1 watchersCompare

[ Source](https://github.com/ZelAk312/php-mapper)[ Packagist](https://packagist.org/packages/zelak/mapper)[ RSS](/packages/zelak-mapper/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (3)Dependencies (2)Versions (4)Used By (0)

Zelak's Mapper
==============

[](#zelaks-mapper)

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

[](#installation)

```
composer require zelak/mapper

```

Usage
=====

[](#usage)

Creating simple DTOs
--------------------

[](#creating-simple-dtos)

DTOs needs type hinting for the Mapper to work properly

```
class UserDto {
    public int $id;
    public string $username;
    public string $password;
}
```

Creating the mapper, registering and using the Mapper

```
$mapper = new Mapper(); // Creating
$mapper->createMap(UserDto::class); // Registering

// Using
$user = new stdClass();
$user->id = "1";
$user->username = "username";
$user->password = "password":

$userDto = $mapper->map($user, UserDto::class);
```

Result

```
// Before
{
    "id": "1",
    "username": "username",
    "password": "password"
}

// After
{
    "id": 1,
    "username": "username",
    "password": "password"
}
```

---

Creating linked DTOs
--------------------

[](#creating-linked-dtos)

```
class ProductDto {
    public string $name;
    public BuyerDto $buyer;
}

class BuyerDto {
    public string $name;
}
```

Creating the mapper, registering and using the Mapper

```
$mapper = new Mapper(); // Creating
$mapper->createMap(ProductDto::class); // Registering
$mapper->createMap(BuyerDto::class);

// Using
$buyer = new stdClass();
$buyer->name = "buyerName";
$product = new stdClass();
$product->name = "propName";
$product->buyer = $buyer;

$productDto = $mapper->map($product, ProductDto::class);
```

Result

```
// Before
{
    "name": "propName",
    "buyer": {
        "name": "buyerName"
    }
}

// After
{
    "name": "propName",
    "buyer": {
        "name": "buyerName"
    }
}
```

---

Creating linked array DTOs
--------------------------

[](#creating-linked-array-dtos)

```
class ProductArrDto {
    public string $name;
    public array $buyer;
}

class BuyerDto {
    public string $name;
}
```

Creating the mapper and registering the Mapper Since ProductArrDto uses an array we need to specify which class it needs to use
We do this by using the specify function when creating the Map

```
$mapper = new Mapper(); // Creating
$mapper->createMap(ProductArrDto::class) // Registering
    ->specify("buyer", BuyerDto::class); // createMap(BuyerDto::class);
```

Result

```
// Before
{
    "name": "propName",
    "buyer": [
        {
            "name": "buyerName"
        }
    ]
}

// After
{
    "name": "propName",
    "buyer": [
        {
            "name": "buyerName"
        }
    ]
}
```

Docs
====

[](#docs)

Mapper Class
------------

[](#mapper-class)

### Create a map for the a class

[](#create-a-map-for-the-a-class)

```
createMap(string className)
* className -> The name of the class to register the map for

```

### Map object to a registered class

[](#map-object-to-a-registered-class)

```
map(mixed data, string className)
* data      -> The data to map to the class
* className -> The name of the class to map to

```

---

MappedClass Class
-----------------

[](#mappedclass-class)

### Customize the transformation from the Object to the class

[](#customize-the-transformation-from-the-object-to-the-class)

```
from(Closure closure)
* closure -> A closure with first parameter being the From Object and the second parameter being the To Object

```

Example

```
$mapper = new Mapper();
$mapper->createMap(UserDto::class)
    ->from(function ($from, $to) {
        $to->username = $from->username . " $from->id"
    };);
```

Result

```
// Before
{
    "id": "1",
    "username": "username",
    "password": "password"
}

// After
{
    "id": "1",
    "username": "username 1",
    "password": "password"
}
```

### Ignore a property from the object

[](#ignore-a-property-from-the-object)

```
ignore(string propName)
* propName -> The property name to ignore when mapping

```

Example

```
$mapper = new Mapper();
$mapper->createMap(UserDto::class)
    ->ignore("password")
```

Result

```
// Before
{
    "id": "1",
    "username": "username",
    "password": "password"
}

// After
{
    "id": "1",
    "username": "username"
}
```

### Specify a linked array to a DTO

[](#specify-a-linked-array-to-a-dto)

```
specify(string propName, string toClass)
* propName -> The property name to specify the link to
* toClass  -> The name of the class that the array represents

```

### Rename a property

[](#rename-a-property)

```
renameProp(string propName, string renamedPropName)
* propName -> The property name to rename
* renamedPropName  -> The renamed property name

```

```
class UserRenameDto {
    public string $id;
    public string $name;
    public string $password;
}
```

Creating the mapper, registering and using the Mapper

```
$mapper = new Mapper(); // Creating
$mapper->createMap(UserRenameDto::class) // Registering
    ->renameProp("username", "name"); // Rename the property

// Using
$user = new stdClass();
$user->id = "1";
$user->username = "username";
$user->password = "password":

$userDto = $mapper->map($user, UserRenameDto::class);
```

Result

```
// Before
{
    "id": "1",
    "username": "username",
    "password": "password"
}

// After
{
    "id": "1",
    "name": "username",
    "password": "password"
}
```

### Array Assoc to array with specific property

[](#array-assoc-to-array-with-specific-property)

```
toArrayProp(string propName, string propToUse)
* propName -> The property name for the array
* propToUse-> The property to use when making the array

```

Creating the mapper, registering and using the Mapper

```
$mapper = new Mapper(); // Creating
$mapper->createMap(ProductDto::class) // Registering
    ->toArrayProp("buyer", "name"); // Map to array
$mapper->createMap(BuyerDto::class);

// Using
$buyer = new stdClass();
$buyer->name = "buyerName";
$product = new stdClass();
$product->name = "propName";
$product->buyer = $buyer;

$productDto = $mapper->map($user, ProductDto::class);
```

Result

```
// Before
{
    "name": "propName",
    "buyer": [
        {
            "name": "buyerName"
        }
    ]
}

// After
{
    "name": "propName",
    "buyer": [
        "buyerName"
    ]
}
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 88.9% 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 ~9 days

Total

3

Last Release

1782d ago

### Community

Maintainers

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

---

Top Contributors

[![Zelak312](https://avatars.githubusercontent.com/u/17593965?v=4)](https://github.com/Zelak312 "Zelak312 (8 commits)")[![Soraclee](https://avatars.githubusercontent.com/u/69369114?v=4)](https://github.com/Soraclee "Soraclee (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

PHPackages © 2026

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