Allows generate class files parse from json and map json to php object, including multi-level and complex objects;

Overview

nixihz/php-object

Allows generate class files parse from json and map json to php object, including multi-level and complex objects;

Installation

You can add this library as a local, per-project dependency to your project using Composer:

composer require nixihz/php-object

If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:

composer require --dev nixihz/php-object

Feature

  • Generate PHP Class files from json
  • Map json to object

Generate PHP class file from json

Once you have executed composer require nixihz/php-object you could use vendor/bin/php-object-gen to generate class file from json

Usage:

  1. demo
$./vendor/bin/php-object-gen --path=/tmp --json='{"foo_bar":{"foo":1.3,"bar":[641,641]}}'

output
Class FooBar saved at: /tmp/FooBar.php
Class BaseClass saved at: /tmp/BaseClass.php

  1. add params --help for more
$./vendor/php-object-gen --helper

Map json to object

fromJson map json to object

fromJson($json); var_export($demo); ">
class FooBarClass extends PhpObject
{
    /** @var string */
    public $foo;

    /** @var integer */
    public $bar;
}

class DemoClass extends PhpObject
{
    /** @var FooBarClass */
    public $foobar;

    /** @var FooBarClass[] */
    public $foobars;

}

$json = '{"foobar":{"foo":"hello world","bar":64100},"foobars":[{"foo":"hello","bar":641},{"foo":"world","bar":664411}]}';
$demo = (new DemoClass())->fromJson($json);

var_export($demo);

outputs:

DemoClass::__set_state(array(
   'foobar' => 
      FooBarClass::__set_state(array(
         'foo' => 'hello world',
         'bar' => 64100,
      )),
   'foobars' => 
      array (
        0 => 
        FooBarClass::__set_state(array(
           'foo' => 'hello',
           'bar' => 641,
        )),
        1 => 
        FooBarClass::__set_state(array(
           'foo' => 'world',
           'bar' => 664411,
        )),
      ),
))

You might also like...
An article about alternative solution for convert object into a JSON Object for your api.

Do we really need a serializer for our JSON API? The last years I did build a lot of JSON APIs but personally was never happy about the magic of using

Creating data transfer objects with the power of php objects. No php attributes, no reflection api, and no other under the hook work.

Super Simple DTO Creating data transfer objects with the power of php objects. No php attributes, no reflection api, and no other under the hook work.

PHP package to make your objects strict and throw exception when you try to access or set some undefined property in your objects.

📢 Yell PHP package to make your objects strict and throw exception when you try to access or set some undefined property in your objects. Requirement

Composer Plugin for automatically including files for easing function usage in php.

Php Inc Php inc is a composer plugin for automatically including certain files into composer's autoload and autoload-dev files config. Given a set of

Deeper is a easy way to compare if 2 objects is equal based on values in these objects. This library is heavily inspired in Golang's reflect.DeepEqual().

Deeper Deeper is a easy way to compare if 2 objects is equal based on values in these objects. This library is heavily inspired in Golang's reflect.De

MajorDoMo is an open-source DIY smarthome automation platform aimed to be used in multi-protocol and multi-services environment.
MajorDoMo is an open-source DIY smarthome automation platform aimed to be used in multi-protocol and multi-services environment.

MajorDoMo (Major Domestic Module) is an open-source DIY smarthome automation platform aimed to be used in multi-protocol and multi-services environment. It is based on web-technologies stack and ready to be delivered to any modern device. It is very flexible in configuration with OOP paradigm used to set up automation rules and scripts. This platform can be installed on almost any personal computer running Windows or Linux OS.

An object store for specific objects.

Object Storage A simple object storage class that will only allow "supported" objects. class MyObjectStorage extends Herrera\Util\ObjectStorage {

PHP functions that help you validate structure of complex nested PHP arrays.

PHP functions that help you validate structure of complex nested PHP arrays.

Your alter ego object. Takes the best of object and array worlds.

Supporting Opensource formapro\values is an MIT-licensed open source project with its ongoing development made possible entirely by the support of com

Owner
zhixin
"nixihz" is reverse for "zhixin"
zhixin
JSONFinder - a library that can find json values in a mixed text or html documents, can filter and search the json tree, and converts php objects to json without 'ext-json' extension.

JSONFinder - a library that can find json values in a mixed text or html documents, can filter and search the json tree, and converts php objects to json without 'ext-json' extension.

Eboubaker Eboubaker 2 Jul 31, 2022
Allows reflection of object attributes, including inherited and non-public ones

sebastian/object-reflector Allows reflection of object attributes, including inherited and non-public ones. Installation You can add this library as a

Sebastian Bergmann 6k Jan 4, 2023
Easy management of Virtualization technologies including KVM, Xen, OpenVZ, Virtuozzo, and LXC/LXD including unified commands, monitoring, template management, and many more features.

ProVirted About Easy management of Virtualization technologies including KVM, Xen, OpenVZ, Virtuozzo, and LXC/LXD including unified commands, monitori

null 2 Aug 22, 2022
Talkino allows you to integrate multi social messengers and contact into your website and enable your users to contact you using multi social messengers' accounts.

Talkino Welcome to our GitHub Repository Talkino is a click to chat plugin to show your agents’ multiple social messengers, phone and emails on the ch

Traxconn 2 Sep 21, 2022
PHP library that helps to map any input into a strongly-typed value object structure.

Valinor • PHP object mapper with strong type support Valinor is a PHP library that helps to map any input into a strongly-typed value object structure

Team CuyZ 873 Jan 7, 2023
Parse DSN strings into value objects to make them easier to use, pass around and manipulate

DSN parser Parse DSN strings into value objects to make them easier to use, pass around and manipulate. Install Via Composer composer require nyholm/d

Tobias Nyholm 77 Dec 13, 2022
Venture allows you to create and manage complex, async workflows in your Laravel apps.

Venture is a package to help you build and manage complex workflows of interdependent jobs using Laravel's queueing system. Installation Note: Venture

Kai Sassnowski 680 Dec 14, 2022
Generate static map url for maptiler.com

PHP static maps url generator The package is used for static maps (images) URL generate. For now, it works only with maptiler.com. I welcome your comm

Andrey Surzhikov 2 Sep 8, 2022
Immutable value object for IPv4 and IPv6 addresses, including helper methods and Doctrine support.

IP is an immutable value object for (both version 4 and 6) IP addresses. Several helper methods are provided for ranges, broadcast and network address

Darsyn 224 Dec 28, 2022