PHP code example of thiagocordeiro / serializer

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

    

thiagocordeiro / serializer example snippets




declare(strict_types=1);

namespace App\ValueObject;

class User
{
    private string $name;
    private string $email;

    public function __construct(string $name, string $email)
    {
        $this->name = $name;
        $this->email = $email;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getEmail(): string
    {
        return $this->email;
    }
}


class User
{
    public readonly string $name;
    public readonly string $email;

    public function __construct(string $name, string $email)
    {
        $this->name = $name;
        $this->email = $email;
    }
}

use Serializer\Builder\Encoder\EncoderFactory;
use Serializer\Builder\Encoder\FileLoader\PipelineEncoderFileLoader;
use Serializer\Builder\Decoder\DecoderFactory;
use Serializer\Builder\Decoder\FileLoader\PipelineDecoderFileLoader;
use Serializer\JsonSerializer;

$encoder = new EncoderFactory(PipelineEncoderFileLoader::full('path/to/cache'));
$decoder = new DecoderFactory(PipelineDecoderFileLoader::full('path/to/cache'));
$serializer = new JsonSerializer($encoder, $decoder);

$json = '{"name":"Arthur Dent","email":"[email protected]"}';
$serializer->deserialize($json, \App\ValueObject\User::class);

// or

$json = '[
  {"name":"Arthur Dent","email":"[email protected]"},
  {"name":"Chuck Norris","email":"[email protected]"},
  ...
]';
$serializer->deserialize($json, \App\ValueObject\User::class);



use Serializer\Builder\Encoder\EncoderFactory;
use Serializer\Builder\Encoder\FileLoader\PipelineEncoderFileLoader;
use Serializer\Builder\Decoder\DecoderFactory;
use Serializer\Builder\Decoder\FileLoader\PipelineDecoderFileLoader;
use Serializer\JsonSerializer;

$encoder = new EncoderFactory(PipelineEncoderFileLoader::full('path/to/cache'));
$decoder = new DecoderFactory(PipelineDecoderFileLoader::full('path/to/cache'));
$serializer = new JsonSerializer($encoder, $decoder);

$user = new \App\ValueObject\User('Arthur Dent', '[email protected]');

$json = $serializer->serialize($user);
// will return {"name":"Arthur Dent","email":"[email protected]"}

// or
$json = $serializer->serialize([$user1, $user2, ...]);



declare(strict_types=1);

namespace App\ValueObject;

class User
{
    // properties...

    public function __construct(string $name, string $email, ?Address $address)
    {
        $this->name = $name;
        $this->email = $email;
        $this->address = $address;
    }

    // getters
}



use ValueObject\Address;

class User
{
    // properties...

    /**
     * @param Address[] $addresses
     */
    public function __construct(string $name, array $addresses)
    {
        $this->name = $name;
        $this->addresses = $addresses;
    }

    // getters
}



declare(strict_types=1);

namespace App\ValueObject;

class User
{
    // properties...

    public function __construct(string $name, string $email, string $type = 'user')
    {
        $this->name = $name;
        $this->email = $email;
        $this->type = $type;
    }

    // getters
}



declare(strict_types=1);

namespace App\ValueObject;

class User
{
    // properties...

    public function __construct(Place ...$places)
    {
        $this->places = $places;
    }

    // getters
}



declare(strict_types=1);

namespace App\ValueObject;

class User
{
    // properties...

    public function __construct(DateTime $createdAt, DateTimeImmutable $updatedAt)
    {
        $this->createdAt = $createdAt;
        $this->updatedAt = $updatedAt;
    }

    // getters
}