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

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

silvesterk/object-mapper
========================

A simple PHP object mapping library with attribute support

v1.1.0(11mo ago)020MITPHPPHP &gt;=8.1

Since Jun 3Pushed 11mo agoCompare

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

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

ObjectMapper
============

[](#objectmapper)

A PHP library for mapping properties between objects with support for attributes and type safety.

Features
--------

[](#features)

- Map properties from a source object to a target object.
- Support for custom property mapping using attributes.
- Handles uninitialized properties gracefully.
- Throws exceptions for type mismatches or mapping errors.

Requirements
------------

[](#requirements)

- PHP 8.1 or higher
- Composer

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

[](#installation)

Install the library using Composer:

```
composer require silvesterk/object-mapper
```

Usage
-----

[](#usage)

### Basic Mutable Mapping

[](#basic-mutable-mapping)

```
use Silvesterk\ObjectMapper\ObjectMapper;
use Silvesterk\ObjectMapper\Tests\TestClass\SourceTestClassWithNoAttributes;
use Silvesterk\ObjectMapper\Tests\TestClass\DestinationTestClass;

$source = new SourceTestClassWithNoAttributes();
$source->testProp = 42;

$destination = new DestinationTestClass();

ObjectMapper::map($source, $destination);

echo $destination->testProp; // Outputs: 42
```

### Immutable Mapping

[](#immutable-mapping)

```
use Silvesterk\ObjectMapper\ObjectMapper;
use Silvesterk\ObjectMapper\Tests\TestClass\SourceTestClassWithNoAttributes;
use Silvesterk\ObjectMapper\Tests\TestClass\DestinationTestClass;

$source = new SourceTestClassWithNoAttributes();
$source->testProp = 42;

$destination = new DestinationTestClass();

$newDestination = ObjectMapper::mapImmutable($source, $destination);

echo $newDestination->testProp; // Outputs: 42
```

### Mapping with Attributes

[](#mapping-with-attributes)

Use attributes to map properties with different names between source and target objects.

```
use Silvesterk\ObjectMapper\ObjectMapper;
use Silvesterk\ObjectMapper\Tests\TestClass\SourceTestClassWithTargetAttribute;
use Silvesterk\ObjectMapper\Tests\TestClass\DestinationTestClass;

class SourceTestClassWithTargetAttribute
{
    #[Target(propertyName: 'differentProp')]
    public ?int $testProp = null;
}

$source = new SourceTestClassWithTargetAttribute();
$source->testProp = 42;

$destination = new DestinationTestClass();
$mappedObject = ObjectMapper::mapImmutable($source, $destination);

echo $mappedObject->differentProp; // Outputs: 42
```

### Handling Type Mismatches

[](#handling-type-mismatches)

If a property type mismatch occurs, an `ObjectMapperException` is thrown.

```
use Silvesterk\ObjectMapper\ObjectMapper;
use Silvesterk\ObjectMapper\Exception\ObjectMapperException;
use Silvesterk\ObjectMapper\Tests\TestClass\SourceTestClassWithWrongDataType;
use Silvesterk\ObjectMapper\Tests\TestClass\DestinationTestClass;

$source = new SourceTestClassWithWrongDataType();
$source->testPropInt = 'Invalid Type';

$destination = new DestinationTestClass();

try {
    ObjectMapper::mapImmutable($source, $destination);
} catch (ObjectMapperException $e) {
    echo $e->getMessage(); // Outputs the error message
}
```

Testing
-------

[](#testing)

Run the tests using PHPUnit:

```
vendor/bin/phpunit
```

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

[](#contributing)

1. Fork the repository.
2. Create a new branch for your feature or bugfix.
3. Commit your changes and push the branch.
4. Open a pull request.

License
-------

[](#license)

This project is licensed under the MIT License. See the `LICENSE` file for details.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance54

Moderate activity, may be stable

Popularity6

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 83.3% 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 ~0 days

Total

2

Last Release

340d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/92d92734a8b9acb15991aac3419ee805346a6558d254573c378aac04bfffbdaa?d=identicon)[silvesterk](/maintainers/silvesterk)

---

Top Contributors

[![silvesterk](https://avatars.githubusercontent.com/u/12173921?v=4)](https://github.com/silvesterk "silvesterk (5 commits)")[![skokai](https://avatars.githubusercontent.com/u/227112596?v=4)](https://github.com/skokai "skokai (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

PHPackages © 2026

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