wayofdev / laravel-symfony-serializer
Laravel wrapper around Symfony Serializer.
Fund package maintenance!
wayofdev
Installs: 4 717
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 1
Forks: 1
Open Issues: 11
Requires
- php: ^8.2
- doctrine/annotations: ^2.0
- illuminate/http: ^10.15
- symfony/property-access: ^6.3
- symfony/serializer: ^6.3
Requires (Dev)
- ergebnis/composer-normalize: ^2.31
- nunomaduro/larastan: ^2.6
- orchestra/testbench: ^8.5
- pestphp/pest: ^2.6
- pestphp/pest-plugin-laravel: ^2.0.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/phpunit: ^10.2
- ramsey/uuid: ^4.7
- roave/security-advisories: dev-latest
- symfony/yaml: ^6.3
- wayofdev/cs-fixer-config: ^1.2
Suggests
- symfony/yaml: For using the YamlEncoder.
- dev-master
- v1.2.60
- v1.2.59
- v1.2.58
- v1.2.57
- v1.2.56
- v1.2.55
- v1.2.54
- v1.2.53
- v1.2.52
- v1.2.51
- v1.2.50
- v1.2.49
- v1.2.48
- v1.2.47
- v1.2.46
- v1.2.45
- v1.2.44
- v1.2.43
- v1.2.42
- v1.2.41
- v1.2.40
- v1.2.39
- v1.2.38
- v1.2.37
- v1.2.36
- v1.2.35
- v1.2.34
- v1.2.33
- v1.2.32
- v1.2.31
- v1.2.30
- v1.2.29
- v1.2.28
- v1.2.27
- v1.2.26
- v1.2.25
- v1.2.24
- v1.2.23
- v1.2.22
- v1.2.21
- v1.2.20
- v1.2.19
- v1.2.18
- v1.2.17
- v1.2.16
- v1.2.15
- v1.2.14
- v1.2.13
- v1.2.12
- v1.2.11
- v1.2.10
- v1.2.9
- v1.2.8
- v1.2.7
- v1.2.6
- v1.2.5
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.25
- v1.1.24
- v1.1.23
- v1.1.22
- v1.1.21
- v1.1.20
- v1.1.19
- v1.1.18
- v1.1.17
- v1.1.16
- v1.1.15
- v1.1.14
- v1.1.13
- v1.1.12
- v1.1.11
- v1.1.10
- v1.1.9
- v1.1.8
- v1.1.7
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-renovate/orchestra-testbench-9.x
- dev-renovate/major-illuminate
- dev-renovate/phpunit-phpunit-11.x
- dev-renovate/actions-cache-4.x
- dev-renovate/ergebnis-composer-normalize-2.x-lockfile
- dev-renovate/phpstan-packages
- dev-renovate/symfony
- dev-renovate/major-symfony
- dev-renovate/pestphp-pest-2.x-lockfile
This package is auto-updated.
Last update: 2024-04-13 10:14:11 UTC
README
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 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; } }
→ 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
🙆🏼♂️ 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: