loading...

Create PHP Object from JSON Content

abgeo profile image Temuri Takalandze ・1 min read

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...
}

Now you want to convert this JSON to POPO with relations. The ABGEO07/json-to-popo package gives you this ability. Install it using Composer:
composer require abgeo/json-to-popo

Now 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"
//    }
//  }
//}

Posted on by:

abgeo profile

Temuri Takalandze

@abgeo

Middle Software Engineer, having over 3 years of experience in Back-End development; The author of several PHP libraries and modules; Open Source Contributor.

Discussion

markdown guide