encjacom / serializer
There is no license information available for the latest version (v2.0.2) of this package.
v2.0.2
2020-12-01 12:11 UTC
README
Table of Contents
main features:
- work recursively up to 50 nested objects/arrays
- handling \DateTime objects
- implement Java style naming convention.
Installation
Add Serializer to your composer.json
file.
{
"require": {
"encjacom/serializer": "~1.2"
}
}
or simply fire up on terminal:
composer require encjacom/serializer
Then at the top of your PHP script require the autoloader:
require 'vendor/autoload.php';
Quick Start
Php Serializer allows you to switch between:
objects --- arrays --- json, xml
<?php
use iPresso\Component\Serializer\Serializer;
require_once '../vendor/autoload.php';
$serializer = new Serializer();
class Car
{
private $model = 'SomeModel';
private $color = 'red';
/**
* @var Part[]
*/
private $parts;
public function setParts($parts)
{
$this->parts = $parts;
}
}
class Part
{
private $number = 123;
private $name = 'engine';
}
$car = new Car();
$car->setParts([new Part(), new Part(), new Part()]);
$array = $serializer->normalize($car);
$json = json_encode($array);
and also in reverse direction:
json --- arrays --- objects
<?php
$json = <<<JSON
{
"model":"MyModel",
"color":"red",
"parts":[
{
"number":123,
"name":"engine"
},
{
"number":124,
"name":"lamp"
},
{
"number":125,
"name":"wheel"
}
]
}
JSON;
//$array = json_decode($json, true);
$array = [
'model' => 'MyModel',
'color' => 'red',
'parts' => [
0 => [
'number' => 123,
'name' => 'engine'
],
1 => [
'number' => 124,
'name' => 'lamp'
],
2 => [
'number' => 125,
'name' => 'wheel'
],
]
];
$car = $serializer->denormalize($array, Car::class);