terrazza/serializer

Terrazza Component Serializer

Installs: 2

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Type:cakephp-plugin

1.0.0 2021-12-17 16:11 UTC

This package is auto-updated.

Last update: 2024-05-12 00:35:14 UTC


README

This component is meant to be used to turn objects into a specific format (XML,JSON,YAML,...) and the other way around.

  1. Methods
    1. Deserializer
      1. Decoder
      2. Denormalizer
        1. method: denormalize
        2. method: denormalizeMethodValues
    2. Serializer
      1. Normalizer
      2. Encoder
    3. Factory
      1. DeserializerFactory
      2. SerializerFactory
  2. Examples
  3. Install
  4. Requirements

Deserializer

Deserializer is a combination of

  1. decode (JSON,XML,CSV) a given string into an array
  2. denormalize into a class

Decode

The decoder converts a given string into an array. Actually Terrazza/Serializer supports

  • JSON
  • XML

Denormalize

The Denormalizer supports two methods.

method: denormalizeClass

This method convert in input into the given className and

  • validate input types
  • load/handle nested objects

Properties:

  • className (string)
  • input (mixed)
  • restrictArguments (default: false)

Business logic

  1. initialize className with __constructor (if public)
  2. handle unused arguments with "setter"-methods (if public)

method: denormalizeMethodValues

This method map/convert given arguments into/based on an object and his methodName.

Properties:

  • object
  • methodName (string)
  • input (mixed)
  • restrictArguments (default: false)

How should a class be designed

We suggest that all required arguments are handled by the __constructor
and all optional arguments are handled by the setter.

Serializer

Serialize is a combination of

  1. normalize object to array
  2. encode array to (JSON,..)

Normalizer

The Normalizer converts an object into an array.

Business logic

  1. try to find for all properties by there "getter" methods (get{}, is{}, has{})
    found: retrieve related property value

  2. for all properties, public accessible mandatory (not handled by methods)
    found: retrieve property value

Encoder

The encoder converts an array to a string by using.

actually provided encodings:

  • Json

Factory

Every factory covers his parent and provides, "contentType" based, an automatic execution.

DeserializerFactory

//
// $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)
}
*/

SerializerFactory

//
// $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}
*/

Examples

Unserialize + Serialize JSON (without Factory)

//
// $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     

Denormalizer::denormalizeMethodValues

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

How to install

Install via composer

composer require terrazza/serializer

Requirements

php version

  • >= 7.4

php extension

  • ext-json
  • ext-libxml

composer packages

  • psr/log
  • terrazza/annotation

composer packages (require-dev)

  • terrazza/logger