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

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

youcanshop/cereal
=================

A package to simplify serialization management.

v1.1.2(2y ago)311.2k↓38.6%[1 PRs](https://github.com/youcan-shop/cereal/pulls)MITPHPPHP &gt;=7.4

Since Sep 14Pushed 2y ago1 watchersCompare

[ Source](https://github.com/youcan-shop/cereal)[ Packagist](https://packagist.org/packages/youcanshop/cereal)[ RSS](/packages/youcanshop-cereal/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (4)Versions (7)Used By (0)

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

[![Tests](https://github.com/youcan-shop/Cereal/actions/workflows/tests.yml/badge.svg)](https://github.com/NextmediaMa/Cereal/actions/workflows/tests.yaml)[![Total Downloads](https://camo.githubusercontent.com/2bfaa708b36dd2ac2da3c8bc0a5125e80cfbe9321d45f65f43d1fcc74458533c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f796f7563616e73686f702f63657265616c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/youcanshop/cereal)[![License](https://camo.githubusercontent.com/2ba32988341e1aba762cc19f9677c52fa8ce2a4b6688de32eba705bce0293daf/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f796f7563616e2d73686f702f43657265616c3f7374796c653d666c61742d737175617265)](https://github.com/NextmediaMa/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 youcanshop/cereal
```

Usage
-----

[](#usage)

To start using the package, you need to create a class that implements the `YouCanShop\Cereal\Contracts\Serializable` interface, and use the `YouCanShop\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 `YouCanShop\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 `YouCan\Cereal\SerializationHandlerFactory`:

```
use YouCan\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

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 82.6% 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 ~2 days

Total

5

Last Release

970d ago

### Community

Maintainers

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

---

Top Contributors

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

###  Code Quality

TestsPest

Static AnalysisPHPStan, Rector

Type Coverage Yes

### Embed Badge

![Health badge](/badges/youcanshop-cereal/health.svg)

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

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)[hassankhan/config

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

97513.5M170](/packages/hassankhan-config)[meyfa/php-svg

Read, edit, write, and render SVG files with PHP

54613.9M42](/packages/meyfa-php-svg)

PHPackages © 2026

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