mapado/request-fields-parser

Convert string like `id,firstname,lastname,jobs{startDate,position,company{id,recordNumber}}` to the array

v1.1.0 2018-02-12 15:57 UTC

This package is auto-updated.

Last update: 2020-01-08 16:04:50 UTC


README

Convert string like id,firstname,lastname,jobs{startDate,position,company{id,recordNumber}} to the following array:

[
    'id' => true,
    'firstname' => true,
    'lastname' => true,
    'jobs' => [
        'startDate' => true,
        'position' => true,
        'company' => [
            'id' => true,
            'recordNumber' => true,
        ],
    ]
]

You can think of it like an explode on steroids.

Installation

composer require mapado/request-fields-parser

Usage

use Mapado\RequestFieldsParser\Parser;

$parser = new Parser();

$outArray = $parser->parse($string);

Extensibility

You can decorate the Parser like this:

use Mapado\RequestFieldsParser\ParserInterface;

class ExtendedParser implements ParserInterface
{
    /**
     * @var ParserInterface
     */
    private $decoratedParser;

    public function __construct(ParserInterface $decoratedParser)
    {
        $this->decoratedParser = $decoratedParser;
    }

    public function parse(string $sttring): array
    {
        // do stuff and return an array
    }
}

Contribute

Just run make test to launch the test suite