wayofdev/laravel-symfony-serializer

Laravel wrapper around Symfony Serializer.

v1.2.60 2023-09-14 19:24 UTC

README


logo.gh-light-mode-only.png#gh-light-mode-only logo.gh-dark-mode-only.png#gh-dark-mode-only

Build Status Total Downloads Latest Stable Version Software License Commits since latest release

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);
    }
}

🧪 Running Tests

→ PHPUnit tests

To run phpunit and pest tests, run the following command:

$ make test

→ Static Analysis

Code quality using PHPStan:

$ make lint-stan

→ Coding Standards Fixing

Fix code using The PHP Coding Standards Fixer (PHP CS Fixer) to follow our standards:

$ make lint-php

🤝 License

Licence


🙆🏼‍♂️ Author Information

Created in 2023 by lotyp / wayofdev


🙌 Want to Contribute?

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

  • 🤔 Suggest a feature
  • 🐛 Report an issue
  • 📖 Improve documentation
  • 👨‍💻 Contribute to the code

🧱 Credits and Useful Resources

This repository is based on code from following repositories: