Giter Club home page Giter Club logo

json-to-popo's Introduction

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.

json-to-popo's People

Contributors

abgeo avatar dependabot-preview[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

json-to-popo's Issues

Support for PHP 8.0?

Hi,
thank you for package. I used this package with PHP 8.0 and it works without problem.
Can you add to composer support or is there any possible issue?
"php": "^7.2|^8",

"php": "^7.2",

Thank you.
BR, Klemen

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.