PHP Classes

PHP JSON Object Create: Fill PHP objects from a JSON string

Recommend this page to a friend!
  Info   View files Example   View files View files (43)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 105 This week: 1All time: 9,696 This week: 560Up
Version License PHP version Categories
json-to-popo 1.0.0Custom (specified...5PHP 5, Data types
Description 

Author

This class can fill PHP objects from a JSON string.

It can take a string with a values defined in the JSON format and create an object of a given class with values from the JSON string.

The package parses the class definition to extract the expected names and types of the object variables, so it knows how to set the object variable values.

Innovation Award
PHP Programming Innovation award nominee
August 2020
Number 9
JSON is nowadays a popular means to represent data as a string that can be stored in files, databases or in any other type of storage.

This class provides means to load the values of an object of a given class from a JSON string by parsing the class to determine how to assign the object variables from values extracted from the data structured defined in JSON format.

Manuel Lemos
Picture of Temuri Takalandze
  Performance   Level  
Name: Temuri Takalandze <contact>
Classes: 6 packages by
Country: Georgia Georgia
Age: 24
All time rank: 35775 in Georgia Georgia
Week rank: 416 Up1 in Georgia Georgia Up
Innovation award
Innovation award
Nominee: 4x

Winner: 1x

Example

<?php

/*
 * This file is part of the abgeo/json-to-popo.
 *
 * Copyright (C) 2020 Temuri Takalandze <takalandzet@gmail.com>.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

use ABGEO\POPO\Example\Person;
use
ABGEO\POPO\Composer;

require
__DIR__ . '/../vendor/autoload.php';
require
__DIR__ . '/POPO/Department.php';
require
__DIR__ . '/POPO/Position.php';
require
__DIR__ . '/POPO/Person.php';

$composer = new Composer();
$jsonContent = file_get_contents(__DIR__ . '/example.json');

$resultObject = $composer->composeObject($jsonContent, Person::class);

var_dump($resultObject);


Details

json-to-popo

Fill Plain Old PHP Object with JSON content.

Build Status Coverage Status GitHub release Packagist Version GitHub license

Installation

You can install this library with Composer:

  • `composer require abgeo/json-to-popo`

Usage

Include composer autoloader in your main file (Ex.: index.php)

  • `require __DIR__.'/../vendor/autoload.php';`

Consider that you have example.json with the following content:

{
  "firstName": "Temuri",
  "lastName": "Takalandze",
  "active": true,
  "position": {
    "title": "Developer",
    "department": {
      "title": "IT"
    }
  }
}

and several POPO classes to represent this JSON data:

Department.php

<?php

class Department
{
    /
     * @var string
     */
    private $title;

    // Getters and Setters here...
}

Position.php

<?php

class Position
{
    /
     * @var string
     */
    private $title;

    /
     * @var \ABGEO\POPO\Example\Department
     */
    private $department;

    // Getters and Setters here...
}

Person.php

<?php

class Person
{
    /
     * @var string
     */
    private $firstName;

    /
     * @var string
     */
    private $lastName;

    /
     * @var bool
     */
    private $active;

    /
     * @var \ABGEO\POPO\Example\Position
     */
    private $position;

    // Getters and Setters here...
}

Note: All POPO properties must have full qualified @var annotation with the correct data type.

Now you want to convert this JSON to POPO with relations. This package gives you this ability.

Let's create new ABGEO\POPO\Composer object and read example.json content:

$composer = new Composer();
$jsonContent = file_get_contents(__DIR__ . '/example.json');

Time for magic! Call composeObject() with the contents of JSON and the main class, and this will give you POPO:

$resultObject = $composer->composeObject($jsonContent, Person::class);

Print $resultObject:

var_dump($resultObject);

//class ABGEO\POPO\Example\Person#2 (4) {
//  private $firstName =>
//  string(6) "Temuri"
//  private $lastName =>
//  string(10) "Takalandze"
//  private $active =>
//  bool(true)
//  private $position =>
//  class ABGEO\POPO\Example\Position#4 (2) {
//    private $title =>
//    string(9) "Developer"
//    private $department =>
//    class ABGEO\POPO\Example\Department#7 (1) {
//      private $title =>
//      string(2) "IT"
//    }
//  }
//}

See full example here.

Changelog

Please see CHANGELOG for details.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Authors

License

Copyright © 2020 Temuri Takalandze. Released under the MIT license.


  Files folder image Files  
File Role Description
Files folder image.github (1 file)
Files folder imageexamples (2 files, 1 directory)
Files folder imagesrc (1 file, 1 directory)
Files folder imagetests (4 files, 1 directory)
Accessible without login Plain text file .coveralls.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json 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  /  .github  
File Role Description
  Accessible without login Plain text file FUNDING.yml Data Auxiliary data

  Files folder image Files  /  examples  
File Role Description
Files folder imagePOPO (3 files)
  Accessible without login Plain text file example.json Data Auxiliary data
  Accessible without login Plain text file example.php Example Example script

  Files folder image Files  /  examples  /  POPO  
File Role Description
  Plain text file Department.php Class Class source
  Plain text file Person.php Class Class source
  Plain text file Position.php Class Class source

  Files folder image Files  /  src  
File Role Description
Files folder imageUtil (2 files)
  Plain text file Composer.php Class Class source

  Files folder image Files  /  src  /  Util  
File Role Description
  Plain text file AnnotationParser.php Class Class source
  Plain text file Normalizer.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageMeta (2 directories)
  Plain text file AnnotationParserTest.php Class Class source
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Plain text file ComposerTest.php Class Class source
  Plain text file NormalizerTest.php Class Class source

  Files folder image Files  /  tests  /  Meta  
File Role Description
Files folder imageClasses (11 files)
Files folder imageJSON (12 files)

  Files folder image Files  /  tests  /  Meta  /  Classes  
File Role Description
  Plain text file Class1.php Class Class source
  Plain text file Class10.php Class Class source
  Plain text file Class11.php Class Class source
  Plain text file Class2.php Class Class source
  Plain text file Class3.php Class Class source
  Plain text file Class4.php Class Class source
  Plain text file Class5.php Class Class source
  Plain text file Class6.php Class Class source
  Plain text file Class7.php Class Class source
  Plain text file Class8.php Class Class source
  Plain text file Class9.php Class Class source

  Files folder image Files  /  tests  /  Meta  /  JSON  
File Role Description
  Accessible without login Plain text file 1.json Data Auxiliary data
  Accessible without login Plain text file 10.json Data Auxiliary data
  Accessible without login Plain text file 11.json Data Auxiliary data
  Accessible without login Plain text file 12.json Data Auxiliary data
  Accessible without login Plain text file 2.json Data Auxiliary data
  Accessible without login Plain text file 3.json Data Auxiliary data
  Accessible without login Plain text file 4.json Data Auxiliary data
  Accessible without login Plain text file 5.json Data Auxiliary data
  Accessible without login Plain text file 6.json Data Auxiliary data
  Accessible without login Plain text file 7.json Data Auxiliary data
  Accessible without login Plain text file 8.json Data Auxiliary data
  Accessible without login Plain text file 9.json Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:105
This week:1
All time:9,696
This week:560Up