PHPackages                             digital-craftsman/self-aware-normalizers - 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. digital-craftsman/self-aware-normalizers

ActiveSymfony-bundle[Utility &amp; Helpers](/categories/utility)

digital-craftsman/self-aware-normalizers
========================================

Adds interfaces and normalizers that allow value objects to normalize and denormalize themselves.

1.3.0(3mo ago)15.1k↓15.9%6MITPHPPHP 8.4.\*|8.5.\*CI passing

Since Dec 3Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/digital-craftsman-de/self-aware-normalizers)[ Packagist](https://packagist.org/packages/digital-craftsman/self-aware-normalizers)[ RSS](/packages/digital-craftsman-self-aware-normalizers/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (11)Versions (10)Used By (6)

Enable value objects to normalize and denormalize themselves
============================================================

[](#enable-value-objects-to-normalize-and-denormalize-themselves)

A Symfony bundle to enable value objects and DTOs to normalize and denormalize themselves through implementing simple interfaces that normalize to scalar values and denormalize themselves from scalar values (`string`, `int`, `float`, `bool` and `array`). Adding this kind of logic to the classes themselves might be considered a bad practice, but depending on the use case it will actually be better due to the fact that the data structure and the normalization need to be changed together.

The name implies that the value objects and DTOs are self-aware in the sense that they know how to normalize and denormalize themselves and that they are self-aware enough to do so 🙂

As it's a central part of an application, it's tested thoroughly (including mutation testing).

[![Latest Stable Version](https://camo.githubusercontent.com/cf1edcbc08969dbbb5554eefcf75fcef05061fe10d45d5a13a286a575d1599ee/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f737461626c652d312e332e302d626c7565)](https://packagist.org/packages/digital-craftsman/self-aware-normalizers)[![PHP Version Require](https://camo.githubusercontent.com/2f2005e31dc46e3c057be679d712cd9c4486aafcd26810f33ab48992af8d4971/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d382e34253743382e352d356235643935)](https://packagist.org/packages/digital-craftsman/self-aware-normalizers)[![codecov](https://camo.githubusercontent.com/825510e028ddf609e32d834676eae638adabd74bf7f857cc155a398900a9923c/68747470733a2f2f636f6465636f762e696f2f67682f6469676974616c2d6372616674736d616e2d64652f73656c662d61776172652d6e6f726d616c697a6572732f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d424c304a4b5a594c4247)](https://codecov.io/gh/digital-craftsman-de/self-aware-normalizers)[![Packagist Downloads](https://camo.githubusercontent.com/6f651ec9b7230ad4ffba7b78130d64cb513a59c4995b6cd9970c17d02742afe3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6469676974616c2d6372616674736d616e2f73656c662d61776172652d6e6f726d616c697a657273)](https://camo.githubusercontent.com/6f651ec9b7230ad4ffba7b78130d64cb513a59c4995b6cd9970c17d02742afe3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6469676974616c2d6372616674736d616e2f73656c662d61776172652d6e6f726d616c697a657273)[![Packagist License](https://camo.githubusercontent.com/6d35372306a131306b8f84eeade456a4361f82a86164f00a07d12f473496e864/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6469676974616c2d6372616674736d616e2f73656c662d61776172652d6e6f726d616c697a657273)](https://camo.githubusercontent.com/6d35372306a131306b8f84eeade456a4361f82a86164f00a07d12f473496e864/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6469676974616c2d6372616674736d616e2f73656c662d61776172652d6e6f726d616c697a657273)

Installation and configuration
------------------------------

[](#installation-and-configuration)

Install package through composer:

```
composer require digital-craftsman/self-aware-normalizers
```

Optionally, you can add a `self-aware-normalizers.php` file to your `config/packages`. The options are described below.

Usage
-----

[](#usage)

### Normalizers

[](#normalizers)

To make the normalization process easier, there are the following normalizers included:

- `StringNormalizableNormalizer`
- `IntNormalizableNormalizer`
- `FloatNormalizableNormalizer`
- `BoolNormalizableNormalizer`
- `ArrayNormalizableNormalizer`

Additionally, there is an interface for each of the normalizers. Every class that implements one of the interfaces, will be automatically normalized to the respected type. This means putting the logic of how serialization of a class works within the class. That's not really seen as a good practice. In my experience, the data structure and the normalization need to be changed together. So, I like it better to have both in one place. I've used this approach in multiple large scale projects for years and haven't had a single issue with it yet. But your mileage may vary.

With this you can have nested denormalization that looks like this:

```
/**
 * @psalm-type NormalizedSearch = array{
 *     searchTerm: string,
 *     limit: int,
 * }
 */
final readonly class Search implements ArrayNormalizable
{
    public function __construct(
        public SearchTerm $searchTerm,
        public Limit $limit,
    ) {
    }

    /**
    * @param NormalizedSearch $data
    */
    public static function denormalize(array $data): self
    {
        return new self(
            searchTerm: SearchTerm::denormalize($data['searchTerm']),
            limit: Limit::denormalize($data['limit']),
        );
    }

    /**
    * @return NormalizedSearch
    */
    public function normalize(): array
    {
        return [
            'searchTerm' => $this->searchTerm->normalize(),
            'limit' => $this->limit->normalize(),
        ];
    }
}
```

#### Denormalized for null handling

[](#denormalized-for-null-handling)

When handling `null` you can use the `Nullable*Denormalizable` interfaces with the related `Nullable*DenormalizableTrait` to handle switches between `null` and the class like the following:

```
/**
 * @psalm-type NormalizedSearchWithOptionalLimit = array{
 *     searchTerm: string,
 *     limit: int | null,
 * }
 */
final readonly class SearchWithOptionalLimit implements ArrayNormalizable
{
    public function __construct(
        public SearchTerm $searchTerm,
        public ?Limit $limit,
    ) {
    }

    /**
    * @param NormalizedSearchWithOptionalLimit $data
    */
    public static function denormalize(array $data): self
    {
        return new self(
            searchTerm: SearchTerm::denormalize($data['searchTerm']),
            limit: Limit::denormalizeWhenNotNull($data['limit']),
        );
    }

    /**
    * @return NormalizedSearchWithOptionalLimit
    */
    public function normalize(): array
    {
        return [
            'searchTerm' => $this->searchTerm->normalize(),
            'limit' => $this->limit?->normalize(),
        ];
    }
}
```

Internally the value object simply has to implement the relevant interface and use the related trait like the following:

```
final readonly class Limit implements IntNormalizable, NullableIntDenormalizable
{
    use NullableIntDenormalizableTrait;

    public function __construct(
        public int $limit,
        ...
```

### Doctrine types

[](#doctrine-types)

When using the normalizers, you can also use the same logic for doctrine types.

#### Automatic doctrine types

[](#automatic-doctrine-types)

You don't even need to define custom doctrine types. All you need to do is to add the directory your implementing classes are into the `config/packages/self-aware-normalizers.php` configuration:

```
