PHPackages                             done-super-app/cereal - 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. done-super-app/cereal

ActiveLibrary

done-super-app/cereal
=====================

A package to simplify serialization management.

v1.0.0(1y ago)010.6k↓10.3%MITPHPPHP &gt;=7.4CI passing

Since Jan 18Pushed 1y ago1 watchersCompare

[ Source](https://github.com/DoneSuperApp/cereal)[ Packagist](https://packagist.org/packages/done-super-app/cereal)[ RSS](/packages/done-super-app-cereal/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (2)Used By (0)

[![Cereal package logo](https://github.com/DoneSuperApp/cereal/raw/main/assets/cereallogo.svg)](https://github.com/DoneSuperApp/cereal/blob/main/assets/cereallogo.svg)
**Cereal**

[![Tests](https://github.com/DoneSuperApp/Cereal/actions/workflows/tests.yml/badge.svg)](https://github.com/DoneSuperApp/Cereal/actions/workflows/tests.yaml)[![Total Downloads](https://camo.githubusercontent.com/5eff047cfc74b6f084cfc8f5c64e5c6b04dda2ae56fde2bee4f1b266bd70990e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f446f6e6553757065724170702f63657265616c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/DoneSuperApp/cereal)[![License](https://camo.githubusercontent.com/590790906e3a4a13218da8b217fbe215334263596eef2c7a1dd94d0088726ec8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f446f6e6553757065724170702f43657265616c3f7374796c653d666c61742d737175617265)](https://github.com/DoneSuperApp/Cereal/blob/master/LICENSE.md)

Cereal is a PHP package that allows you to easily serialize and deserialize your data.

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

[](#installation)

You can install the package via composer:

```
composer require done-super-app/cereal
```

Usage
-----

[](#usage)

To start using the package, you need to create a class that implements the `DoneSuperApp\Cereal\Contracts\Serializable` interface, and use the `DoneSuperApp\Cereal\Cereal` trait which contains some helpers for a default configuration.

```
class User implements Serializable
{
    use Cereal;

    public int $age;
    public string $name;
    public float $weight;
    public bool $isStraight;

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

    public function serializes(): array
    {
        return ['name', 'age', 'weight', 'isStraight'];
    }
}
```

The `serializes` method returns an array of the properties that you want to serialize. Then the package will automatically serialize and deserialize the data based on the type hinted properties.

> **Note**
>
> - The package will only serialize public properties that are type hinted.
> - The package will serialize properties in the order they are defined in the `serialize` method.

### Serialization Handlers

[](#serialization-handlers)

In most uses cases, you will need to serialize and deserialize some properties in a different way. For example, you may want to serialize a `DateTime` object to a timestamp, or serialize a `User` object to an array or something. For that, you're going to create your own serialization handler and add it to the `DoneSuperApp\Cereal\SerializationHandlerFactory`.

```
class UserHandler implements SerializationHandler {

    protected UserRepository $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function serialize(Serializable $serializable, $value): string
    {
        return $value->getId();
    }

    /**
     * @param string $value
     *
     * @return ?User
     */
    public function deserialize(Serializable $serializable, $value): ?User
    {
        return $this->userRepository->find($value);
    }
}
```

In the above example, when trying to serialize a user object, we serialize only the ID, and when trying to deserialize it, we fetch it again from database.

Then, you need to add your handler to the `DoneSuperApp\Cereal\SerializationHandlerFactory`:

```
use DoneSuperApp\Cereal\SerializationHandlerFactory;

SerializationHandlerFactory::getInstance()
    ->addHandler(User::class, new UserHandler($userRepository));
```

Now, when working with our object that will be serialized, and if it contains a `User` instance, it will automatically be serialized and deserialized using our handler.

```
class Post implements Serializable
{
    use SerializeTrait;

    public string $title;
    public string $content;

    public User $author;

    public function __construct(string $title, string $content, User $author)
    {
        $this->title = $title;
        $this->content = $content;
        $this->author = $author;
    }

    public function serializes(): array
    {
        return ['title', 'content', 'author'];
    }
}
```

### Laravel Bridge

[](#laravel-bridge)

// TODO

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance41

Moderate activity, may be stable

Popularity23

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 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

Unknown

Total

1

Last Release

485d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4e77e863a88494fe41ab35f0d3fa40d08172040ea801aa558672af608da4e1e1?d=identicon)[Whyounes](/maintainers/Whyounes)

---

Top Contributors

[![Whyounes](https://avatars.githubusercontent.com/u/1175548?v=4)](https://github.com/Whyounes "Whyounes (2 commits)")

###  Code Quality

TestsPest

Static AnalysisPHPStan, Rector

Type Coverage Yes

### Embed Badge

![Health badge](/badges/done-super-app-cereal/health.svg)

```
[![Health](https://phpackages.com/badges/done-super-app-cereal/health.svg)](https://phpackages.com/packages/done-super-app-cereal)
```

PHPackages © 2026

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