didix16/php-apidataobject

A simple library that allows easy handle incoming data from any sources (specially from API sources) and put data in a fashion way which is more practical to access it like an array or object. It allows to implement a DTO very fast.

v1.0.3 2021-09-23 10:47 UTC

This package is auto-updated.

Last update: 2024-04-23 16:06:10 UTC


README

A simple library that allows easy handle incoming data from any sources (specially from API sources) and put data in a fashion way which is more practical to access it like an array or object. It allows to implement a DTO very fast.

Content

What is an ApiDataObject

It is just a class that can handle incoming data from API sources in JSON format or any other format and structure them in memory to easy handling like an associative array and/or general object.

To pass data into this class, first it should be a simple PHP associative array or plain PHP std object.

It can be created by using JSON string text.

Installation

composer require didix16/php-apidataobject

Usage

class ApiPlatformDataObject extends ApiDataObject {}

...

class AWebHookProcessor {

    /**
     * @var array|object
     */
    $data;

    public function process()
    {
        $apiData = new ApiPlatformDataObject($this->data);

        /**
         * data = 
         * [
         *  'property1' => 'value1',
         *  'property2' => 'value2',
         *  'property3' => 'value3',
         *  'property4' => 'value4',
         * ...
         * ]
         */

        /**
         * Different accessors
         */
        $apiData['property1'];
        $apiData->property1;
        $apiData->property1();

        /**
         * Different setters
         */
        $apiData['property1'] = 'value5';
        $apiData->property1 = 'value5';

        // chainable setter properties
        $apiData
        ->property1('value5')
        ->property2('value6')
        ...
    }

    public function processJson(string $json)
    {
        $apiData = ApiPlatformDataObject::fromJson($json);

        /**
         * Different accessors
         */
        $apiData['property1'];
        $apiData->property1;
        $apiData->property1();

        /**
         * Different setters
         */
        $apiData['property1'] = 'value5';
        $apiData->property1 = 'value5';

        // chainable setter properties
        $apiData
        ->property1('value5')
        ->property2('value6')
        ...

    }
}

See also

  • php-apidatamapper - A DTO library that allows map incoming API data to any of your entities/models by using a simple filed mapping language.