PHPackages                             spiral/marshaller - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. spiral/marshaller

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

spiral/marshaller
=================

Marshaller package

v1.0.0(2y ago)18451MITPHPPHP &gt;=8.1CI failing

Since Jul 31Pushed 2y ago2 watchersCompare

[ Source](https://github.com/spiral/marshaller)[ Packagist](https://packagist.org/packages/spiral/marshaller)[ Docs](https://github.com/spiral/marshaller)[ RSS](/packages/spiral-marshaller/feed)WikiDiscussions 1.x Synced 1mo ago

READMEChangelog (1)Dependencies (8)Versions (2)Used By (1)

Marshaller
==========

[](#marshaller)

[![PHP Version Require](https://camo.githubusercontent.com/fab145ca1aaedeba9a2384b26f9b259972277b256900a444033dfb10e16eedbe/68747470733a2f2f706f7365722e707567782e6f72672f73706972616c2f6d61727368616c6c65722f726571756972652f706870)](https://packagist.org/packages/spiral/marshaller)[![Latest Stable Version](https://camo.githubusercontent.com/687f9b5012a261ccc07ef498dc97a1dedbe784993a28ecd1c856573bc9e881ff/68747470733a2f2f706f7365722e707567782e6f72672f73706972616c2f6d61727368616c6c65722f762f737461626c65)](https://packagist.org/packages/spiral/marshaller)[![phpunit](https://github.com/spiral/marshaller/actions/workflows/phpunit.yml/badge.svg)](https://github.com/spiral/marshaller/actions)[![psalm](https://github.com/spiral/marshaller/actions/workflows/psalm.yml/badge.svg)](https://github.com/spiral/marshaller/actions)[![Codecov](https://camo.githubusercontent.com/42a5fa5ecd1e8b5a06993728f36f86411b3713a87f3e14481c64f490e7108907/68747470733a2f2f636f6465636f762e696f2f67682f73706972616c2f6d61727368616c6c65722f6272616e63682f312e782f67726170682f62616467652e737667)](https://codecov.io/gh/spiral/marshaller)[![Total Downloads](https://camo.githubusercontent.com/37947312d768ec597b2b8419a23161ac2aed51ecbe32501497c0a2f9d4554c48/68747470733a2f2f706f7365722e707567782e6f72672f73706972616c2f6d61727368616c6c65722f646f776e6c6f616473)](https://packagist.org/packages/spiral/marshaller)[![type-coverage](https://camo.githubusercontent.com/0dbb3c03f47ab21ba6a6d9104d297da812b3a55c175278ac5c0cc00b01e04f32/68747470733a2f2f73686570686572642e6465762f6769746875622f73706972616c2f6d61727368616c6c65722f636f7665726167652e737667)](https://shepherd.dev/github/spiral/marshaller)[![psalm-level](https://camo.githubusercontent.com/1fc400c4462d9062e15454348ff57fd42b10f73826debce3b84ace5ad7e36aa1/68747470733a2f2f73686570686572642e6465762f6769746875622f73706972616c2f6d61727368616c6c65722f6c6576656c2e737667)](https://shepherd.dev/github/spiral/marshaller)

Introduction
------------

[](#introduction)

The Marshaller package is a PHP tool that helps you convert PHP objects into simple arrays and vice versa. It allows you to marshal objects into array representations for serialization, storage, and transportation, and unmarshal arrays back into objects.

This package provides easy-to-use methods for converting objects to arrays and restoring objects from arrays. It supports handling nested objects and complex data structures.

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

[](#requirements)

Make sure that your server is configured with following PHP version and extensions:

- PHP 8.1+

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

[](#installation)

You can install the package via composer:

```
composer require spiral/marshaller
```

Usage
-----

[](#usage)

> **Note**Here, the low-level usage of the package will be covered. [Marshaller Bridge](https://github.com/spiral/marshaller-bridge) is a ready-made integration for the Spiral Framework.

### Converting objects to arrays

[](#converting-objects-to-arrays)

For example, we have a User class that has simple string properties, an Address property, a Status enum, a UUID, and an array of Address objects, and we need to serialize it:

#### User

[](#user)

```
namespace App\Entity;

use Ramsey\Uuid\UuidInterface;
use Spiral\Marshaller\Meta\Marshal;
use Spiral\Marshaller\Meta\MarshalArray;
use Spiral\Marshaller\Type\EnumType;

class User
{
    public function __construct(
        #[Marshal]
        private UuidInterface $uuid,

        #[Marshal(name: 'first_name')]
        private string $firstName,

        #[Marshal(name: 'last_name')]
        private string $lastName,

        #[Marshal(of: Status::class, type: EnumType::class)]
        private Status $status,

        #[Marshal(of: Address::class)]
        private Address $address,

        #[Marshal(name: 'registered_at', of: \DateTimeImmutable::class)]
        private \DateTimeImmutable $registeredAt,

        #[MarshalArray(name: 'delivery_addresses', of: Address::class)]
        private array $deliveryAddresses
    ) {
    }
}
```

Using attributes, we specify which private and protected properties to serialize and configure additional Marshaller parameters (for example, what type of array elements should be).

> **Note**Specifying the `Marshal` attribute is optional for public properties unless the property requires additional Marshaller configuration.

#### Address

[](#address)

```
namespace App\Entity;

use Spiral\Marshaller\Meta\Marshal;

final class Address
{
    public function __construct(
        #[Marshal]
        private string $street,

        #[Marshal]
        private string $city,

        #[Marshal]
        private string $country
    ) {
    }
}
```

#### Status

[](#status)

```
namespace App\Entity;

enum Status: string
{
    case Active = 'active';
    case Inactive = 'inactive';
}
```

To serialize a User object, we need to create a `Marshaller` object and call the **marshal** method and pass the User object into it.

```
use App\Entity\Address;
use App\Entity\Status;
use App\Entity\User;
use Ramsey\Uuid\Uuid;
use Spiral\Attributes\AttributeReader;
use Spiral\Marshaller\Mapper\AttributeMapperFactory;
use Spiral\Marshaller\Marshaller;

$address = new Address(
    street: 'Washington St.',
    city: 'San Francisco',
    country: 'USA',
);

$deliveryAddresses = [
    new Address(
        street: 'Street 1',
        city: 'New York',
        country: 'USA',
    ),
    new Address(
        street: 'Street 2',
        city: 'Chicago',
        country: 'USA',
    )
];

$user = new User(
    uuid: Uuid::uuid4(),
    firstName: 'John',
    lastName: 'Doe',
    status: Status::Active,
    address: $address,
    registeredAt: new \DateTimeImmutable(),
    deliveryAddresses: $deliveryAddresses,
);

$marshaller = new Marshaller(new AttributeMapperFactory(new AttributeReader()));
$data = $marshaller->marshal($user);

// result:
array(7) {
  'uuid' =>
  string(36) "e9aa20f8-8425-4020-af63-4a88725286c4"
  'first_name' =>
  string(4) "John"
  'last_name' =>
  string(3) "Doe"
  'status' =>
  array(2) {
    'name' =>
    string(6) "Active"
    'value' =>
    string(6) "active"
  }
  'address' =>
  array(3) {
    'street' =>
    string(14) "Washington St."
    'city' =>
    string(13) "San Francisco"
    'country' =>
    string(3) "USA"
  }
  'registered_at' =>
  string(25) "2023-07-16T13:57:35+00:00"
  'delivery_addresses' =>
  array(2) {
    [0] =>
    array(3) {
      'street' =>
      string(8) "Street 1"
      'city' =>
      string(8) "New York"
      'country' =>
      string(3) "USA"
    }
    [1] =>
    array(3) {
      'street' =>
      string(8) "Street 2"
      'city' =>
      string(7) "Chicago"
      'country' =>
      string(3) "USA"
    }
  }
}
```

### Converting arrays to objects

[](#converting-arrays-to-objects)

To unserialize an array, we need to create a `Marshaller` object and call the **unmarshal** method and pass the array with data and object to populate into it.

```
use Spiral\Attributes\AttributeReader;
use Spiral\Marshaller\Mapper\AttributeMapperFactory;
use Spiral\Marshaller\Marshaller;

$marshaller = new Marshaller(new AttributeMapperFactory(new AttributeReader()));

$data = [
    'uuid' => '4730d422-19ec-4da8-a3be-d42a774e0f2f',
    'first_name' => 'John',
    'last_name' => 'Doe',
    'status' => [
        'name' => 'Active',
        'value' => 'active'
    ],
    'address' => [
        'street' => 'Washington St.',
        'city' => 'San Francisco',
        'country' => 'USA',
    ],
    'registered_at' => '2023-07-16T13:23:04+00:00',
    'delivery_addresses' => [
        [
            'street' => 'Street 1',
            'city' => 'New York',
            'country' => 'USA',
        ],
        [
            'street' => 'Street 2',
            'city' => 'Chicago',
            'country' => 'USA',
        ]
    ],
];

$user = $marshaller->unmarshal(
    $data,
    (new \ReflectionClass(User::class))->newInstanceWithoutConstructor()
);

// result:
class App\Entity\User#453 (7) {
  private Ramsey\Uuid\UuidInterface $uuid =>
  class Ramsey\Uuid\Lazy\LazyUuidFromString#421 (2) {
    private ?Ramsey\Uuid\UuidInterface $unwrapped =>
    NULL
    private string $uuid =>
    string(36) "4730d422-19ec-4da8-a3be-d42a774e0f2f"
  }
  private string $firstName =>
  string(4) "John"
  private string $lastName =>
  string(3) "Doe"
  private App\Entity\Status $status =>
  enum App\Entity\Status::Active : string("active");
  private App\Entity\Address $address =>
  class App\Entity\Address#447 (3) {
    private string $street =>
    string(14) "Washington St."
    private string $city =>
    string(13) "San Francisco"
    private string $country =>
    string(3) "USA"
  }
  private DateTimeImmutable $registeredAt =>
  class DateTimeImmutable#412 (3) {
    public $date =>
    string(26) "2023-07-16 13:23:04.000000"
    public $timezone_type =>
    int(1)
    public $timezone =>
    string(6) "+00:00"
  }
  private array $deliveryAddresses =>
  array(2) {
    [0] =>
    class App\Entity\Address#392 (3) {
      private string $street =>
      string(8) "Street 1"
      private string $city =>
      string(8) "New York"
      private string $country =>
      string(3) "USA"
    }
    [1] =>
    class App\Entity\Address#443 (3) {
      private string $street =>
      string(8) "Street 2"
      private string $city =>
      string(7) "Chicago"
      private string $country =>
      string(3) "USA"
    }
  }
}
```

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.

License
-------

[](#license)

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

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 82.4% 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

1021d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/796136?v=4)[Anton Tsitou](/maintainers/wolfy-j)[@wolfy-j](https://github.com/wolfy-j)

---

Top Contributors

[![msmakouz](https://avatars.githubusercontent.com/u/67324318?v=4)](https://github.com/msmakouz "msmakouz (14 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (3 commits)")

---

Tags

serializermarshallerspiral

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/spiral-marshaller/health.svg)

```
[![Health](https://phpackages.com/badges/spiral-marshaller/health.svg)](https://phpackages.com/packages/spiral-marshaller)
```

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[temporal/sdk

Temporal SDK

4002.2M18](/packages/temporal-sdk)[goetas-webservices/xsd2php

Convert XSD (XML Schema) definitions into PHP classes and JMS metadata

2411.6M37](/packages/goetas-webservices-xsd2php)[goetas-webservices/xsd2php-runtime

Convert XSD (XML Schema) definitions into PHP classes

4910.9M36](/packages/goetas-webservices-xsd2php-runtime)[flix-tech/avro-serde-php

A library to serialize and deserialize Avro records making use of the confluent schema registry

674.0M17](/packages/flix-tech-avro-serde-php)[laminas/laminas-serializer

Serialize and deserialize PHP structures to a variety of representations

3411.2M115](/packages/laminas-laminas-serializer)

PHPackages © 2026

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