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

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

nddcoder/object-mapper
======================

Object mapper for PHP

1.0.0(4y ago)1641MITPHPPHP ^8.0

Since Dec 4Pushed 4y ago2 watchersCompare

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

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

Object mapper
=============

[](#object-mapper)

[![Latest Version on Packagist](https://camo.githubusercontent.com/a902600ffd326d4e8d1ac0ff51fa37cc61bbb5fcb0356161dc059cec1c8fb0b8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e6464636f6465722f6f626a6563742d6d61707065722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/nddcoder/object-mapper)[![GitHub Tests Action Status](https://camo.githubusercontent.com/5eadaf6c920702be48afea34b4ea8d45dca885489f78b3a83133b4707d706fb8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f64616e6764756e67636e74742f6f626a6563742d6d61707065722f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/nddcoder/object-mapper/actions?query=workflow%3Arun-tests+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/f1e004ad3ef9aefea6535f5a089b839788b3f074dd212ecf9bb50d4388a31096/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6e6464636f6465722f6f626a6563742d6d61707065722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/nddcoder/object-mapper)

An `ObjectMapper` for PHP (inspired by ObjectMapper in java)

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

[](#installation)

You can install the package via composer:

```
composer require nddcoder/object-mapper
```

Usage
-----

[](#usage)

```
use Nddcoder\ObjectMapper\ObjectMapper;

class User {
    public string $name;
    public string $email;
}

$objectMapper = new ObjectMapper();

//Make object from json string
$jsonString = '{"name":"Dung Nguyen Dang","email":"dangdungcntt@gmail.com"}';
$user = $objectMapper->readValue($jsonString, User::class);
echo $user->name; //Dung Nguyen Dang
echo $user->email; //dangdungcntt@gmail.com

//You can pass an associative array to readValue function instead of string
$user = $objectMapper->readValue(['name' => 'Dung Nguyen Dang', 'email' => 'dangdungcntt@gmail.com'], User::class);

//Convert object to json string
$userJsonString = $objectMapper->writeValueAsString($user);
echo $userJsonString; //{"name":"Dung Nguyen Dang","email":"dangdungcntt@gmail.com"}
```

#### Array Property

[](#array-property)

Use `ArrayProperty` Attribute to specific type of array item

```
use Nddcoder\ObjectMapper\ObjectMapper;
use Nddcoder\ObjectMapper\Attributes\ArrayProperty;

class Comment {
    public string $from;
    public string $content;
}

class Post {
    public string $title;

    #[ArrayProperty(Comment::class)]
    public array $comments;
}

$objectMapper = new ObjectMapper();

//Make object from json string
$jsonString = '{"title":"New Blog Post","comments":[{"from":"nddcoder","content":"Hello"}]}';
$post = $objectMapper->readValue($jsonString, Post::class);
echo $post->title; //New Blog Post
print_r($post->comments);
/*
Array
(
    [0] => Comment Object
        (
            [from] => nddcoder
            [content] => Hello
        )

)
*/
```

#### Custom JSON property

[](#custom-json-property)

You can use `JsonProperty('')` to custom name for a property

```
use Nddcoder\ObjectMapper\ObjectMapper;
use Nddcoder\ObjectMapper\Attributes\JsonProperty;

class Post {
    public string $title;

    #[JsonProperty('body')]
    public string $content;
}

$objectMapper = new ObjectMapper();

//Make object from json string
$jsonString = '{"title":"New Blog Post","body":"Blog body here"}';
$post = $objectMapper->readValue($jsonString, Post::class);
echo $post->title; //New Blog Post
echo $post->content; //Blog body here

//Convert object to json string
$postJsonString = $objectMapper->writeValueAsString($post);
echo $postJsonString; //{"title":"New Blog Post","body":"Blog body here"}
```

#### Custom behavior via getter/setter

[](#custom-behavior-via-gettersetter)

You can define getter/setter to custom behavior when get/set a property

```
use Nddcoder\ObjectMapper\ObjectMapper;
use Nddcoder\ObjectMapper\Attributes\JsonProperty;

class User {
    public string $username;
    protected string $password;

    public function setUsername(string $username): void
    {
        $this->username = strtolower($username);
    }

    public function getPassword(): ?string
    {
        return null;
    }

    public function setPassword(string $password): void
    {
        $this->password = md5($password);
    }
}

$objectMapper = new ObjectMapper();

//Make object from json string
$jsonString = '{"username":"NDDCoder","password":"secret"}';
$user = $objectMapper->readValue($jsonString, User::class);
print_r($user);
/*
User Object
(
    [username] => nddcoder
    [password:protected] => 5ebe2294ecd0e0f08eab7690d2a6ee69
)
*/

//Convert object to json string
$userJsonString = $objectMapper->writeValueAsString($user);
echo $userJsonString; //{"username":"nddcoder","password":null}
```

#### Encoders

[](#encoders)

By default, package included 2 encoders for `DateTimeInterface` and `stdClass`

You can create your custom encoder by implements `ObjectMapperEncoder` interface

```
use MongoDB\BSON\ObjectId;
use Nddcoder\ObjectMapper\Contracts\ObjectMapperEncoder;

class ObjectIdEncoder implements ObjectMapperEncoder
{
    public function encode(mixed $value, ?string $className = null): string
    {
        return (string) $value;
    }

    public function decode(mixed $value, ?string $className = null): mixed
    {
        return new ObjectId($value);
    }
}
```

and then using `ObjectMapper::addGlobalEncoder` to add it as global or `addEncoder` to add it to current instance only

```
ObjectMapper::addGlobalEncoder(ObjectId::class, ObjectIdEncoder::class);
$objectMapper->addEncoder(ObjectId::class, ObjectIdEncoder::class);
```

You can remove global encoder using `ObjectMapper::removeGlobalEncoder` or `removeEncoder` to remove encoder from current instance only

```
ObjectMapper::removeGlobalEncoder(ObjectId::class);
$objectMapper->removeEncoder(ObjectId::class);
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Dung Nguyen Dang](https://github.com/dangdungcntt)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity61

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

Total

4

Last Release

1673d ago

Major Versions

v0.0.3 → 1.0.02021-10-18

### Community

Maintainers

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

---

Top Contributors

[![dangdungcntt](https://avatars.githubusercontent.com/u/22521948?v=4)](https://github.com/dangdungcntt "dangdungcntt (37 commits)")

---

Tags

object-mapperphpobject mappernddcoder

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[opportus/object-mapper

Maps generically source to target objects via extensible strategies and controls.

181.5k1](/packages/opportus-object-mapper)

PHPackages © 2026

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