PHPackages                             web-fu/anymapper - 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. web-fu/anymapper

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

web-fu/anymapper
================

Library that allows to map objects and arrays into objects and arrays with strong type support and pattern detection.

v2.0.2(5mo ago)214MITPHPPHP ^8.1

Since Dec 27Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/web-fu/anymapper)[ Packagist](https://packagist.org/packages/web-fu/anymapper)[ RSS](/packages/web-fu-anymapper/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (7)Versions (14)Used By (0)

AnyMapper
=========

[](#anymapper)

[![Latest Stable Version](https://camo.githubusercontent.com/023993ae1ae7b3a40e98e31e8e2f38f5d5e6eeb47957d4d63f1dadfdad11b6dc/68747470733a2f2f706f7365722e707567782e6f72672f7765622d66752f616e796d61707065722f76)](https://packagist.org/packages/web-fu/anymapper)[![PHP Version Require](https://camo.githubusercontent.com/8312ea81f0b6012f12d903d4a7e8553b449104871339c04a7f840adca72f35dd/68747470733a2f2f706f7365722e707567782e6f72672f7765622d66752f616e796d61707065722f726571756972652f706870)](https://packagist.org/packages/web-fu/anymapper)[![Test status](https://github.com/web-fu/anymapper/actions/workflows/tests.yaml/badge.svg)](https://github.com/web-fu/anymapper/actions/workflows/tests.yaml/badge.svg)[![Static analysis status](https://github.com/web-fu/anymapper/actions/workflows/static-analysis.yml/badge.svg)](https://github.com/web-fu/anymapper/actions/workflows/static-analysis.yml/badge.svg)[![Code style status](https://github.com/web-fu/anymapper/actions/workflows/code-style.yaml/badge.svg)](https://github.com/web-fu/anymapper/actions/workflows/code-style.yaml/badge.svg)

### A library that allows to map objects and arrays into objects and arrays with strong type support and pattern detection.

[](#a-library-that-allows-to-map-objects-and-arrays-into-objects-and-arrays-with-strong-type-support-and-pattern-detection)

AnyMapper can get a variety of data inputs (object, arrays and composite of both) and hydrate a destination object or array ensuring safe type handling for the data during the process.
AnyMapper can detect and extract data from public properties, standard getter / setter, use constructors and class factories and optionally perform smart data casting (ie: from a string to a date time).
AnyMapper will not interfere with private or protected properties or methods and cannot grant the resulting object is in a "valid state"

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

[](#installation)

web-fu/anymapper is available on [Packagist](https://packagist.org/packages/web-fu/anymapper) and can be installed using [Composer](https://getcomposer.org/).

```
composer require web-fu/anymapper
```

> Requires PHP 8.1 or newer.

Examples
--------

[](#examples)

### Simple Mapping

[](#simple-mapping)

```
final class MyClass
{
    private string $foo;
    public string $bar;

    public function setFoo(string $foo): MyClass {
        $this->foo = $foo . ' and I was set in a setter';
        return $this;
    }

    public function getFoo(): string
    {
        return $this->foo;
    }
}

$source = [
    'foo' => 'I am foo',
    'bar' => 'I am bar',
];

// Fill an existing class
$destination = new MyClass();

(new \WebFu\AnyMapper\AnyMapper())
    ->map($source)
    ->into($destination)
    ->run();

echo $destination->getFoo(); // I am foo and I was set in a setter
echo PHP_EOL;
echo $destination->bar; // I am bar;
echo PHP_EOL;

// Create a new object of a class
$destination = (new \WebFu\AnyMapper\AnyMapper())
    ->map($source)
    ->as(MyClass::class)
    ->run();

echo $destination->getFoo(); // I am foo and I was set in a setter
echo PHP_EOL;
echo $destination->bar; // I am bar;
echo PHP_EOL;
```

### Casting Strategy

[](#casting-strategy)

```
// Use a strategy to customize mapping
final class MyClass
{
    public DateTime $value;
}

$source = [
    'value' => '2022-12-01',
];

$destination = (new \WebFu\AnyMapper\AnyMapper())
    ->map($source)
    ->using(
        (new \WebFu\AnyMapper\Strategy\AllowedCastingStrategy())
            ->allow('string', DateTime::class)
    )
    ->as(MyClass::class)
    ->run();

echo $destination->value->format('Y-m-d H:i:s'); // 2022-12-01 00:00:00
echo PHP_EOL;
```

### Casting through callbacks

[](#casting-through-callbacks)

```
final class MyClass
{
    public int $value;
}

$source = [
    'value' => true,
];

$destination =  (new \WebFu\AnyMapper\AnyMapper())
    ->map($source)
    ->using(
        (new \WebFu\AnyMapper\Strategy\CallbackCastingStrategy())
            ->addMethod(
                'bool',
                'int',
                fn (bool $value) => (int) $value,
            )
    )
    ->as(MyClass::class)
    ->run();

echo $destination->value; // 1
```

### DocBlock Type Support

[](#docblock-type-support)

```
// Use a strategy to customize mapping
final class MyClass
{
    /** @var DateTime */
    public $value;
}

$source = [
    'value' => '2022-12-01',
];

$destination = (new \WebFu\AnyMapper\AnyMapper())
    ->map($source)
    ->using(
        (new \WebFu\AnyMapper\Strategy\DocBlockDetectStrategy())
    )
    ->as(MyClass::class)
    ->run();

echo $destination->value->format('Y-m-d H:i:s'); // 2022-12-01 00:00:00
echo PHP_EOL;
```

### Serialization

[](#serialization)

```
// Perform a standard serialization
final class Bar
{
    private string $element;

    public function getElement(): string
    {
        return $this->element;
    }

    public function setElement(string $element): Bar
    {
        $this->element = $element;

        return $this;
    }
}

final class Foo
{
    /** @var Bar[] */
    private array $bars;

    /**
     * @return array
     */
    public function getBars(): array
    {
        return $this->bars;
    }

    /**
     * @param array $bars
     * @return Foo
     */
    public function setBars(array $bars): Foo
    {
        $this->bars = $bars;
        return $this;
    }
}

$foo = new Foo();
$foo->setBars([
    (new Bar())->setElement('string'),
]);

$destination =  (new \WebFu\AnyMapper\AnyMapper())
    ->map($foo)
    ->serialize();

var_export($destination);
/*
array (
  'bars' =>
  array (
    0 =>
    array (
      'element' => 'string',
    ),
  ),
)
*/
```

See `/examples` folder for full examples

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance73

Regular maintenance activity

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity62

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

Recently: every ~241 days

Total

12

Last Release

151d ago

Major Versions

0.5.0 → 1.0.02023-08-02

1.0.0 → v2.0.02024-05-02

PHP version history (3 changes)0.1.0PHP 8.0.\* || 8.1.\* || 8.2.\*

v2.0.0PHP 8.1.\* || 8.2.\* || 8.3.\*

v2.0.2PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/67ea52e8c69cddde94034b5d9006666d238a7b4266791954f5ce80e7264b722b?d=identicon)[web-fu](/maintainers/web-fu)

---

Top Contributors

[![web-fu](https://avatars.githubusercontent.com/u/2857138?v=4)](https://github.com/web-fu "web-fu (251 commits)")

---

Tags

arrayconversionobjectmappingmapperhydrator

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/web-fu-anymapper/health.svg)

```
[![Health](https://phpackages.com/badges/web-fu-anymapper/health.svg)](https://phpackages.com/packages/web-fu-anymapper)
```

###  Alternatives

[cuyz/valinor

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

1.5k13.2M174](/packages/cuyz-valinor)[symfony/property-access

Provides functions to read and write from/to an object or array using a simple string notation

2.8k317.3M3.2k](/packages/symfony-property-access)[orisai/object-mapper

Raw data mapping to validated objects

1133.6k2](/packages/orisai-object-mapper)[nutgram/hydrator

Hydrator for PHP 8.0+

12346.2k9](/packages/nutgram-hydrator)[jasny/dotkey

Dot notation access for objects and arrays

15223.8k7](/packages/jasny-dotkey)[selective/transformer

A strictly typed array transformer with dot-access, fluent interface and filters.

3819.8k1](/packages/selective-transformer)

PHPackages © 2026

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