PHP code example of terrazza / serializer

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

    

terrazza / serializer example snippets

`
//
// $logger has to be a Psr\Log\LoggerInterface implementation
//

use Terrazza\Component\Serializer\Factory\DeserializerFactory;

class ReadmeTargetObject {
    public int $id;
    public ?int $amount=null;
    public function __construct(int $id) {
        $this->id = $id;
    }
    public function getId() : int {
        return $this->id;
    }
    public function setAmount(?int $amount) : void {
        $this->amount = $amount;
    }
    public function getAmount() :?int {
        return $this->amount;
    }
}

$content = json_encode(
    [
        'id' => 12,
        'amount' => 100
    ]
);

$deserializer   = new DeserializerFactory($logger);
$object         = $deserializer->deserialize(TargetObject::class, "json", $content);
var_dump($object);

/*
class ReadmeTargetObject {
  public int $id =>
  int(12)
  public ?int $amount =>
  int(100)
}
*/
`
//
// $logger has to be a Psr\Log\LoggerInterface implementation
//

use Terrazza\Component\Serializer\Factory\SerializerFactory;

class ReadmeTargetObject {
    public int $id;
    public ?int $amount=null;
    public function __construct(int $id) {
        $this->id = $id;
    }
    public function getId() : int {
        return $this->id;
    }
    public function setAmount(?int $amount) : void {
        $this->amount = $amount;
    }
    public function getAmount() :?int {
        return $this->amount;
    }
}

$object         = new ReadmeTargetObject(12);
$object->setAmount(100);
$serializer     = new SerializerFactory($logger);
$response       = $serializer->serialize($object, "json");

var_dump($response);
/*
{"id":12,"amount":100}
*/
`
//
// $logger has to be a Psr\Log\LoggerInterface implementation
//

use Terrazza\Component\Serializer\Decoder\JsonDecoder;
use Terrazza\Component\Serializer\Denormalizer;
use Terrazza\Component\Serializer\Encoder\JsonEncoder;
use Terrazza\Component\Serializer\Normalizer;
use Terrazza\Component\Serializer\Serializer;
use Terrazza\Component\Serializer\Deserializer;

class ReadmeTargetObject {
    public int $id;
    public ?int $amount=null;
    public function __construct(int $id) {
        $this->id = $id;
    }
    public function getId() : int {
        return $this->id;
    }
    public function setAmount(?int $amount) : void {
        $this->amount = $amount;
    }
    public function getAmount() :?int {
        return $this->amount;
    }
}

$data = [
    'id'        => 1,
    'amount'    => 13
];
$input          = json_encode($data);
$logger         = Logger::get();
$deserializer   = (new Deserializer(
    new JsonDecoder(),
    new Denormalizer($logger)
));
$object         = $deserializer->deserialize(ReadmeTargetObject::class, $input);
echo $object->getId();      // 1
echo $object->getName();    // Max 

$serializer = (new Serializer(
    new JsonEncoder(),
    new Normalizer($logger)
));
var_dump(json_encode($data) === $serializer->serialize($object)); // true     
`
//
// $logger has to be a Psr\Log\LoggerInterface implementation
//

use \Terrazza\Component\Serializer\Denormalizer;

class ReadmeTargetObject {
    public int $id;
    public ?int $amount=null;
    public function __construct(int $id) {
        $this->id = $id;
    }
    public function getId() : int {
        return $this->id;
    }
    public function setAmount(?int $amount) : void {
        $this->amount = $amount;
    }
    public function getAmonut() :?int {
        return $this->amount;
    }
}

$object         = new ReadmeTargetObject(12);
$denormalizer   = new Denormalizer($logger);
$values         = $denormalizer->denormalizeMethodValues($object, "setAmount", [
    "amount" => 12, "unknown" => 11
]);
//
// property "unkonwn" has been removed: property does not exists in method
//
var_dump([
            12
        ] === $values);