PHPackages                             earc/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. earc/serializer

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

earc/serializer
===============

eArc - the explicit architecture framework - serializer component

1.1(5y ago)011MITPHPPHP ^8.0 || ^7.4

Since Aug 5Pushed 5y ago1 watchersCompare

[ Source](https://github.com/Koudela/eArc-serializer)[ Packagist](https://packagist.org/packages/earc/serializer)[ RSS](/packages/earc-serializer/feed)WikiDiscussions master Synced 1mo ago

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

earc/serializer
===============

[](#earcserializer)

Standalone lightweight extendable serializer component of the eArc libraries.

Although it's a small library, it is very powerful. With just a few lines of code you can use it to import entities from csv or map them to a bunch of sql-tables.

table of contents
-----------------

[](#table-of-contents)

- [installation](#installation)
- [basic usage](#basic-usage)
- [advanced usage](#advanced-usage)
    - [filtering properties](#filtering-properties)
    - [customizing serialization via specialized data types](#customizing-serialization-via-specialized-data-types)
    - [data mapping](#data-mapping)
- [releases](#releases)
    - [release v1.1](#release-v11)
    - [release v1.0](#release-v10)
    - [release v0.1](#release-v01)
    - [release v0.0](#release-v00)

installation
------------

[](#installation)

Install the earc serializer library via composer.

```
$ composer require earc/serializer

```

basic usage
-----------

[](#basic-usage)

First enable the [earc/di](https://github.com/Koudela/eArc-di/) dependency injection component.

```
use eArc\DI\DI;

DI::init();
```

Now your ready to serialize some content.

```
use eArc\Serializer\Api\Serializer;

$serializedValue = di_get(Serializer::class)->serialize($value);
$deserializedValue = di_get(Serializer::class)->deserialize($serializedValue);
```

`$value` is the same as `$deserializedValue`.

The data types `DateTime`, `object`, `array`, `string`, `int`,`float`, `bool`and `null` values are supported by the default serialization type out of the box.

Even serialization of recursive object structures are possible.

```
use eArc\Serializer\Api\Serializer;

class X {
    /** @var Y */
    protected $referenceToY;

    public function __construct()
    {
        $this->referenceToY = new Y($this);
    }
}

class Y {
    /** @var X */
    protected $referenceToX;

    public function __construct(X $x)
    {
        $this->referenceToX = $x;
    }
}

$serializedValue = di_get(Serializer::class)->serialize(new X());

echo $serializedValue;
```

This outputs:

`{"type":"X","value":{"content":{"referenceToY":{"type":"Y","value":{"content":{"referenceToX":{"type":"X","value":{"content":null,"id":57}}},"id":60}}},"id":57}}`

```
$object = di_get(Serializer::class)->deserialize($serializedValue);
```

`$object` yields an instance of `X` referencing an instance of `Y` having a reference on `X`. Of course, it is not the same instance as serialized. It's an identical instance like you would receive on cloning the object. If multiple references point to the same instance, this holds also true after deserialization. You can enforce same instances by wrapping multiple objects into an array.

```
use eArc\Serializer\Api\Serializer;

class Z1 {}
class Z2 {
    public $z1;
}

$classZ1 = new Z1();
$classZ2 = new Z2();
$classZ2->z1 = $classZ1;

$serializedValue = di_get(Serializer::class)->serialize([0 => $classZ1, 1 => $classZ2]);
```

The deserialized value would also hold two references to the same instance of the class `Z1`.

advanced usage
--------------

[](#advanced-usage)

The basic usage of the earc/serializer is not superior to the native php `serialize/unserialize` function. You should use the earc/serializer only if you need the advanced features.

### filtering properties

[](#filtering-properties)

The earc/serializer offers an easy way to organize the filtering of properties. Extend the `SerializerDefaultType` and overwrite the `filterProperty()` method. Only if this method returns true the property will be serialized.

```
use eArc\Serializer\SerializerTypes\SerializerDefaultType;

class MySerializerType extends SerializerDefaultType
{
    public function filterProperty(string $fQCN, string $propertyName): bool
    {
        static $blacklistProperties = [
            MyClass::class => [
                'myPropertyOne' => true,
                'anotherNotSerializedProperty' => true,
            ],
        ];

        return !array_key_exists($fQCN, $blacklistProperties)
            && !array_key_exists($propertyName, $blacklistProperties[$fQCN]);
    }
}
```

To use this Filter use an object of the extending class as second argument.

```
use eArc\Serializer\Api\Serializer;

$serializerType = di_get(MySerializerType::class);

$serializedValue = di_get(Serializer::class)->serialize($value, $serializerType);
$deserializedValue = di_get(Serializer::class)->deserialize($serializedValue, $serializerType);
```

### customizing serialization via specialized data types

[](#customizing-serialization-via-specialized-data-types)

There are use cases where you need full control over the serialization process. For example in the case of further processing the serialized value or if your object/data needs a special processing prior serialization or post deserialization. Specialized data types offer you this level of control while keeping your architecture clean.

To create your own data type implement the `DataTypeInterface`.

```
use eArc\Serializer\DataTypes\Interfaces\DataTypeInterface;

class MyDataType implements DataTypeInterface
{
    public function isResponsibleForSerialization(?object $object, $propertyName, $propertyValue): bool
    {
        return is_subclass_of($propertyValue, MyObjectsClassOrInterface::class);
    }

    public function serialize(?object $object, $propertyName, $propertyValue)
    {
        return [
            'type' => MyObjectsClassOrInterface::class,
            'value' => 'Put your serialized Information here.',
        ];
    }
    public function isResponsibleForDeserialization(?object $object, string $type, $value): bool
    {
        return $type === MyObjectsClassOrInterface::class;
    }

    public function deserialize(?object $object, string $type, $value)
    {
        return 'Here you return your deserialized class instance';
    }
}
```

Hint 1: `serialize()` can return anything that can be processed by the native php function `json_encode()`. If it is not an array or does not have the `type` and `value` keys the `$type` parameter will be an empty string and the `$value`parameter will hold the serialized value.

Hint 2: You can use the `FactoryService`, `SerializeService` or the `ObjectHashService`for specialized tasks.

To use the new data type you need a serializer type referencing the new data type. Either implement the `SerializerTypeInterface` or extend the `SerializerDefaultType`and overwrite the `getDataTypes()` method.

```
use eArc\Serializer\SerializerTypes\SerializerDefaultType;

class MySerializerType extends SerializerDefaultType
{
    public function getDataTypes(): iterable
    {
        yield MyDataType::class => null;

        foreach (parent::getDataTypes() as $class => $object) {
            yield $class => $object;
        }
    }
}
```

Note the order. If multiple data types are applicable, the data type placed first is applied.

Hint: If you need to build the `MyDataType` object by another dependency injection system than earc/di you can pass the object instead of `null`.

To use the new serializer type instead of the default pass its object as second argument to the serializer methods.

```
use eArc\Serializer\Api\Serializer;

$serializerType = di_get(MySerializerType::class);

$serializedValue = di_get(Serializer::class)->serialize($value, $serializerType);
$deserializedValue = di_get(Serializer::class)->deserialize($serializedValue, $serializerType);
```

Hint: You can use your own dependency injection function (or container) to build the serializer type object.

### data mapping

[](#data-mapping)

You can use earc/serializer to do all kinds of data mapping, even if tables are involved. This includes mapping (nested) objects to csv or sql and back. Check the examples' folder to get an impression.

releases
--------

[](#releases)

### release v1.1

[](#release-v11)

- PHP ^8.0 || ^7.4

### release v1.0

[](#release-v10)

- data types can be prioritized
- serializer types passed as parameter instead of tagged data types
- filter (whitelist or blacklist) properties

### release v0.1

[](#release-v01)

- PHP 7.4
- additional supported data types:
    - DateTime
- serialization of private properties of parents

### release v0.0

[](#release-v00)

first official release

- supported data types:
    - object (class instance)
    - object (StdClass)
    - array
    - string
    - float
    - int
    - bool
    - `null`
- customized data types
- support for serialization of recursive object relations
- easy readable serialized output
- serializing structures:
    - json

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

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

Total

4

Last Release

1886d ago

Major Versions

0.1 → 1.02021-01-01

PHP version history (3 changes)0.0PHP ^7.2

0.1PHP ^7.4

1.1PHP ^8.0 || ^7.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/b7308f2797252cace014cfec12c1b5978c1bf5608be78d7a188ff690192959f3?d=identicon)[Thomas Koudela](/maintainers/Thomas%20Koudela)

---

Top Contributors

[![Koudela](https://avatars.githubusercontent.com/u/21366492?v=4)](https://github.com/Koudela "Koudela (10 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[lee-to/laravel-seo-by-url

Easy seo for Laravel and MoonShine

239.4k](/packages/lee-to-laravel-seo-by-url)

PHPackages © 2026

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