PHP Classes

PHP JSON to Object Mapper: Map JSON decoded data into entity objects

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 136 All time: 9,277 This week: 660Up
Version License PHP version Categories
php-json-to-entity 1.0.0Custom (specified...7Language, PHP 7
Description 

Author

This package can map JSON decoded data into entity objects.

It takes an object decoded from a JSON string an creates another object of a given class set to the values of the input object.

Picture of Niko
  Performance   Level  
Innovation award
Innovation award
Nominee: 5x

Winner: 1x

 

Documentation

JSON to Entity

pipeline status coverage report

Map json into entity. This allows you to easily validate payload json and map it automatically into entity class. This class can be for example ORM class, when you can directly save it into the DB.

This is pretty nice, if you're lazy and need just to develop fast, but if you need high performance application, please map json and validate json manually. Comes with performance strike, but saves time.

Install

Via composer:

composer require niko9911/json-to-entity

Usage

<?php
declare(strict_types=1);

// Declare entity where to map.
final class Basic
{
    /
     * @var string
     */
    private $bar;
    /
     * @var int|null
     */
    private $foo;
    /
     * @var array
     */
    private $fooBar;

    /
     * BasicUnitTestEntity constructor.
     *
     * @param string $bar
     * @param int    $foo
     * @param array  $fooBar
     */
    public function __construct(string $bar, ?int $foo, array $fooBar)
    {
        $this->bar = $bar;
        $this->foo = $foo;
        $this->fooBar = $fooBar;
    }

    /
     * @return string
     */
    public function getBar(): string
    {
        return $this->bar;
    }

    /
     * @return int|null
     */
    public function getFoo(): ?int
    {
        return $this->foo;
    }

    /
     * @return array
     */
    public function getFooBar(): array
    {
        return $this->fooBar;
    }
}

// JSON
$json = <<<JSON
{
  "bar": "Some_Bar",
  "foo": 10,
  "fooBar": ["a", "b", "c"]
}
JSON;

$mapper = new \Niko9911\JsonToEntity\Mapper();
$entity = $mapper->map(\json_decode($json), Basic::class);
var_dump($entity);
//class Basic#25 (3) {
//  private $bar =>
//  string(8) "Some_Bar"
//  private $foo =>
//  int(10)
//  private $fooBar =>
//  array(3) {
//    [0] =>
//    string(1) "a"
//    [1] =>
//    string(1) "b"
//    [2] =>
//    string(1) "c"
//  }
//}


License

Licensed under the MIT license.


  Files folder image Files (33)  
File Role Description
Files folder imagesrc (1 file, 1 directory)
Files folder imagetests (1 file, 2 directories)
Accessible without login Plain text file .php_cs.dist Example Example script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Read me

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:136
This week:0
All time:9,277
This week:660Up