jeurboy/object-field-selector

There is no license information available for the latest version (0.1.3) of this package.

Class for mapping large and complex object or varible into smaller object

0.1.3 2018-10-01 05:47 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:42:04 UTC


README

Latest Stable Version Total Downloads Monthly Downloads Daily Downloads

Description

The library for filter complex array/hash or object and return the actual fields you want to use.

If you have a large variables contained with unused values which not necessary to used. The library will helps by filter out all the unused fields and left your fields on return variable.

The library can traverse the complex array or nested array in any level of array/hash or object.

For a deeper knowledge of how to use this package, follow this index:

Installation

You can install the package via composer require command:

composer require jeurboy/object-field-selector

Or simply add it to your composer.json dependences and run composer update:

"require": {
    "jeurboy/object-field-selector": "*"
}

Usage

For simple usage. Just create returned schema and send to DataParser;

    $parser = new Jeurboy\SimpleObjectConverter\SchemaParser();
    $parser->addSchema("test1");
    $token = $parser->getParsedSchema();

    $data = [
        'test1' => "11234",
        'test2' => "22222"
    ];

    $DataParser = new Jeurboy\SimpleObjectConverter\DataParser();
    $return = $DataParser->getOutput($data, $token);
    print_r($return);

You will see following result.

Array
(
    [test1] => 11234
)

If you have nested Array just pass .(Dot) in your schema.

    $parser = new Jeurboy\SimpleObjectConverter\SchemaParser();
    $parser->addSchema("test1.eee");
    $token = $parser->getParsedSchema();

    $data = [
        'test1' => [
            'eee' => "hello holy",
        ]
    ];

    $DataParser = new Jeurboy\SimpleObjectConverter\DataParser();
    $return = $DataParser->getOutput($data, $token);
    print_r($return);

You will get result array like this.

Array
(
    [test1] => Array
        (
            [eee] => hello holy
        )
)

If there are multiple field inside input array, use comma(,) to define more fields.

    $parser = new Jeurboy\SimpleObjectConverter\SchemaParser();
    $parser->addSchema("test1");
    $token = $parser->getParsedSchema();

    $data = [
        'test1' => [
            'eee' => "hello holy",
            'fff' => "hello holy",
        ]
    ];

    $DataParser = new Jeurboy\SimpleObjectConverter\DataParser();
    $return = $DataParser->getOutput($data, $token);
    print_r($return);

You will see the result has multiple field returned.

Array
(
    [test1] => Array
        (
            [eee] => hello holy
            [fff] => hello holy
        )

)

The library also support object inside sequencial array.

    $parser = new Jeurboy\SimpleObjectConverter\SchemaParser();
    $parser->addSchema("test1.hello");
    $token = $parser->getParsedSchema();

    $data = [
        'test1' => [
            [
                "hello" => "world1",
                "ok"    => "google1",
            ],[
                "hello" => "world2",
                "ok"    => "google2",
            ],
        ]
    ];
    $DataParser = new Jeurboy\SimpleObjectConverter\DataParser();
    $return = $DataParser->getOutput($data, $token);
    print_r($return);

Result is

Array
(
    [test1] => Array
        (
            [0] => Array
                (
                    [hello] => world1
                )

            [1] => Array
                (
                    [hello] => world2
                )

        )

)

You can see all test case in example/example.php files.