terrazza / serializer
Terrazza Component Serializer
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Type:cakephp-plugin
Requires
- php: >=7.4
- ext-json: *
- ext-libxml: *
- psr/log: 1.1.*
- terrazza/reflectionclass: 1.0.*
Requires (Dev)
- phpunit/phpunit: ^9.3
- terrazza/logger: 1.0.*
- vimeo/psalm: 4.x-dev
README
This component is meant to be used to turn objects into a specific format (XML,JSON,YAML,...) and the other way around.
- Methods
- Examples
- Install
- Requirements
Deserializer
Deserializer is a combination of
- decode (JSON,XML,CSV) a given string into an array
- denormalize into a class
Decode
The decoder converts a given string into an array. Actually Terrazza/Serializer supports
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
- initialize className with __constructor (if public)
- 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
- normalize object to array
- encode array to (JSON,..)
Normalizer
The Normalizer converts an object into an array.
Business logic
- try to find for all properties by there "getter" methods (get{}, is{}, has{})
found: retrieve related property value - 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