PHPackages                             gilsegura/serializer - 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. gilsegura/serializer

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

gilsegura/serializer
====================

A minimal, framework-agnostic serialization contract for PHP 8.5+.

1.1.0(3w ago)01.5k4MITPHPPHP ^8.4CI passing

Since Dec 5Pushed 3w ago1 watchersCompare

[ Source](https://github.com/gilsegura/serializer)[ Packagist](https://packagist.org/packages/gilsegura/serializer)[ RSS](/packages/gilsegura-serializer/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (5)Versions (7)Used By (4)

SERIALIZER
==========

[](#serializer)

[![tests](https://github.com/gilsegura/serializer/actions/workflows/tests.yaml/badge.svg)](https://github.com/gilsegura/serializer/actions/workflows/tests.yaml)[![codecov](https://camo.githubusercontent.com/11c1ca8401bea46d4cff6fdfd2f766278c31a00fcecbfc2597a878616e206e6d/68747470733a2f2f636f6465636f762e696f2f6769746875622f67696c7365677572612f73657269616c697a65722f67726170682f62616467652e737667)](https://codecov.io/github/gilsegura/serializer)[![static analysis](https://github.com/gilsegura/serializer/actions/workflows/static-analysis.yaml/badge.svg)](https://github.com/gilsegura/serializer/actions/workflows/static-analysis.yaml)[![coding standards](https://github.com/gilsegura/serializer/actions/workflows/coding-standards.yaml/badge.svg)](https://github.com/gilsegura/serializer/actions/workflows/coding-standards.yaml)

A minimal, framework-agnostic serialization contract for PHP 8.4+.

`gilsegura/serializer` defines a single generic contract, `SerializableInterface`, that objects implement to convert themselves to and from a plain array, plus a `Serializer` facade that wraps an object together with its class name so it can be restored later as its exact concrete type.

Features
--------

[](#features)

- PHP 8.4+
- A generic `SerializableInterface` contract: `serialize()` / `deserialize()`
- Per-implementation attribute shapes via `@implements`, fully understood by static analysis
- A `Serializer` facade producing a self-describing `{class, attributes}` structure
- No dependencies beyond PHP itself

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

[](#installation)

```
composer require gilsegura/serializer
```

The contract
------------

[](#the-contract)

`SerializableInterface` is generic over the shape of its attributes, `TAttributes`. Each implementation declares its own concrete shape with an `@implements` tag, so static analysis knows the exact type of every attribute on both sides of the round trip:

```
use Serializer\SerializableInterface;

/**
 * @implements SerializableInterface
 */
final readonly class Point implements SerializableInterface
{
    public function __construct(
        public int $x,
        public int $y,
    ) {
    }

    public static function deserialize(array $attributes): static
    {
        return new self($attributes['x'], $attributes['y']);
    }

    public function serialize(): array
    {
        return ['x' => $this->x, 'y' => $this->y];
    }
}
```

Because the shape is declared in `@implements`, `deserialize()` reads `$attributes['x']` as a typed `int` with no casts or assertions: the analyser already knows it is an `int`.

`TAttributes` is constrained to `array`, so an implementation may serialize to a map (an associative object) or to a list:

```
/**
 * @implements SerializableInterface
 */
final readonly class NumberCollection implements SerializableInterface
{
    /** @var int[] */
    public array $numbers;

    public function __construct(int ...$numbers)
    {
        $this->numbers = $numbers;
    }

    public static function deserialize(array $attributes): static
    {
        return new self(...$attributes);
    }

    public function serialize(): array
    {
        return $this->numbers;
    }
}
```

The Serializer facade
---------------------

[](#the-serializer-facade)

`Serializer` wraps an object with its class name, producing a self-describing structure that can be stored or transported and later restored to the exact same type:

```
use Serializer\Serializer;

$serialized = Serializer::serialize(new Point(1, 2));
// ['class' => Point::class, 'attributes' => ['x' => 1, 'y' => 2]]

$point = Serializer::deserialize($serialized);
// Point(1, 2)
```

Because the class name travels with the data, `deserialize()` returns the original concrete type rather than a generic interface. This makes the format portable across process boundaries (queues, caches, storage) while staying type-safe.

Composing serializable objects
------------------------------

[](#composing-serializable-objects)

An object's `serialize()` may delegate to nested serializable objects, building up a tree. Each level declares its own shape, so the whole structure stays typed end to end. This makes `SerializableInterface` a good fit for value objects, domain events, and read models that need a stable, self-describing wire format.

License
-------

[](#license)

MIT. See [LICENSE](LICENSE).

###  Health Score

49

—

FairBetter than 94% of packages

Maintenance95

Actively maintained with recent releases

Popularity20

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity61

Established project with proven stability

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

Recently: every ~0 days

Total

6

Last Release

22d ago

Major Versions

0.1.0 → 1.0.02026-06-11

PHP version history (2 changes)0.1.0PHP &gt;=8.3

1.0.0PHP ^8.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/29969702?v=4)[josué](/maintainers/gilsegura)[@gilsegura](https://github.com/gilsegura)

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/gilsegura-serializer/health.svg)

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

###  Alternatives

[mck89/peast

Peast is PHP library that generates AST for JavaScript code

19139.2M47](/packages/mck89-peast)[sauladam/shipment-tracker

Parses tracking information for several carriers, like UPS, USPS, DHL and GLS by simply scraping the data. No need for any kind of API access.

9843.5k](/packages/sauladam-shipment-tracker)[jstewmc/rtf

Read and write Rich Text Format (RTF) documents with PHP

45153.1k6](/packages/jstewmc-rtf)[tcds-io/php-jackson

A lightweight, flexible object serializer for PHP, inspired by FasterXML/jackson

113.2k10](/packages/tcds-io-php-jackson)

PHPackages © 2026

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