visual-craft / deserializer-bundle
Symfony Bundle for simplifying deserialization of API (or other) data
Installs: 14 352
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 5
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=7.4
- ext-json: *
- symfony/framework-bundle: ^5.3|^6.0|^7.0
- symfony/property-access: ^5.3|^6.0|^7.0
- symfony/serializer: ^5.3|^6.0|^7.0
- symfony/validator: ^5.3|^6.0|^7.0
- symfony/yaml: ^5.3|^6.0|^7.0
Requires (Dev)
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2025-03-31 16:07:15 UTC
README
Bundle for deserialization of API Responses or any other json data
Installation
$ composer require visual-craft/deserializer-bundle
- Enable in kernel (Symfony 3.4)
<?php public function registerBundles() { $bundles = [ // ... new VisualCraft\DeserializerBundle\VisualCraftDeserializerBundle(), // ... ];
Or add to bundles.php (Symfony 4.0+)
<?php return [ VisualCraft\DeserializerBundle\VisualCraftDeserializerBundle::class => ['all' => true], ];
Usage
Firstly, inject DeserializerBuilderFactory to your Controller (or service):
<?php use VisualCraft\DeserializerBundle\DeserializerBuilderFactory; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class SomeController extends Controller { // ... public function __construct(DeserializerBuilderFactory $deserializerBuilderFactory) { $this->deserializerBuilderFactory = $deserializerBuilderFactory; } // ...
Or using Service Locator:
<?php use VisualCraft\DeserializerBundle\DeserializerBuilderFactory; // ... $deserializerBuilderFactory = $this->get(DeserializerBuilderFactory::class);
- Basic usage
<?php $this->deserializerBuilderFactory ->create(SomeDataClass::class) ->getDeserializer() ->deserialize($request->getContent()) ;
- Configuring object to populate
<?php // Retrieve from storage $objectToPopulate = $repository->find(1); $this->deserializerBuilderFactory ->create(SomeDataClass::class) ->setObjectToPopulate($objectToPopulate) ->getDeserializer() ->deserialize($request->getContent()) ;
- Other features example:
<?php $this->deserializerBuilderFactory ->create(SomeDataClass::class) ->setValidationGroups(['validation_group']) // or: ->setValidationGroups(function($object) { // if (something) return ['validation_group']; // endif }) ->setDeserializationGroups(['deserialization_group']) ->getDeserializer() ->deserialize($request->getContent()) ;
License
MIT