PHP Classes
elePHPant
Icontem

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

Recommend this page to a friend!

  Author Author  
Name: Niko <contact>
Classes: 5 packages by
Country: Finland Finland


  Detailed description   Download Download .zip .tar.gz  
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.

Details

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.


  Classes of Niko  >  PHP JSON to Object Mapper  >  Download Download .zip .tar.gz  >  Support forum Support forum  >  Blog Blog  >  RSS 1.0 feed RSS 2.0 feed Latest changes  
Name: PHP JSON to Object Mapper
Base name: php-json-to-entity
Description: Map JSON decoded data into entity objects
Version: -
PHP version: 7
License: Custom (specified in a license file)
 
  Groups   Applications   Files Files  

  Groups  
Group folder image Language Constructs to assist in the language control View top rated classes
Group folder image PHP 7 Classes using PHP 7 specific features View top rated classes


  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  Files folder image Files  
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

  Files folder image Files  /  src  
File Role Description
Files folder imageDomain (2 files, 2 directories)
  Plain text file Mapper.php Class Class source

  Files folder image Files  /  src  /  Domain  
File Role Description
Files folder imageException (5 files)
Files folder imageProperty (1 file)
  Plain text file EntityBuilder.php Class Class source
  Plain text file Strings.php Class Class source

  Files folder image Files  /  src  /  Domain  /  Exception  
File Role Description
  Plain text file Exception.php Class Class source
  Plain text file MappingException.php Class Class source
  Plain text file PropertyNotNullableException.php Class Class source
  Plain text file PropertyUndefinedException.php Class Class source
  Plain text file ValueCannotBeCaste...edTypeException.php Class Class source

  Files folder image Files  /  src  /  Domain  /  Property  
File Role Description
  Plain text file Inspector.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageStubs (1 directory)
Files folder imageUnit (2 files)
  Plain text file UnitTestCase.php Class Class source

  Files folder image Files  /  tests  /  Stubs  
File Role Description
Files folder imageUnit (2 directories)

  Files folder image Files  /  tests  /  Stubs  /  Unit  
File Role Description
Files folder imageBasicUnitTest (5 files)
Files folder imageComplexUnitTest (10 files)

  Files folder image Files  /  tests  /  Stubs  /  Unit  /  BasicUnitTest  
File Role Description
  Plain text file BasicUnitTestEntity.php Class Class source
  Plain text file BasicUnitTestEntityBarMissing.php Class Class source
  Plain text file BasicUnitTestEntityNoGetters.php Class Class source
  Accessible without login Plain text file success.json Data Auxiliary data
  Accessible without login Plain text file successHyphens.json Data Auxiliary data

  Files folder image Files  /  tests  /  Stubs  /  Unit  /  ComplexUnitTest  
File Role Description
  Plain text file ComplexMainEntity.php Class Class source
  Plain text file ComplexSubEntity.php Class Class source
  Plain text file ComplexSubSubEntity.php Class Class source
  Accessible without login Plain text file failureCastStringToInt.json Data Auxiliary data
  Accessible without login Plain text file failureCastTooBigInt.json Data Auxiliary data
  Accessible without login Plain text file failurePropertyCannotBeNull.json Data Auxiliary data
  Accessible without login Plain text file failurePropertyNotNullable.json Data Auxiliary data
  Accessible without login Plain text file failureSubSubNotNullable.json Data Auxiliary data
  Accessible without login Plain text file success.json Data Auxiliary data
  Accessible without login Plain text file successHyphens.json Data Auxiliary data

  Files folder image Files  /  tests  /  Unit  
File Role Description
  Plain text file BasicUnitTest.php Class Class source
  Plain text file ComplexUnitTest.php Class Class source

Download Download all files: php-json-to-entity.tar.gz php-json-to-entity.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.