itav / serializer
Installs: 13 527
Dependents: 11
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^7.1
Requires (Dev)
- phpunit/phpunit: ^6.5
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": { "itav/serializer": "~1.2" } }
or simply fire up on terminal:
composer require itav/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 Itav\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);
How to Contribute
write to me: sylwester7799@gmail.com