PHP code example of visual-craft / deserializer-bundle

1. Go to this page and download the library: Download visual-craft/deserializer-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

visual-craft / deserializer-bundle example snippets




public function registerBundles()
{
    $bundles = [
        // ...
        new VisualCraft\DeserializerBundle\VisualCraftDeserializerBundle(),
        // ...
    ];



return [
    VisualCraft\DeserializerBundle\VisualCraftDeserializerBundle::class => ['all' => true],
];




use VisualCraft\DeserializerBundle\DeserializerBuilderFactory;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class SomeController extends Controller
{
    // ...
    public function __construct(DeserializerBuilderFactory $deserializerBuilderFactory)
    {
        $this->deserializerBuilderFactory = $deserializerBuilderFactory;
    }
// ...



use VisualCraft\DeserializerBundle\DeserializerBuilderFactory;

// ...

$deserializerBuilderFactory = $this->get(DeserializerBuilderFactory::class);



$this->deserializerBuilderFactory
    ->create(SomeDataClass::class)
    ->getDeserializer()
    ->deserialize($request->getContent())
;



// Retrieve from storage
$objectToPopulate = $repository->find(1);
$this->deserializerBuilderFactory
    ->create(SomeDataClass::class)
    ->setObjectToPopulate($objectToPopulate)
    ->getDeserializer()
    ->deserialize($request->getContent())
;



$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())
;