PHPackages                             shureban/laravel-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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. shureban/laravel-object-mapper

ActiveLaravel-package[Parsing &amp; Serialization](/categories/parsing)

shureban/laravel-object-mapper
==============================

Laravel SDK mapping data in object

1.2.0(2y ago)35.9k↓100%1[1 PRs](https://github.com/Shureban/laravel-object-mapper/pulls)1MITPHPPHP ^8.1

Since Nov 25Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Shureban/laravel-object-mapper)[ Packagist](https://packagist.org/packages/shureban/laravel-object-mapper)[ RSS](/packages/shureban-laravel-object-mapper/feed)WikiDiscussions master Synced today

READMEChangelog (9)DependenciesVersions (10)Used By (1)

Laravel object mapper
=====================

[](#laravel-object-mapper)

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

[](#installation)

Require this package with composer using the following command:

```
composer require shureban/laravel-object-mapper
```

Add the following class to the `providers` array in `config/app.php`:

```
Shureban\LaravelObjectMapper\ObjectMapperServiceProvider::class,
```

You can also publish the config file to change implementations (ie. interface to specific class).

```
php artisan vendor:publish --provider="Shureban\LaravelObjectMapper\ObjectMapperServiceProvider"
```

How to use
----------

[](#how-to-use)

You have 3 options to use `ObjectMapper`

### Inheritance

[](#inheritance)

Your mapped object (dto) must inheritance from `\Shureban\LaravelObjectMapper\MappableObject`

```
class User extends MappableObject
{
    public int $id;
}

$user1 = (new User())->mapFromJson('{"id": 10}');
$user2 = (new User())->mapFromArray(['id' => 10]);
$user3 = (new User())->mapFromRequest($formRequest);
```

### Using trait

[](#using-trait)

Your mapped object (dto) must use `\Shureban\LaravelObjectMapper\MappableTrait`

```
class User
{
    use MappableTrait;

    public int $id;
}

$user1 = (new User())->mapFromJson('{"id": 10}');
$user2 = (new User())->mapFromArray(['id' => 10]);
$user3 = (new User())->mapFromRequest($formRequest);
```

### Delegate mapping to ObjectMapper

[](#delegate-mapping-to-objectmapper)

```
class User {
    public int $id;
}

$user1 = (new ObjectMapper(new User()))->mapFromJson('{"id": 10}');
$user2 = (new ObjectMapper(new User()))->mapFromArray(['id' => 10]);
$user3 = (new ObjectMapper(new User()))->mapFromRequest($formRequest);
```

Mappable cases
--------------

[](#mappable-cases)

Below you will see cases which you can use for mapping data into your object

### Simple types

[](#simple-types)

- `mixed`
- `string`
- `bool`, `boolean`
- `int`, `integer`
- `double`, `float`
- `array`
- `object`

### Box types

[](#box-types)

- `Carbon`
- `DateTime`
- `Collection`

### Custom types

[](#custom-types)

- `CustomClass`
- `Enum`
- `Eloquent`

### Array of types

[](#array-of-types)

That typo of mapping may be realized only via phpDoc notation

- `int[]`
- `int[][]`
- `DateTime[]`
- `CustomClass[]`

### Special cases

[](#special-cases)

#### Constructor

[](#constructor)

If your type object has 1 required parameter and value is NOT an array, ObjectMapper will build instance of this type via constructor call If your type object has 0 or more than 1 required parameters, it will throw WrongConstructorParametersNumberException exception

Correct case:

```
class User
{
    public int $id;

    public function __construct(int $id) {
        $this->id = $id;
    }
}
```

Wrong case:

```
class User
{
    public int    $id;
    public string $name;

    public function __construct(int $id, string $name) {
        $this->id   = $id;
        $this->name = $name;
    }
}
```

#### PhpDoc

[](#phpdoc)

PhpDoc type hinting has much more priority than main type.

```
class User
{
    /**
    * @var int
    */
    public int $id;
    /**
    * @var DateTime
    */
    public $dateOfBirthday;
    /**
    * @var Address[]
    */
    public array $addresses;
}
```

#### Setters

[](#setters)

If you want to realize your own logic for setting value, you may place setter method in your mapped object This setter should start from `set` word and been in camelCase notation.

```
class User
{
    public string   $id;
    public DateTime $dateOfBirthday;

    public function setId(int $id, array $rawData = []): void
    {
        $this->id = Hash::make($id);
    }

    public function setDateOfBirthday(string $dateOfBirthday, array $rawData = []): void
    {
        $this->dateOfBirthday = new DateTime($dateOfBirthday);
    }
}

$user = (new ObjectMapper(new User()))->mapFromArray(['id' => 10, 'dateOfBirthday' => '1991-01-01']);

echo $user->id; // $2y$10$XqHrk0oXa7.9AihthdVxW.dd637zj9EhlTJX0eUEKiV61dbs7a7ZO
echo $user->dateOfBirthday->format('Y'); // 1991
```

Some words about second parameter `$rawData`. Value of this parameter depends on method selected for mapping

- mapFromJson - $rawData will be JSON
- mapFromArray - $rawData will be Array
- mapFromRequest - $rawData will be FormRequest object

#### Readonly parameters

[](#readonly-parameters)

Readonly parameters will always be skipped

```
class User
{
    public readonly int $id;
}

$user = (new ObjectMapper(new User()))->mapFromArray(['id' => 10]);

echo $user->id; // 0
```

Config rewriting
----------------

[](#config-rewriting)

In `object_mapper.php` config file have been presented all mappable types classes. You have opportunity to rewrite mapping flow or realize you own one.

If you need to create your own type mapping, follow this way:

- create class inherited from `\Shureban\LaravelObjectMapper\Types\Type`
- place you type into config file in `type -> box` array

###  Health Score

33

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity60

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

Recently: every ~102 days

Total

9

Last Release

801d ago

Major Versions

0.4.0 → 1.0.02022-12-01

PHP version history (2 changes)0.0.6PHP ^8.0

0.2.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/d6df85acf6d2ae745da6af44e0db9c97e64c528ef5b9b0276bad5c4ac7f3888a?d=identicon)[Shureban](/maintainers/Shureban)

---

Top Contributors

[![Shureban](https://avatars.githubusercontent.com/u/5560764?v=4)](https://github.com/Shureban "Shureban (30 commits)")

---

Tags

jsonobjectmappingshureban

### Embed Badge

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

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

###  Alternatives

[justinrainbow/json-schema

A library to validate a json schema.

3.6k311.5M536](/packages/justinrainbow-json-schema)[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k463.5M118](/packages/mtdowling-jmespathphp)[jms/serializer

Library for (de-)serializing data of any complexity; supports XML, and JSON.

2.3k133.8M789](/packages/jms-serializer)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k88.3M577](/packages/jms-serializer-bundle)[cuyz/valinor

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

1.5k8.1M86](/packages/cuyz-valinor)[colinodell/json5

UTF-8 compatible JSON5 parser for PHP

30421.2M37](/packages/colinodell-json5)

PHPackages © 2026

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