Skip to content

rafrsr/lib-array2object

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lib-array2object

Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version Latest Unstable Version Total Downloads License

SensioLabsInsight

Array to object conversion library. Convert a array to a object using simple and common property annotations

Installation

  1. Install composer
  2. Execute: composer require rafrsr/lib-array2object

Usage

Only need the class with common annotations

<?php

class Player
{
    /** @var string */
    protected $name;

   /** @var integer */
    protected $number;

   //setters and getters here
}

To use:

$phpArray = ['name' => 'David', 'number' => '1'];
$object = Array2ObjectBuilder::create()->build()->createObject(Player::class, $phpArray);
echo $object->getName();//David
echo $object->getNumber();//1

$array = Object2ArrayBuilder::create()->build()->createArray($object);
echo $array['name'];//David
echo $array['number'];//1

Serialization

This library can be used to serialize/deserialize objects without a complex configuration.

Json
//deserialize
$json = '{"name":"David","number"=>"1"}';
$object = Array2ObjectBuilder::create()->build()->createObject(Team::class, json_decode($json, true));
echo $object->getName()//David

//serialize
$array = Object2ArrayBuilder::create()->build()->createArray($object);
echo json_encode($array); // {"name":"David","number"=>"1"}
Xml

Using the library rafrsr/lib-array2xml is very handy convert from/to xml

//deserialize
$xml = '<Player><name>Player 1</name><number>1</number></Player>';
$object = Array2ObjectBuilder::create()->build()->create(Team::class, XML2Array::createArray($xml));
echo $object->getName()//David

//serialize
$array = Object2ArrayBuilder::create()->build()->createArray($object);
echo Array2XML::createXml('Player', $array); //  '<Player><name>Player 1</name><number>1</number></Player>'

This library its handy to use and has some advanced configuration, but in some cases (need groups, versions and other stuffs) is required use advanced libraries like jms/serializer

Documentation

Full documentation are available on the wiki page

Copyright

This project is licensed under the MIT license.

About

Array to object conversion library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages