Skip to content

🔧 Laravel + Symfony Serializer. This package provides a bridge between Laravel and Symfony Serializer.

License

Notifications You must be signed in to change notification settings

wayofdev/laravel-symfony-serializer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


WayOfDev Logo

Build
Build Status

Project
Total Downloads Latest Stable Version Commits since latest release PHP Version Require

Quality
Codecov Mutation testing badge PHP Stan Level 6 of 9

Community
Discord Follow on Twitter (X)


Laravel Symfony Serializer

📄 About

This package integrates the Symfony Serializer component into Laravel, providing a powerful tool for serializing and deserializing objects into various formats such as JSON, XML, CSV, and YAML.

Detailed documentation on the Symfony serializer can be found on their official page.

→ Purpose

This package brings the power of the Symfony Serializer component to Laravel. While Laravel does not have a built-in serializer and typically relies on array or JSON transformations, this package provides more advanced serialization capabilities. This includes object normalization, handling of circular references, property grouping, and format-specific encoders.

If you are building a REST API, working with queues, or have complex serialization needs, this package will be especially useful. It allows you to use objects as payload instead of simple arrays, and supports various formats such as JSON, XML, CSV, and YAML. This documentation will guide you through the installation process and provide examples of how to use the package to serialize and deserialize your objects.


🙏 If you find this repository useful, please consider giving it a ⭐️. Thank you!


💿 Installation

Require as dependency:

composer req wayofdev/laravel-symfony-serializer

You can publish the config file with:

$ php artisan vendor:publish \
  --provider="WayOfDev\Serializer\Bridge\Laravel\Providers\SerializerServiceProvider" \
  --tag="config"

💻 Usage

The package provides a list of serializers that can be used to serialize and deserialize objects.

The serializers available in this package are: symfony-json, symfony-csv, symfony-xml, symfony-yaml.

Warning The yaml encoder requires the symfony/yaml package and is disabled when the package is not installed. Install the symfony/yaml package and the encoder will be automatically enabled.

We will use this example DTO for serialization purposes:

<?php

namespace Application\DTO;

use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\SerializedName;

class MyDTO
{
    #[Groups(['public'])]
    #[SerializedName('id')]
    private int $id;

    #[Groups(['public'])]
    #[SerializedName('name')]
    private string $name;

    #[Groups(['private', 'public'])]
    #[SerializedName('email')]
    private string $email;

    public function __construct(int $id, string $name, string $email)
    {
        $this->id = $id;
        $this->name = $name;
        $this->email = $email;
    }

    public function id(): int
    {
        return $this->id;
    }

    public function name(): string
    {
        return $this->name;
    }

    public function email(): string
    {
        return $this->email;
    }
}

→ Using SerializerManager in your Service Classes

<?php

namespace Application\Services;

use WayOfDev\Serializer\SerializerManager;
use Application\DTO\MyDTO;

class MyService
{
    public function __construct(
        private readonly SerializerManager $serializer,
    ) {
    }

    public function someMethod(): void
    {
        $serializer = $serializer->getSerializer('json');
        $dto = new MyDTO(1, 'John Doe', 'john@example.com');

        $content = $serializer->normalize(
            data: $dto,
            context: ['groups' => ['private']]
        );

        $serialized = $serializer->serialize($content);
    }
}

→ Using ResponseFactory in Laravel Controllers

Here's an example of how you can use the ResponseFactory in a Laravel controller:

Example Controller:

<?php

namespace Laravel\Http\Controllers;

use Application\DTO\MyDTO;
use Illuminate\Http\Request;
use WayOfDev\Serializer\HttpCode;
use WayOfDev\Serializer\ResponseFactory;

class MyController extends Controller
{
    private ResponseFactory $response;

    public function __construct(ResponseFactory $response)
    {
        $this->response = $response;
    }

    public function index()
    {
        $dto = new MyDTO(1, 'John Doe', 'john@example.com');

        $this->response->withContext(['groups' => ['private']]);
        $this->response->withStatusCode(HttpCode::HTTP_OK);
      
        return $this->response->create($dto);
    }
}

🔒 Security Policy

This project has a security policy.


🙌 Want to Contribute?

Thank you for considering contributing to the wayofdev community! We are open to all kinds of contributions. If you want to:

You are more than welcome. Before contributing, kindly check our contribution guidelines.

Conventional Commits


🫡 Contributors

Contributors Badge

🌐 Social Links


⚖️ License

Licence


🧱 Credits and Useful Resources

This repository is inspired by the following projects: