rafrsr / lib-array2object
Array to object conversion library
Installs: 17 177
Dependents: 4
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.4
- doctrine/common: ^2.6|^3.0
- doctrine/inflector: ^1.0|^2.0
- symfony/property-access: ^2.8|^3.4|^4.0|^5.0
Requires (Dev)
- phpunit/phpunit: ^5.7.27
- satooshi/php-coveralls: ^1.1
README
Array to object conversion library. Convert a array to a object using simple and common property annotations
Installation
- Install composer
- 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.