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.
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!
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"
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 thesymfony/yaml
package and is disabled when the package is not installed. Install thesymfony/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;
}
}
<?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);
}
}
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);
}
}
This project has a security policy.
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
You are more than welcome. Before contributing, kindly check our contribution guidelines.
- Twitter: Follow our organization @wayofdev and the author @wlotyp.
- Discord: Join our community on Discord.
This repository is inspired by the following projects: