byjg/serializer

Serialize any object into array and format it JSON or XML

4.9.1 2024-01-03 23:50 UTC

This package is auto-updated.

Last update: 2024-04-07 23:56:16 UTC


README

Build Status Opensource ByJG GitHub source GitHub license GitHub release

Serialize any object into array and format it JSON, YAML or XML

Converting any object/content into array

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

<?php
$result = \ByJG\Serializer\SerializerObject::instance($data)->serialize();
$result2 = \ByJG\Serializer\SerializerObject::instance($anyJsonString)->fromJson()->serialize();
$result3 = \ByJG\Serializer\SerializerObject::instance($anyYamlString)->fromYaml()->serialize();

In the examples above $result, $result2 and $result3 will be an associative array.

Formatting an array into JSON, YAML or ZML

<?php
$data = [ ... any array content ... ]

echo (new JsonFormatter())->process($data);
echo (new XmlFormatter())->process($data);
echo (new YamlFormatter())->process($data);
echo (new PlainTextFormatter())->process($data);

Customizing the Serialization

Ignore null elements: withDoNotSerializeNull()

The SerializerObject brings all properties by default. For example:

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

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

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

But you can setup for ignore the null elements:

<?php
$result = \ByJG\Serializer\SerializerObject::instance($myclass)
            ->withDoNotSerializeNull()
            ->serialize();
print_r($result);

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

Do not parse some classes: withDoNotParse([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
$result = \ByJG\Serializer\SerializerObject::instance($myclass)
            ->withDoNotParse([
                MyClass::class
            ])
            ->serialize();

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 BindableObject
{}

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

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

Copy contents from any object to another

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

// Convert all properties of any object into array
SerializerObject::serialize($source);

Copy contents from an object with CamelCase properties to another with snake_case properties

class Source
{
    public $idModel;
    public $clientName;
    public $age;
}

class Target
{
    public $id_model;
    public $client_name;
    public $age;
}

$source = new Source();
$source->idModel = 1;
$source->clientName = 'John';
$source->age = 30;

BinderObject::bind($source, $target, new CamelToSnakeCase());

Copy contents from an object with snake_case properties to another with CamelCase properties

class Source
{
    public $id_model;
    public $client_name;
    public $age;
}

class Target
{
    public $idModel;
    public $clientName;
    public $age;
}

$source = new Source();
$source->id_model = 1;
$source->client_name = 'John';
$source->age = 30;

BinderObject::bind($source, $target, new SnakeToCamelCase());

Install

composer require "byjg/serialize"

Test

vendor/bin/phpunit

Dependencies

flowchart TD
    byjg/serializer --> ext-json
    byjg/serializer --> symfony/yaml

Open source ByJG