Serializer

Opensource ByJG Scrutinizer Code Quality Code Coverage Build Status

Serialize any object into array and format it JSON or XML

Basic Usage

Just use the Serializer class with any kind of object, stdClass or array;

<?php
$serializer = new \ByJG\Serializer\SerializerObject($data);
$result = $object->build();

$result is an array. You can use a Formatter to transform it in JSON or XML.

Formatting the Output with a formatter

<?php
$serializer = new \ByJG\Serializer\SerializerObject($data);
$result = $serializer->build();

echo (new JsonFormatter())->process($result);
echo (new XmlFormatter())->process($result);

Customizing the Serialization

Ignore null elements: setBuildNull(false)

The SerializerObject brings all properties by default. For example:

<?php
$myclass->setName('Joao');
$myclass->setAge(null);

$serializer = new \ByJG\Serializer\SerializerObject($myclass);
$result = $serializer->build();
print_r($result);

// Will return:
// Array
// (
//     [name] => Joao
//     [age] => 
// )

But you can setup for ignore the null elements:

<?php
$serializer = new \ByJG\Serializer\SerializerObject($myclass);
$result = $serializer->setBuildNull(false)->build();
print_r($result);

// And the result will be:
// Array
// (
//     [name] => Joao
// )

Do not parse some classes: setDoNotParse([object])

Sometimes we want to serialize the object but ignore some class types.

Setting this option below the whole classes defined in the setDoNotParse will be ignored and not parsed:

<?php
$serializer = new \ByJG\Serializer\SerializerObject($myclass);
$result = $serializer->setDoNotParse([
    MyClass::class
]);

Create a bindable object

Add to the object the method bind that allows set contents from another object

<?php
// Create the class
class MyClass extends BinderObject
{}

// Bind any data into the properties of myclass
$myclass->bind($data);

// You can convert to array all properties
$myclass->toArray();

Copy contents from any object to another

// Set all properties from $source that matches with the property in $target
BinderObject::bindObject($source, $target);

// Convert all properties of any object into array
BinderObject::toArrayFrom($source);

Install

composer require "byjg/serialize=1.0.*"

Test

phpunit

Open source ByJG