PHP code example of spiral / marshaller-bridge

1. Go to this page and download the library: Download spiral/marshaller-bridge 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/ */

    

spiral / marshaller-bridge example snippets


protected const LOAD = [
    // ...
    \Spiral\MarshallerBridge\Bootloader\MarshallerBootloader::class,
];



declare(strict_types=1);

use Spiral\Core\Container\Autowire;
use Spiral\Marshaller\Mapper\AttributeMapperFactory;
use Spiral\Marshaller\Type\ArrayType;
use Spiral\Marshaller\Type\DateTimeType;
use Spiral\Marshaller\Type\DateIntervalType;
use Spiral\Marshaller\Type\EnumType;
use Spiral\Marshaller\Type\ObjectType;

return [
    'mapperFactory' => AttributeMapperFactory::class,
    'matchers' => [
        EnumType::class,
        DateTimeType::class,
        DateIntervalType::class,
        ArrayType::class,
        ObjectType::class,
    ],
];

use Spiral\Serializer\SerializerManager;

$serializer = $this->container->get(SerializerManager::class);

$result = $manager->serialize($payload, 'marshaller-json');
$result = $manager->serialize($payload, 'marshaller-serializer');

$result = $manager->unserialize($payload, Post::class, 'marshaller-json');
$result = $manager->unserialize($payload, Post::class, 'marshaller-serializer');

use Spiral\Marshaller\MarshallerInterface;

$marshaller = $this->container->get(MarshallerInterface::class);

$result = $marshaller->marshal($from); // from object to array
$result = $marshaller->unmarshal($from, new Post()); // from array to object