PHPackages                             xp-forge/marshalling - 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. xp-forge/marshalling

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

xp-forge/marshalling
====================

Marshalling

v2.4.0(1y ago)1111.3k—3.6%[1 PRs](https://github.com/xp-forge/marshalling/pulls)5BSD-3-ClausePHPPHP &gt;=7.0.0

Since Aug 29Pushed 1y ago1 watchersCompare

[ Source](https://github.com/xp-forge/marshalling)[ Packagist](https://packagist.org/packages/xp-forge/marshalling)[ Docs](http://xp-framework.net/)[ RSS](/packages/xp-forge-marshalling/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (17)Used By (5)

Marshalling
===========

[](#marshalling)

[![Build status on GitHub](https://github.com/xp-forge/marshalling/workflows/Tests/badge.svg)](https://github.com/xp-forge/marshalling/actions)[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)[![Requires PHP 7.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_0plus.svg)](http://php.net/)[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)[![Latest Stable Version](https://camo.githubusercontent.com/8b72b5de2f80bdb40e705bdbbb709435be465219fceb92243bff44c52f036227/68747470733a2f2f706f7365722e707567782e6f72672f78702d666f7267652f6d61727368616c6c696e672f76657273696f6e2e737667)](https://packagist.org/packages/xp-forge/marshalling)

Marshalling converts objects to maps and vice versa.

Example
-------

[](#example)

All primitives, arrays and maps thereof are yielded as-is:

```
use util\data\Marshalling;

$m= new Marshalling();
$m->marshal('Test');            // "Test"
$m->marshal([1, 2, 3]);         // [1, 2, 3]
$m->marshal(['admin' => true]); // ["admin" => true]
```

Value objects are marshalled using field =&gt; getter lookups; supporting both *method named for field* and *getField* conventions.

```
use util\data\Marshalling;

class Person {
  private $name, $age;

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

  public function name(): string { return $this->name; }
  public function age(): int { return $this->age; }
}

$m= new Marshalling();
$m->marshal(new Person('...', 42)); // ["name" => "...", "age" => 42]
```

When unmarshalling from maps, pass the type as second parameter. Objects are created without invoking the constructor, and by either setting the fields directly or by using the *setField* convention.

```
use util\data\Marshalling;

$m= new Marshalling();
$person= $m->unmarshal(['name' => '...', 'age' => 42], Person::class);
```

Types from the `util` package are handled by default:

```
use util\data\Marshalling;
use util\{Date, Bytes, Money, UUID};

$m= new Marshalling();
$m->marshal(Date::now());                   // "2018-08-29T10:40:49+0200" (ISO 8601)
$m->marshal(new Bytes("\x50\x4b\x03\x04")); // "UEsDBA==" (base64)
$m->marshal(new Money(12.99, $currency));   // ["amount" => 12.99, "currency" => "EUR"]
$m->marshal(UUID::randomUUID());            // ["value" => "a2b15f7b-f6e0-45fa-9d7f-703fda05d4ac"]
```

Custom logic
------------

[](#custom-logic)

To supply a function for marshalling and unmarshalling, use *mapping()* and *resolving()*:

```
use util\data\Marshalling;

class User {
  public function __construct(public int $id, public string $name) { }
}

$marshalling= (new Marshalling())
  ->mapping(User::class, fn($user) => $user->id)
  ->resolving(User::class, fn($value) => new User($value, posix_getpwuid($value)['name'])
;
$value= $marshalling->marshal(new User(0, 'root'));
// 0

$user= $marshalling->unmarshal($value, User::class);
// User(id: 0, name: 'root')
```

See also
--------

[](#see-also)

-
-

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance40

Moderate activity, may be stable

Popularity31

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity65

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

Recently: every ~115 days

Total

14

Last Release

505d ago

Major Versions

v0.3.2 → v1.0.02019-12-01

v1.1.1 → v2.0.02023-09-24

PHP version history (2 changes)v0.1.0PHP &gt;=5.6.0

v1.0.0PHP &gt;=7.0.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/07d18d882c8b4aaf3466432f64018214f2771eda333202175431ee7233795376?d=identicon)[thekid](/maintainers/thekid)

---

Top Contributors

[![thekid](https://avatars.githubusercontent.com/u/696742?v=4)](https://github.com/thekid "thekid (87 commits)")

---

Tags

datamarshallingobject-mappingxp-frameworkmodulexp

### Embed Badge

![Health badge](/badges/xp-forge-marshalling/health.svg)

```
[![Health](https://phpackages.com/badges/xp-forge-marshalling/health.svg)](https://phpackages.com/packages/xp-forge-marshalling)
```

PHPackages © 2026

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