PHPackages                             guyliangilsing/php-class-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. guyliangilsing/php-class-mapper

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

guyliangilsing/php-class-mapper
===============================

A simple package that provides you with a simple class mapping system.

1.1.2(3y ago)022MITPHPPHP &gt;=8.0

Since Jun 18Pushed 3y ago1 watchersCompare

[ Source](https://github.com/GuylianGilsing/PHPClassMapper)[ Packagist](https://packagist.org/packages/guyliangilsing/php-class-mapper)[ RSS](/packages/guyliangilsing-php-class-mapper/feed)WikiDiscussions main Synced 3w ago

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

PHPClassMapper
==============

[](#phpclassmapper)

A simple package that provides you with a simple class mapping system.

Table of Contents
-----------------

[](#table-of-contents)

- [PHPClassMapper](#phpclassmapper)
    - [Table of Contents](#table-of-contents)
    - [Features](#features)
    - [Installation](#installation)
    - [usage](#usage)
        - [Class to class mapping](#class-to-class-mapping)
            - [Configuring the mapper](#configuring-the-mapper)
            - [Creating a mapping](#creating-a-mapping)
            - [Creating the mapper](#creating-the-mapper)
            - [Using the mapper](#using-the-mapper)
        - [Array mapping](#array-mapping)
            - [Configuring the mapper](#configuring-the-mapper-1)
            - [Creating a mapping](#creating-a-mapping-1)
            - [Creating the mapper](#creating-the-mapper-1)
            - [Using the mapper](#using-the-mapper-1)
    - [Dependency injection containers](#dependency-injection-containers)

Features
--------

[](#features)

PHPClassMapper comes with the following features:

- Class to class mapping
- Array to class mapping
- Class to array mapping

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

[](#installation)

```
$ composer require guyliangilsing/php-class-mapper
```

usage
-----

[](#usage)

### Class to class mapping

[](#class-to-class-mapping)

#### Configuring the mapper

[](#configuring-the-mapper)

You need to supply a configuration class before you can create the mapper class. You can create a configuration as follows:

```
use PHPClassMapper\Configuration\MapperConfiguration;

$configuration = new MapperConfiguration();
```

In this configuration you can add a mapping with the following code:

```
use PHPClassMapper\Configuration\MapperConfiguration;

$configuration = new MapperConfiguration();
$configuration->addMapping(Source::class, Destination::class, new MyMapping());
```

#### Creating a mapping

[](#creating-a-mapping)

To let the mapper know how to map your classes, you need to provide it with a mapping. A mapping can be created as follows:

```
use PHPClassMapper\Exceptions\MissingContextDataFieldException;
use PHPClassMapper\Exceptions\MissingMappingException;
use PHPClassMapper\MapperInterface;

class MyMapping implements MappingInterface
{
    /**
     * Maps one class into another class.
     *
     * @param object $source The class that needs to be mapped to a different class.
     * @param array $contextData An associative array (key => value) that gives the mapper additional
     * data to work with.
     * @param MapperInterface $mapper An extra mapper instance to map objects with.
     *
     * @throws InvalidArgumentException when a `MyClass::name` string refers to a class that does not exist.
     * @throws MissingMappingException when a mapping between two class types can't be found.
     * @throws MissingContextDataFieldException when a specific context data field can't be found.
     */
    public function mapObject(object $source, array $contextData, MapperInterface $mapper): object;
    {
        if (!($source instanceof Source))
        {
            throw new MissingMappingException($source::class, Destination::class);
        }

        return new Destination();
    }
}
```

#### Creating the mapper

[](#creating-the-mapper)

Once you have your configuration, you can instantiate a mapper:

```
use PHPClassMapper\Configuration\MapperConfiguration;
use PHPClassMapper\Mapper;

$configuration = new MapperConfiguration();

// Do your configuration logic here...

$mapper = new Mapper($configuration);
```

#### Using the mapper

[](#using-the-mapper)

Once you have the mapper instantiated, you can use it in the following way:

```
use PHPClassMapper\Configuration\MapperConfiguration;
use PHPClassMapper\Mapper;

$configuration = new MapperConfiguration();

// Do your configuration logic here...

$mapper = new Mapper($configuration);

// Without contextvariables
$mapper->map($source, Destination::class);

// With context variables
$mappedClass = $mapper->map($source, Destination::class, [
    'contextFieldOne' => 'Hello',
    'contextFieldTwo' => 'World'
]);
```

### Array mapping

[](#array-mapping)

#### Configuring the mapper

[](#configuring-the-mapper-1)

You need to supply a configuration class before you can create the mapper class. You can create a configuration as follows:

```
use PHPClassMapper\Configuration\ArrayMapperConfiguration;

$configuration = new ArrayMapperConfiguration();
```

In this configuration you can add two different mapping types:

- To array mapping (class -&gt; array)
- From array mapping (array -&gt; class)

**To array mapping**

```
use PHPClassMapper\Configuration\ArrayMapperConfiguration;

$configuration = new ArrayMapperConfiguration();
$configuration->addToArrayMapping(Source::class, new ToArrayMapping());
```

**From array mapping**

```
use PHPClassMapper\Configuration\ArrayMapperConfiguration;

$configuration = new ArrayMapperConfiguration();
$configuration->addFromArrayMapping(Destination::class, new FromArrayMapping());
```

#### Creating a mapping

[](#creating-a-mapping-1)

Since you can map arrays in two different ways, two different interfaces are being used:

- ToArrayMappingInterface
- FromArrayMappingInterface

**To array mapping**

```
use PHPClassMapper\ArrayMapperInterface;
use PHPClassMapper\Configuration\ToArrayMappingInterface;

final class ToArrayMapping implements ToArrayMappingInterface
{
    /**
     * Maps an object into an array.
     *
     * @param object $source The class that needs to be mapped to an array.
     * @param array $contextData An associative array (key => value) that gives the mapper additional
     * data to work with.
     * @param ArrayMapperInterface $mapper An extra mapper instance to map objects with.
     *
     * @throws InvalidArgumentException when a `MyClass::name` string refers to a class that does not exist.
     * @throws MissingMappingException when a mapping between two class types can't be found.
     * @throws MissingContextDataFieldException when a specific context data field can't be found.
     *
     * @return array
     */
    public function mapObject(object $source, array $contextData, ArrayMapperInterface $mapper): array;
    {
        return [];
    }
}
```

**From array mapping**

```
use PHPClassMapper\ArrayMapperInterface;
use PHPClassMapper\Configuration\FromArrayMappingInterface;

final class FromArrayMapping implements FromArrayMappingInterface
{
    /**
     * Maps an array into an object.
     *
     * @param array $source The class that needs to be mapped to a different class.
     * @param array $contextData An associative array (key => value) that gives the mapper additional
     * data to work with.
     * @param ArrayMapperInterface $mapper An extra mapper instance to map nested arrays with.
     *
     * @throws InvalidArgumentException when a `MyClass::name` string refers to a class that does not exist.
     * @throws MissingMappingException when a mapping between two class types can't be found.
     * @throws MissingContextDataFieldException when a specific context data field can't be found.
     */
    public function mapObject(array $source, array $contextData, ArrayMapperInterface $mapper): object;
    {
        return new Destination();
    }
}
```

#### Creating the mapper

[](#creating-the-mapper-1)

Once you have your configuration, you can instantiate a mapper:

```
use PHPClassMapper\Configuration\ArrayMapperConfiguration;
use PHPClassMapper\ArrayMapper;

$configuration = new ArrayMapperConfiguration();

// Do your configuration logic here...

$mapper = new ArrayMapper($configuration);
```

#### Using the mapper

[](#using-the-mapper-1)

Once you have the mapper instantiated, you can use it in the following way:

```
use PHPClassMapper\Configuration\ArrayMapperConfiguration;
use PHPClassMapper\ArrayMapper;

$configuration = new ArrayMapperConfiguration();

// Do your configuration logic here...

$mapper = new ArrayMapper($configuration);

// To array mapping
$objToMap = // Your object here...
$mappedArray = $mapper->toArray($objToMap);

// From array mapping
$mappedObject = $mapper->fromArray([], Destination::class);
```

Dependency injection containers
-------------------------------

[](#dependency-injection-containers)

You can register the mapper with your favorite dependency injection container by using the `MapperInterface` and `MapperConfigurationInterface` interfaces.

###  Health Score

25

—

LowBetter than 36% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

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

Total

4

Last Release

1307d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/47211491?v=4)[Guylian Gilsing](/maintainers/GuylianGilsing)[@GuylianGilsing](https://github.com/GuylianGilsing)

---

Top Contributors

[![GuylianGilsing](https://avatars.githubusercontent.com/u/47211491?v=4)](https://github.com/GuylianGilsing "GuylianGilsing (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/guyliangilsing-php-class-mapper/health.svg)

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

###  Alternatives

[rias/statamic-redirect

29322.9k](/packages/rias-statamic-redirect)[danielstjules/sliceable-stringy

Python string slices in PHP

4751.6k1](/packages/danielstjules-sliceable-stringy)

PHPackages © 2026

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